Nginx Rewrite相关功能-ngx_http_rewrite_module模块指令概述

        Nginx Rewrite相关功能-ngx_http_rewrite_module模块指令概述

                                          作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

   Nginx服务器利用ngx_http_rewrite_module 模块解析和处理rewrite请求,此功能依靠 PCRE(perl compatible regularexpression),因此编译之前要安装PCRE库,rewrite是nginx服务器的重要功能之一,用于实现URL的重写,URL的重写是非常有用的功能,比如它可以在我们改变网站结构之后,不需要客户端修改原来的书签,也无需其他网站修改我们的链接,就可以设置为访问,另外还可以在一定程度上提高网站的安全性。

  ngx_http_rewrite_module模块的官网文档:https://nginx.org/en/docs/http/ngx_http_rewrite_module.html。

一.if指令(用于条件判断)

1>.if指令功能简介

  用于条件匹配判断,并根据条件判断结果选择不同的Nginx配置,可以配置在server或location块中进行配置,Nginx的if语法仅能使用if做单次判断,不支持使用if else或者if elif这样的多重判断。

2>.编辑nginx的主配置文件(本篇博客试验基本上不会修改nginx的主配置文件,而是在主配置文件中加载子配置文件)

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf/nginx.conf
worker_processes  4;
worker_cpu_affinity 00000001 00000010 00000100 00001000; 
 
events {
   worker_connections  100000;
   use epoll;
   accept_mutex on;
   multi_accept on; 
}
   
   http {
     include       mime.types;
       
     default_type  text/html;
    
     server_tokens off; 
      
     charset utf-8;
   
     log_format my_access_json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"re
sponsetime":$request_time,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"uri":"$uri",' '"domain":"$host",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"tcp_xff":"$proxy_protocol_addr",' '"http_user_agent":"$http_user_agent",' '"status":"$status"}';   
    access_log logs/access_json.log my_access_json;
 
    ssl_certificate /yinzhengjie/softwares/nginx/certs/www.yinzhengjie.org.cn.crt;
    ssl_certificate_key /yinzhengjie/softwares/nginx/certs/www.yinzhengjie.org.cn.key;
    ssl_session_cache shared:sslcache:20m;
    ssl_session_timeout 10m;
  
    include /yinzhengjie/softwares/nginx/conf.d/*.conf;
}
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/nginx/conf.d/
total 12
-rw-r--r-- 1 root root 274 Dec 22 12:41 https.conf
-rw-r--r-- 1 root root 504 Dec 23 22:38 login.conf
-rw-r--r-- 1 root root 528 Dec 22 12:43 mobile.conf
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/https.conf 
server {
    listen 80;
    listen 443 ssl;
    server_name www.yinzhengjie.org.cn;
   
    location / {
       root /yinzhengjie/data/web/nginx/static;
       index index.html;
    }

    location = /favicon.ico {
       root /yinzhengjie/data/web/nginx/images/jd;
    }
}
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/https.conf
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/mobile.conf 
server {
    listen 80;
    listen 443 ssl;
    server_name mobile.yinzhengjie.org.cn;

    ssl_certificate /yinzhengjie/softwares/nginx/certs/mobile.yinzhengjie.org.cn.crt;
    ssl_certificate_key /yinzhengjie/softwares/nginx/certs/mobile.yinzhengjie.org.cn.key;
    ssl_session_cache shared:sslcache:20m;
    ssl_session_timeout 10m;   

    location / {
       root /yinzhengjie/data/web/nginx/mobile;
       index index.html;
    }

    location /favicon.ico {
       root /yinzhengjie/data/web/nginx/images/taobao;
    }
}
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/mobile.conf

3>.编辑子配置文件

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/login.conf 
server {
    listen 80;
    listen 443 ssl;
    server_name node101.yinzhengjie.org.cn;
   
    location / {
       root /yinzhengjie/data/web/nginx/static;
       index index.html;
    }

    location /login {
       root /yinzhengjie/data/web/nginx;
       
       default_type text/html;
       
       index index.html;
       #判断用户使用的协议
       if ( $scheme != http ){
         echo "if ----> you use scheme is [$scheme]";
       }
       #判断用户访问的文件是否存在
       if ( !-f $request_filename ){
         echo "file is exists";
       }
    }

    location = /favicon.ico {
       root /yinzhengjie/data/web/nginx/images/jd;
    }
}
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 

4>.准备测试数据

[root@node101.yinzhengjie.org.cn ~]# mkdir -pv /yinzhengjie/data/web/nginx/login
mkdir: created directory ‘/yinzhengjie/data/web/nginx/login’
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cp /yinzhengjie/data/web/nginx/static/index.html /yinzhengjie/data/web/nginx/login/
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/data/web/nginx/login/index.html 
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>字体的样式</title>
        <style>
            .hello{
                color: red;
                font-size: 30px;
                font-family: "curlz mt","华文彩云","arial", "微软雅黑";
            }
        </style>
    </head>
    <body>
        <p class="hello">2019尹正杰到此一游,在这里提前祝大家2020年新年快乐~</p>
    </body>
</html>

[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll /yinzhengjie/data/web/nginx/login
total 4
-rw-r--r-- 1 root root 362 Dec 23 22:44 index.html
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 

5>.启动nginx服务

[root@node101.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q                             Local Address:Port                                            Peer Address:Port              
LISTEN      0      128                                            *:22                                                         *:*                  
LISTEN      0      128                                           :::22                                                        :::*                  
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q                             Local Address:Port                                            Peer Address:Port              
LISTEN      0      128                                            *:80                                                         *:*                  
LISTEN      0      128                                            *:22                                                         *:*                  
LISTEN      0      128                                            *:443                                                        *:*                  
LISTEN      0      128                                           :::22                                                        :::*                  
[root@node101.yinzhengjie.org.cn ~]# 

6>.浏览器访问"http://node101.yinzhengjie.org.cn/login/"时,可以正常打开网页

7>.浏览器访问"https://node101.yinzhengjie.org.cn/login/",得到结果如下图所示。

8>.浏览器访问有关不存在的URL,得到结果如下图所示。

9>.Nginx的if指令用法介绍

  用于条件匹配判断,并根据条件判断结果选择不同的Nginx配置,可以配置在server或location块中进行配置,Nginx的if语法仅能使用if做单次判断,不支持使用if else或者if elif这样的多重判断。

  使用正则表达式对变量进行匹配,匹配成功时if指令认为条件为true,否则认为false,变量与表达式之间使用以下符号链接:
    =:
      比较变量和字符串是否相等,相等时if指令认为该条件为true,反之为false。
    !=: 
      比较变量和字符串是否不相等,不相等时if指令认为条件为true,反之为false。
    ~: 
      表示在匹配过程中区分大小写字符,(可以通过正则表达式匹配),满足匹配条件为真,不满足为假。
    ~*: 
      表示在匹配过程中不区分大小写字符,(可以通过正则表达式匹配),满足匹配条件为真,不满足问假。
    !~:
      区分大小写不匹配,不满足为真,满足为假,不满足为真。
    !~*:
      为不区分大小写不匹配,满足为假,不满足为真。
    -f 和 ! -f:
      判断请求的文件是否存在和是否不存在,比如文件若不存在就跳转到网站首页。
    -d 和 ! -d: 
      判断请求的目录是否存在和是否不存在。
    -x 和 ! -x: 
      判断文件是否可执行和是否不可执行。
    -e 和 ! -e: 
      判断请求的文件或目录是否存在和是否不存在(包括文件,目录,软链接)。

  温馨提示: 
    如果$变量的值为空字符串或是以0开头的任意字符串,则if指令认为该条件为false,其他条件为true。

二.set指令(用于自定义变量)

1>.set指令功能介绍

  指定key并给其定义一个变量,变量可以调用Nginx内置变量赋值给key,另外set定义格式为"set $key $value",及无论是key还是value都要加$符号。

2>.编辑子配置文件并重新加载

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/login.conf 
server {
    listen 80;
    listen 443 ssl;
    server_name node101.yinzhengjie.org.cn;
   
    location / {
       root /yinzhengjie/data/web/nginx/static;
       index index.html;
    }

    location /login {
       root /yinzhengjie/data/web/nginx;
       
       default_type text/html;
       
       index index.html;
       
       #自定义一个变量"$name",其值为"yinzhengjie2019"。
       set $name yinzhengjie2019;
       
       #自定义一个变量"$my_port",其值是通过调用Nginx内置变量"$server_port"得到的。
       set $my_port $server_port;
       echo $name;
       echo $my_port;
    }

    location = /favicon.ico {
       root /yinzhengjie/data/web/nginx/images/jd;
    }
}
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]#  
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root     20229     1  0 22:49 ?        00:00:00 nginx: master process nginx
nginx    20266 20229  0 23:11 ?        00:00:00 nginx: worker process
nginx    20267 20229  0 23:11 ?        00:00:00 nginx: worker process
nginx    20268 20229  0 23:11 ?        00:00:00 nginx: worker process
nginx    20269 20229  0 23:11 ?        00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ps -ef | grep nginx | grep -v grep
root     20229     1  0 22:49 ?        00:00:00 nginx: master process nginx
nginx    20320 20229  5 23:23 ?        00:00:00 nginx: worker process
nginx    20321 20229  5 23:23 ?        00:00:00 nginx: worker process
nginx    20322 20229  6 23:23 ?        00:00:00 nginx: worker process
nginx    20323 20229  6 23:23 ?        00:00:00 nginx: worker process
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 

3>.浏览器访问"http://node101.yinzhengjie.org.cn/login/",浏览器返回结果如下图所示

4>.浏览器访问"https://node101.yinzhengjie.org.cn/login/",浏览器返回结果如下图所示

三.break指令(用于中断后续指令)

1>.break指令功能介绍

  用于中断当前相同作用域(location)中的其他Nginx配置,与该指令处于同一作用域的Nginx配置中,位于它前面的配置生效,位于后面的指令配置就不再生效了,Nginx服务器在根据配置处理请求的过程中遇到该指令的时候,回到上一层作用域继续向下读取配置,该指令可以在server块和location块以及if块中使用。

2>.编辑子配置文件并重新加载

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/login.conf 
server {
    listen 80;
    listen 443 ssl;
    server_name node101.yinzhengjie.org.cn;
   
    location / {
       root /yinzhengjie/data/web/nginx/static;
       index index.html;
    }

    location /login {
       root /yinzhengjie/data/web/nginx;
       
       default_type text/html;
       
       index index.html;
       
       #自定义一个变量"$name",其值为"yinzhengjie2019"。
       set $name yinzhengjie2019;
       
       #使用break后,其下面的其它指令将不会继续执行
       break;
       
       #自定义一个变量"$my_port",其值是通过调用Nginx内置变量"$server_port"得到的。
       set $my_port $server_port;
       echo $name;
       echo $my_port;
    }

    location = /favicon.ico {
       root /yinzhengjie/data/web/nginx/images/jd;
    }
}
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]# 

3>.浏览器访问"http://node101.yinzhengjie.org.cn/login/",浏览器返回结果如下图所示

四.return指令(可以用来返回状态码等信息)

1>.return指令功能介绍

  从nginx版本0.8.2开始支持,return用于完成对请求的处理,并直接向客户端返回响应状态码,比如其可以指定重定向URL(对于特殊重定向状态码,301/302等) 或者是指定提示文本内容(对于特殊状态码403/500等),处于此指令后的所有配置都将不被执行,return可以在server、if和location块进行配置。

2>.编辑子配置文件并重新加载

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/login.conf 
server {
    listen 80;
    listen 443 ssl;
    server_name node101.yinzhengjie.org.cn;
   
    location / {
       root /yinzhengjie/data/web/nginx/static;
       index index.html;
    }

    location /login {
       root /yinzhengjie/data/web/nginx;
       
       default_type text/html;
       
       index index.html;
       
       #如果用户使用的是http协议,咱们再这里可以做一些其它操作
       if ( $scheme = http ){
           #写法一:直接返回一个状态码
           #return 888;
           #写法二:返回一个状态码及一个字符串
           #return 999 "Not Allow Http!";
           #写法三:直接返回一个状态码和一个url
           return 301 https://node101.yinzhengjie.org.cn;
       }  
    }

    location = /favicon.ico {
       root /yinzhengjie/data/web/nginx/images/jd;
    }
}
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 

3>.浏览器访问"http://node101.yinzhengjie.org.cn/login",发现会发生跳转

五.rewrite_log指令(此指令并不常用)

1>.rewrite_log指令

  设置是否开启记录ngx_http_rewrite_module模块日志记录到error_log日志文件当中,可以配置在http、server、location或if当中,需要日志级别为notice。

2>.编辑子配置文件并重新加载

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/login.conf 
server {
    listen 80;
    listen 443 ssl;
    server_name node101.yinzhengjie.org.cn;
   
    location / {
       root /yinzhengjie/data/web/nginx/static;
       index index.html;
    }

    location /login {
       root /yinzhengjie/data/web/nginx;
       
       default_type text/html;
       
       index index.html;
       
       #如果用户使用的是http协议,咱们再这里可以做一些其它操作
       if ( $scheme = http ){
           set $name yinzhengjie2019;
           echo $name;
           rewrite_log on;
       }  
    }

    location = /favicon.ico {
       root /yinzhengjie/data/web/nginx/images/jd;
    }
}
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -t
nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nginx -s reload
[root@node101.yinzhengjie.org.cn ~]# 

六.rewrite指令(支持正则表达式)

1>.rewrite指令简介

  通过正则表达式的匹配来改变URI,可以同时存在一个或多个指令,按照顺序依次对URI进行匹配,rewrite主要是针对用户请求的URL或者是URI做具体处理。
  
  URI(universal resource identifier):
    通用资源标识符,标识一个资源的路径,可以不带协议。

  URL(uniform resource location):
    统一资源定位符,是用于在Internet中描述资源的字符串,是URI的子集,主要包括传输协议(scheme)、主机(IP、端口号或者域名)和资源具体地址(目录和文件名)等三部分。
    一般格式为 scheme://主机名[:端口号][/资源路径],如:http://www.yinzhengjie.org.cn:8080/path/file/index.html就是一个URL路径,URL必须带访问协议。

  每个URL都是一个URI,但是URI不都是URL,例如:
    http://example.org/path/to/resource.txt  #URI/URL
    ftp://example.org/resource.txt       #URI/URL
    /absolute/path/to/resource.txt       #URI

  rewrite的官方介绍地址:
    https://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite。

  rewrite可以配置在server、location、if,其具体使用方式为:
    rewrite regex replacement [flag];
  rewrite将用户请求的URI基于regex所描述的模式进行检查,匹配到时将其替换为表达式指定的新的URI。

  温馨提示:
    如果在同一级配置块中存在多个rewrite规则,那么会自下而下逐个检查;被某条件规则替换完成后,会重新一轮的替换检查,隐含有循环机制,但不超过10次;如果超过,提示500响应码,[flag]所表示的标志位用于控制此循环机制,如果替换后的URL是以"http://"或"https://"开头,则替换结果会直接以重向返回给客户端, 即永久重定向301

2>.编辑子配置文件并重新加载

[root@node101.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/nginx/conf.d/login.conf 
server {
    listen 80;
    listen 443 ssl;
    server_name node101.yinzhengjie.org.cn;
   
    location / {
       root /yinzhengjie/data/web/nginx/static;
       index index.html;
    }

    location /login {
       root /yinzhengjie/data/web/nginx;
       
       default_type text/html;
       
       index index.html;
       
       #此处我们故意写错rewrite规则,凡是访问"/login"这个URL的用户,我们不做任何判断,继续调用跳转到"http://node101.yinzhengjie.org.cn/login",从而达到一种递归跳转的效果,生产环境中我们应该避免这样配置!       
    rewrite /login http://node101.yinzhengjie.org.cn/login permanent; } location = /favicon.ico { root /yinzhengjie/data/web/nginx/images/jd; } } [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# nginx -t nginx: the configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf syntax is ok nginx: configuration file /yinzhengjie/softwares/nginx/conf/nginx.conf test is successful [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]# nginx -s reload [root@node101.yinzhengjie.org.cn ~]# [root@node101.yinzhengjie.org.cn ~]#

3>.浏览器访问"http://node101.yinzhengjie.org.cn/login",如下图所示,会发现已经有多次跳转。

4>.rewrite flag使用介绍

  利用nginx的rewrite的指令,可以实现url的重新跳转,rewrtie有四种不同的flag,分别是redirect(临时重定向)、permanent(永久重定向)、break和last。
    redirect:
      临时重定向,重写完成后以临时重定向方式直接返回重写后生成的新URL给客户端,由客户端重新发起请求,需要注意的是,临时重定向客户端浏览器并不会缓存数据到本地。使用相对路径,或者http://或https://开头,状态码:302
    permanent:
      重写完成后以永久重定向方式直接返回重写后生成的新URL给客户端,由客户端重新发起请求,需要注意的是,永久重定向客户端浏览器会缓存数据到本地。状态码:301
    last:
      重写完成后停止对当前URI在当前location中后续的其它重写操作,而后对新的URL启动新一轮重写检查,不建议在location中使用
    break:
      重写完成后停止对当前URL在当前location中后续的其它重写操作,而后直接跳转至重写规则配置块之后的其它配置;结束循环,建议在location中使用

  其中前两种是跳转型的flag,后两种是代理型,跳转型是指有客户端浏览器重新对新地址进行请求,代理型是在WEB服务器内部实现跳转的。"Syntax: rewrite regex replacement [flag];"通过正则表达式处理用户请求并返回替换后的数据包。
 
  博主推荐阅读:
    https://www.cnblogs.com/yinzhengjie/p/12064108.html
原文地址:https://www.cnblogs.com/yinzhengjie/p/12063728.html