NGINX location块的用法

location块写在server中,一个server中可以有多个location。

location匹配规则

= 最高优先级匹配
^~ 某个开头
~ 区分大小写
~* 表示不区分大小写
!~ 取反不匹配大小写
!~* 表示不区分大小写不匹配的正则
/    通用匹配,任何请求都会匹配到
@    内部服务跳转

优先级

= 大于 ^~  大于 ~|~*|!~|!~* 大于 /


匹配方式

location / {
    root /www;
    index index.html;
}

表示匹配www根下面的全部资源。
带路径匹配

location /abc {
    root /www;
    index index.html;
   
}

表示匹配www下的abc文件夹内的全部资源。

区分大小写匹配

location ~ / {
     root /www;
     index index.html;
}

意思是匹配abc为小写。

* 和^

location ^~ / {    // 匹配任意开头
    root /www;
    index index.html;
    
}

location ~*  .jpg$ {  //匹配jpg结尾
    root /www;
    index index.html

} 

重定向和地址重写

重写语句有

if、rewrite、set、return

IF语句

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 		请求到达的服务器端口号;

Bash

Rewrite flag

last 			    表示完成rewrite。默认为last。
break 				本条规则匹配完成后,终止匹配,不再匹配后面的规则
redirect 			返回302临时重定向,浏览器地址会显示跳转后的URL地址
permanent 		    返回301永久重定向,浏览器地址会显示跳转后URL地址

Perl

redirect 和 permanent区别则是返回的不同方式的重定向:

对于客户端来说一般状态下是没有区别的。而对于搜索引擎,相对来说301的重定向更加友好,如果我们把一个地址采用301跳转方式跳转的话,搜索引擎会把老地址的相关信息带到新地址,同时在搜索引擎索引库中彻底废弃掉原先的老地址。

使用302重定向时,搜索引擎(特别是google)有时会查看跳转前后哪个网址更直观,然后决定显示哪个,如果它觉的跳转前的URL更好的话,也许地址栏不会更改。

rewrite

重定向到另一个页面

location /a {
    root /www;
    index index.html;
    rewrite .* /b/index.html permanent;  //匹配到a路径的后面任意字符进行重定向
}
location /b {
    root /www;
    index index.html;


}

匹配定向开头跳转

location /2019/a {
    root /www;
    index index.html;
    rewrite ^/2019/(.*)$ /2018/$1 permanent; //指定开头跳转带后面url跳转
}
location /2018/a {
    root /www;
    index index.html;
}

匹配域名重定向到新的域名

location /a {
    root /www;
    index.html;
    if ( $host ~* 域名 ) {
    rewrite .* http://jd.com$request_url permanent;
    }
    

}

匹配内容地址跳转

location /login {
    root /www;
    index index.html;
    rewrire ^/login/(.*)\.html$ http://$host/reg/login.html?user=$1;

}
匹配到域名加login会跳转到reg
location /reg {
    root /www;
    index index.html;
    

}

匹配二级域名跳转www二级域名移到后面

server {
    listen       80;
    server_name  www.testpm.com;

    location / {
         root   /usr/share/nginx/html;
         index  index.html index.htm;
         if ( $host ~* ^www.testpm.com$) {
                break;
                }
         if ( $host ~* "^(.*)\.testpm\.com$" ) {
                set $user $1;
                rewrite .* http://www.testpm.com/$user permanent;
                }
        }
    location /jack {
         root /usr/share/nginx/html;
         index  index.html index.hml;
        }
    location /alice {
         root /usr/share/nginx/html;
         index index.html index.hml;
        }
}

return 指令

匹配一个不能访问的地址时返回代码403

server {
    listen       80;
    server_name  www.testpm.cn;
    #access_log  /var/log/nginx/http_access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        }

    location ~* \.sh$ {
        return 403;
        }	
}

你可能感兴趣的:(nginx,运维)