nginx的localtion指令详解

it2025-09-26  3

nginx的localtion指令详解

Nginx 的 HTTP 配置主要包括三个区块,结构如下:

http { # 这个是协议级别   include mime.types;   default_type application/octet-stream;   keepalive_timeout 65;   gzip on;     server { # 这个是服务器级别       listen 80;       server_name localhost;         location / { # 这个是请求级别           root html;           index index.html index.htm;         }       } }

1、location 区段

location 是在 server 块中配置,根据不同的 URI 使用不同的配置,来处理不同的请求。location 是有顺序的,会被第一个匹配的location 处理。基本语法如下: location [=|~|~*|^~|@] pattern{……} 2、location 前缀含义 = 表示精确匹配,优先级也是最高的  ^~ 表示uri以某个常规字符串开头,理解为匹配url路径即可  ~ 表示区分大小写的正则匹配  ~* 表示不区分大小写的正则匹配 !~ 表示区分大小写不匹配的正则 !~* 表示不区分大小写不匹配的正则 / 通用匹配,任何请求都会匹配到 @ 内部服务跳转查找顺序和优先级 = 大于 ^~ 大于 ~|~*|!~|!~* 大于 / 多个location配置的情况下匹配顺序为:首先匹配 =,其次匹配^~, 其次是按正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。 3、location 配置示例3、location 配置示例 1、没有修饰符 表示:必须以指定模式开始 server { listen 80; server_name localhost; ​ location /abc { root /home/www/nginx; index 2.html; } ​ 那么,如下是对的:http://192.168.1.9/abc 2、=表示:必须与指定的模式精确匹配 server { listen 80; server_name localhost; access_log /var/log/nginx/http_access.log main; ​ location / { root /usr/share/nginx/html; index a.html index.htm; } location = / { root /usr/share/nginx/html; index b.html index.htm; } } ​ 测试:http://192.168.1.9 =/http://192.168.1.9/a.html / 3、~ 表示:指定的正则表达式要区分大小写 server { server_name localhost;   location ~ /abc { root /home/www/nginx; index 2.html index.html; } } 测试访问:http://192.168.1.9/abc 不正确的http://192.168.1.9/ABC ======================================== 如果将配置文件修改为 location ~ /ABC { root /home/www/nginx; index 2.html index.html; } 在创建目录和文件: [root@ansible-server html]# cd /home/www/nginx/ [root@ansible-server nginx]# mkdir ABC [root@ansible-server nginx]# vim ABC/2.html 访问:http://192.168.1.9/ABC/ 结论:~ 需要区分大小写。而且目录需要根据大小写定义。 4、^~ :类似于无修饰符的行为,也是以指定模式开始,不同的是,如果模式匹配,那么就停止搜索其他模式了。 5、@ :定义命名 location 区段,这些区段客户段不能访问,只可以由内部产生的请求来访问,如error_page等 location 区段匹配示例 ​ location = / {   # 只匹配 / 的查询.   [ configuration A ] } location / {   # 匹配任何以 / 开始的查询,但是正则表达式与一些较长的字符串将被首先匹配。   [ configuration B ] } location ^~ /images/ {   # 匹配任何以 /images/ 开始的查询并且停止搜索,不检查正则表达式。   [ configuration C ] } location ~* \.(gif|jpg|jpeg)$ {   # 匹配任何以gif, jpg, or jpeg结尾的文件   [ configuration D ] } 各请求的处理如下例: / → configuration A /documents/document.html → configuration B /images/1.gif → configuration C /documents/1.jpg → configuration D

12、nginx 地址重写 rewrite

1、什么是Rewrite

Rewrite对称URL Rewrite,即URL重写,就是把传入Web的请求重定向到其他URL的过程

URL Rewrite最常见的应用是URL伪静态化,是将动态页面显示为静态页面方式的一种技术。比如http://www.123.com/news/index.php?id=123 使用URLRewrite 转换后可以显示为 http://www.123.com/news/123.html从安全角度上讲,如果在URL中暴露太多的参数,无疑会造成一定量的信息泄漏,可能会被一些黑客利用,对你的系统造成一定的破坏,所以静态化的URL地址可以给我们带来更高的安全性。实现网站地址跳转,例如用户访问http://360buy.com,将其跳转到http://jd.com。例如当用户访问http://tianyun.com的 80端口时,将其跳转到443端口。

2、Rewrite 相关指令

Nginx Rewrite 相关指令有 if、rewrite、set、return

2.1、if 语句

应用环境 server,location 语法: if (condition) { … } if 可以支持如下条件判断匹配符号 ~ 正则匹配 (区分大小写) ~* 正则匹配 (不区分大小写) !~ 正则不匹配 (区分大小写) !~* 正则不匹配 (不区分大小写) -f 和!-f 用来判断是否存在文件 -d 和!-d 用来判断是否存在目录 -e 和!-e 用来判断是否存在文件或目录 -x 和!-x 用来判断文件是否可执行 ​ 在匹配过程中可以引用一些Nginx的全局变量 $args 请求中的参数; $document_root 针对当前请求的根路径设置值; $host 请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名; $limit_rate 对连接速率的限制; $request_method 请求的方法,比如"GET"、"POST"等; $remote_addr 客户端地址; $remote_port 客户端端口号; $remote_user 客户端用户名,认证用; $request_filename 当前请求的文件路径名(带网站的主目录/usr/local/nginx/html/images /a.jpg) $request_uri 当前请求的文件路径名(不带网站的主目录/images/a.jpg) $query_string 与$args相同; $scheme 用的协议,比如http或者是https $server_protocol 请求的协议版本,"HTTP/1.0"或"HTTP/1.1"; $server_addr 服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(造成资源浪费); $server_name 请求到达的服务器名; $document_uri 与$uri一样,URI地址; $server_port 请求到达的服务器端口号;2.2、Rewrite flagrewrite 指令根据表达式来重定向URI,或者修改字符串。可以应用于server,location, if环境下每行rewrite指令最后跟一个flag标记,支持的flag标记有: last 相当于Apache里的[L]标记,表示完成rewrite。默认为last。 break 本条规则匹配完成后,终止匹配,不再匹配后面的规则 redirect 返回302临时重定向,浏览器地址会显示跳转后的URL地址 permanent 返回301永久重定向,浏览器地址会显示跳转后URL地址 redirect 和 permanent区别则是返回的不同方式的重定向: 对于客户端来说一般状态下是没有区别的。而对于搜索引擎,相对来说301的重定向更加友好,如果我们把一个地址采用301跳转方式跳转的话,搜索引擎会把老地址的相关信息带到新地址,同时在搜索引擎索引库中彻底废弃掉原先的老地址。 使用302重定向时,搜索引擎(特别是google)有时会查看跳转前后哪个网址更直观,然后决定显示哪个,如果它觉的跳转前的URL更好的话,也许地址栏不会更改。

2.3、Rewrite匹配参考示例

本地解析host文件 # http://www.testpm.com/a/1.html ==> http://www.testpm.com/b/2.html location /a { root /html; index 1.html index.htm; rewrite .* /b/2.html permanent; } location /b { root /html; index 2.html index.htm; } 例2: # http://www.testpm.com/2019/a/1.html ==> http://www.testpm.com/2018/a/1.html location /2019/a { root /var/www/html; index 1.html index.hml; rewrite ^/2019/(.*)$ /2018/$1 permanent; } location /2018/a { root /var/www/html; index 1.html index.htl; } ​ 例3: # http://www.qf.com/a/1.html ==> http://jd.com location /a { root /html; if ($host ~* www.qf.com ) { rewrite .* http://jd.com permanent; } } ​ 例4: # http://www.qf.com/a/1.html ==> http://jd.com/a/1.html location /a { root /html; if ( $host ~* qf.com ){ rewrite .* http://jd.com$request_uri permanent; } } ​ 例5: 在访问目录后添加/ (如果目录后已有/,则不加/) # http://www.tianyun.com/a/b/c # $1: /a/b # $2: c # http://$host$1$2/ location /a/b/c { root /usr/share/nginx/html; index index.html index.hml; if (-d $request_filename) { rewrite ^(.*)([^/])$ http://$host$1$2/ permanent; } } ​ 例6: # http://www.tianyun.com/login/tianyun.html ==> http://www.tianyun.com/reg/login.html?user=tianyun location /login { root /usr/share/nginx/html; rewrite ^/login/(.*)\.html$ http://$host/reg/login.html?user=$1; } location /reg { root /usr/share/nginx/html; index login.html; ​ } ​ 例7: #http://www.tianyun.com/qf/11-22-33/1.html ==> http://www.tianyun.com/qf/11/22/33/1.html location /qf { rewrite ^/qf/([0-9]+)-([0-9]+)-([0-9]+)(.*)$ /qf/$1/$2/$3$4 permanent; } ​ location /qf/11/22/33 { root /html; index 1.html; }
最新回复(0)