nginx server、location、root、alias、try_files 指令使用说明

目录

1. url 结构说明

https://zhangsan:123456@hostname:8888/path/path2?param=value#h123

参考 URL - Web API 接口参考 | MDN

示例部分 英文名 中文名 说明
https: protocol 协议名 http:, https:
zhangsan username 用户名
123456 password 密码
hostname hostname 主机名
hostname:8888 host 包含域(即主机名)、后跟
(如果指定了端口)“:”和URL的端口
8888 port 端口
/path/path2 pathname 文件路径
#h123 hash 片段标识符
?param=value search 参数字符串 以开头的“?”开头

2. nginx server 部分简单说明

参考 nginx 官网 server 说明

server {
	# 监听的端口
    listen      80; 
    # 监听的主机名,可以包含 “*” 进行模糊匹配,可以使用正则表达式,进行正则匹配, 空格分隔
    server_name example.org www.example.org *.chengwithle.com ~^(www\.)?(?<domain>.+)$; 
	location / {
		....
	}
}

3. nginx location 部分简单说明

参考 nginx 官网 location 说明

Syntax: location [ = | ~ | ~* | ^~ ] uri { … }
             location @name { … }
Default: —
Context: server, location

    # 对 uri 【实践下来发现应该是 url 中的 pathname 】进行匹配 
    # 对 pathname 以 / 开头的,全局匹配
    location / {
		
	}
	# 对 pathname 以 /user/ 开头的
	location /user/ {
		...
	}
	# 精确匹配
	location = /user/png.jpg {
		
	}
	# 大小写敏感正则匹配
	location ~ \.png$ {
		...
	}
	# 大小写不敏感正则匹配
	location  ~* \.(gif|jpg|jpeg)$ {
		...
	}
	# 如果匹配,则不继续匹配其他的。前缀匹配
	location ^~ /images/ {

	}

优先级

  1. 精确匹配 =
  2. 前缀匹配 ^~(立刻停止后续的正则搜索)
  3. 按文件中顺序的正则匹配 ~或 ~*
  4. 匹配不带任何修饰的前缀匹配,选择匹配最长的前缀匹配

@ 的用法 参考

4. root,alias, try_files 的相关配置

4.1 root

Syntax: root path;
Default: root html;
Context: http, server, location, if in location

设置请求的根目录。例

location /i/ {
    root /data/w3;
}

对于请求 pathname 为 /i/top.gif, /data/w3/i/top.gif 将被返回

path 可以包含变量, 除了 $document_root 和 $realpath_root.
文件的路径是通过向 root 指令的值追加 pathname 来构造的。如果必须修改路径,则应使用 alias 指令

4.2 alias

Syntax: alias path;
Default: —
Context: location

这部分英语原文写的很好,所以就直接中英都加上了
Defines a replacement for the specified location. For example, with the following configuration
定义指定位置的替换。例如,使用以下配置

location /i/ {
    alias /data/w3/images/;
}

on request of “/i/top.gif”, the file /data/w3/images/top.gif will be sent.
对于 “/i/top.gif”的请求,将发送文件/data/w3/images/top.gif。

The path value can contain variables, except $document_root and $realpath_root.
路径值可以包含变量,但 $document_root 和 $realpath_root 除外。

If alias is used inside a location defined with a regular expression then such regular expression should contain captures and alias should refer to these captures (0.7.40), for example:
如果在使用正则表达式定义的 location 内使用 alias,则此类正则表达式应包含 capture,并且别名应引用这些 capture (0.7.40),例如:

location ~ ^/users/(.+\.(?:gif|jpe?g|png))$ {
    alias /data/w3/images/$1;
}

When location matches the last part of the directive’s value:
当 location 匹配指令值的最后一部分时:

location /images/ {
    alias /data/w3/images/;
}

it is better to use the root directive instead:
最好使用 root 指令代替:

location /images/ {
    root /data/w3;
}

4.3 try_files

Syntax: try_files file … uri;
             try_files file … =code;
Default: —
Context: server, location

Checks the existence of files in the specified order and uses the first found file for request processing; the processing is performed in the current context. The path to a file is constructed from the file parameter according to the root and alias directives. It is possible to check directory’s existence by specifying a slash at the end of a name, e.g. “$uri/”. If none of the files were found, an internal redirect to the uri specified in the last parameter is made. For example:
意思是:如果配置了 try_files, 将会寻找 root 或 alias 的值,来寻找文件,最后一项是默认值,可以配置为 内部 uri、错误码、命名location

location /images/ {
    try_files $uri /images/default.gif;
}

location = /images/default.gif {
    expires 30s;
}

The last parameter can also point to a named location, as shown in examples below. Starting from version 0.7.51, the last parameter can also be a code:

location / {
    try_files $uri $uri/index.html $uri.html =404;
}

Example in proxying Mongrel:

location / {
    try_files /system/maintenance.html
              $uri $uri/index.html $uri.html
              @mongrel;
}

location @mongrel {
    proxy_pass http://mongrel;
}

Example for Drupal/FastCGI:

location / {
    try_files $uri $uri/ @drupal;
}

location ~ \.php$ {
    try_files $uri @drupal;

    fastcgi_pass ...;

    fastcgi_param SCRIPT_FILENAME /path/to$fastcgi_script_name;
    fastcgi_param SCRIPT_NAME     $fastcgi_script_name;
    fastcgi_param QUERY_STRING    $args;

    ... other fastcgi_param's
}

location @drupal {
    fastcgi_pass ...;

    fastcgi_param SCRIPT_FILENAME /path/to/index.php;
    fastcgi_param SCRIPT_NAME     /index.php;
    fastcgi_param QUERY_STRING    q=$uri&$args;

    ... other fastcgi_param's
}

In the following example,

location / {
    try_files $uri $uri/ @drupal;
}

the try_files directive is equivalent to

location / {
    error_page 404 = @drupal;
    log_not_found off;
}

And here,

location ~ \.php$ {
    try_files $uri @drupal;

    fastcgi_pass ...;

    fastcgi_param SCRIPT_FILENAME /path/to$fastcgi_script_name;

    ...
}

try_files checks the existence of the PHP file before passing the request to the FastCGI server.

Example for Wordpress and Joomla:

location / {
    try_files $uri $uri/ @wordpress;
}

location ~ \.php$ {
    try_files $uri @wordpress;

    fastcgi_pass ...;

    fastcgi_param SCRIPT_FILENAME /path/to$fastcgi_script_name;
    ... other fastcgi_param's
}

location @wordpress {
    fastcgi_pass ...;

    fastcgi_param SCRIPT_FILENAME /path/to/index.php;
    ... other fastcgi_param's
}
上一篇:[Nginx优化]分享nginx配置文件及优化说明


下一篇:LAMP和LNMP环境搭建的艰辛历程