ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

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

2021-07-19 11:30:33  阅读:254  来源: 互联网

标签:files uri server try alias location root fastcgi


目录

1. url 结构说明

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

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

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

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
}

标签:files,uri,server,try,alias,location,root,fastcgi
来源: https://blog.csdn.net/qq_22783587/article/details/118891858

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有