Nginx-地址重写-rewrite

#一、什么是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对于追求完美主义的网站设计师,就算是网页的地址也希望看起来尽量简洁明快。理论上,搜索引擎更喜欢静态页面形式的网页,搜索引擎对静态页面的评分一般要高于动态页面。所以,UrlRewrite可以让我们网站的网页更容易被搜索引擎所收录。

  • 从安全角度上讲,如果在URL中暴露太多的参数,无疑会造成一定量的信息泄漏,可能会被一些黑客利用,对你的系统造成一定的破坏,所以静态化的URL地址可以给我们带来更高的安全性。

  • 实现网站地址跳转,例如用户访问360buy.com,将其跳转到jd.com。例如当用户访问test.com的 80 端口时,将其跳转到 443 端口。

#二、Rewrite 相关指令
Nginx Rewrite 相关指令有 ifrewritesetreturn

###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. Rewrite flag
rewrite指令根据表达式来重定向URI,或者修改字符串。可以应用于server,location,if环境下,每行 rewrite 指令最后跟一个 flag标记,支持的 flag标记有:

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

redirectpermanent区别则是返回的不同方式的重定向,对于客户端来说一般状态下是没有区别的。而对于搜索引擎,相对来说301的重定向更加友好,如果我们把一个地址采用301跳转方式跳转的话,搜索引擎会把老地址的相关信息带到新地址,同时在搜索引擎索引库中彻底废弃掉原先的老地址。使用302重定向时,搜索引擎(特别是google)有时会查看跳转前后哪个网址更直观,然后决定显示哪个,如果它觉的跳转前的URL更好的话,也许地址栏不会更改。

###2.1 Rewrite匹配参考示例
本地解析host文件

在:C:\Windows\System32\drivers\etc\host
添加:192.168.181.128 www.test.com

#####示例1:
实现访问 www.test.com/a/1.html 跳转到 www.test.com/b/2.html

[root@localhost html]# pwd
/usr/share/nginx/html

[root@localhost html]# cat a/1.html 
test

[root@localhost html]# cat b/2.html 
tiaozhuan chenggong

# 修改配置文件
[root@localhost conf.d]# cat default.conf
server {
    listen       80;
    server_name  www.test.com;
    location /a {
        root   /usr/share/nginx/html;
        index  1.html;
        rewrite .* /b/2.html permanent;
    }

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

}

#####示例2:
实现访问www.test.com/2020/a/1.html 跳转到 www.test.com/2018/a/1.html

[root@localhost html]# pwd
/usr/share/nginx/html

[root@localhost html]# cat 2018/a/1.html 
2018

[root@localhost html]# cat 2020/a/1.html 
2020

# 修改配置文件
[root@localhost conf.d]# cat default.conf 
server {
    listen       80;
    server_name  www.test.com;
    location /2020/a {
        root   /usr/share/nginx/html;
        index  1.html;
        rewrite ^/2020/(.*)$ /2018/$1 permanent;
    }

    location /2018/a {
        root /usr/share/nginx/html;
        index 1.html;
    }

}

#####示例3:
实现访问 www.test.com/a/1.html 跳转到 jd.com

# 修改配置文件
[root@localhost conf.d]# cat default.conf 
server {
    listen       80;
    server_name  www.test.com;
    location /a {
        root   /usr/share/nginx/html;
        if ($host ~* test.com) {
            rewrite .* jd.com permanent;
        }

    }

}

#####示例4:
实现访问 www.test.com/a/1.html 跳转到 jd.com/a/1.html

# 修改配置文件
[root@localhost conf.d]# cat default.conf
server {
    listen 80;
    server_name www.test.com;

    location /a {
        root /usr/share/nginx/html;
        if ( $host ~* test.com ){
            rewrite .* http://jd.com$request_uri permanent;
        }

    }

}

#####示例5:
在访问目录后添加/ (如果目录后已有/,则不加/)

[root@localhost c]# pwd
/usr/share/nginx/html/a/b/c

# 修改配置文件
[root@localhost conf.d]# cat default.conf
server {
    listen 80;
    server_name www.test.com;

    location /a/b/c {
        root /usr/share/nginx/html;
        if (-d $request_filename) {
            rewrite ^(.*)([^/])$ http://$host$1$2/ permanent;
        }

    }

}
# $host: www.test.com 
# $1: /a/b/    
# $2: c

#####示例6:
访问www.test.com/login/test.html 跳转到 www.test.com/reg/login.html?user=test

[root@localhost reg]# pwd 
/usr/share/nginx/html/reg

[root@localhost reg]# cat login.html 
login

# 修改配置文件
[root@localhost conf.d]# cat default.conf
server {
    listen 80;
    server_name www.test.com;

    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:
访问 www.test.com/test/11-22-33/1.html 跳转到 www.test.com/test/11/22/33/1.html

[root@localhost 33]# pwd
/usr/share/nginx/html/test/11/22/33

[root@localhost 33]# cat 1.html 
hello nginx

# 修改配置文件
[root@localhost conf.d]# cat default.conf
server {
    listen 80;
    server_name www.test.com;

    location /test {
        rewrite ^/test/([0-9]+)-([0-9]+)-([0-9]+)(.*)$ /test/$1/$2/$3$4 permanent;
    }

    location /test/11/22/33 {
        root /usr/share/nginx/html;
        index 1.html;
    }

}

###3. set 指令
set 指令是用于定义一个变量,并且赋值

应用环境:
server, location, if

#####应用示例:
访问 alice.test.com 跳转到 www.test.com/alice
访问 jack.test.com 跳转到 www.test.com/jack

# 本地解析host文件
在:C:\Windows\System32\drivers\etc\host
添加:192.168.181.128 www.test.com alice.test.com jack.test.com

# 
[root@localhost html]# pwd
/usr/share/nginx/html

[root@localhost html]# cat jack/index.html 
jack

[root@localhost html]# cat alice/index.html 
alice

# 修改配置文件
[root@localhost conf.d]# cat default.conf
server {
    listen 80;
    server_name www.test.com;

    location / {
        root /usr/share/nginx/html;
        index index.html index.htm;
        if ( $host ~* ^www.test.com$ ){
            break;
        }

        if ( $host ~* ^(.*)\.test\.com ){
            set $user $1;
            rewrite .* http://www.test.com/$user permanent;
        }

    }

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

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

}

###4. return 指令
return 指令用于返回状态码给客户端

应用环境:
server, location, if

#####示例1:
如果访问的 .sh 结尾的文件则返回 403 操作拒绝错误

# 修改配置文件
[root@localhost conf.d]# cat default.conf
server {
    listen 80;
    server_name www.test.com;

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

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

}

#####示例1:
80转443端口

# 修改配置文件
server {
    listen       80;
    server_name  www.test.com;
    access_log  /var/log/nginx/http_access.log  main;
    return 301 https://www.test.com$request_uri;
}

server {
    listen 443 ssl;
    server_name www.test.com;
    access_log  /var/log/nginx/https_access.log  main;

    #ssl on;
    ssl_certificate   /etc/nginx/cert/2447549_www.test.com.pem;
    ssl_certificate_key  /etc/nginx/cert/2447549_www.test.com.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
    ssl_prefer_server_ciphers on;

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

[root@nginx-server ~]# curl -I http://www.test.com
HTTP/1.1 301 Moved Permanently
Server: nginx/1.16.0
Date: Wed, 03 Jul 2019 13:52:30 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: https://www.test.com/

#三、last break 详解

# 
[root@localhost conf.d]# cd /usr/share/nginx/html/
[root@localhost html]# mkdir test
[root@localhost html]# echo "last" > test/last.html
[root@localhost html]# echo "break" > test/break.html
[root@localhost html]# echo "test" > test/test.html

# 修改配置文件
[root@localhost conf.d]# cat default.conf
server {
    listen       80;
    server_name  localhost;
    access_log  /var/log/nginx/last.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    
    location /break {
        root /usr/share/nginx/html;
        rewrite .* /test/break.html break;
    }
    
    location /last {
        root /usr/share/nginx/html;
        rewrite .* /test/last.html last;
    }

    location /test {
        root /usr/share/nginx/html;
        rewrite .* /test/test.html break;
    }

}

注意:

  • last标记在本条 rewrite 规则执行完后,会对其所在的 server { … } 标签重新发起请求;
  • break标记则在本条规则匹配完成后,停止匹配,不再做后续的匹配;

#四、Nginx 的 https ( rewrite )

server {
        listen       80;
        server_name  *.vip9999.top vip9999.top;

        if ($host ~* "^www.vip9999.top$|^vip9999.top$" ) {
                return 301 https://www.vip9999.top$request_uri;
        }

        if ($host ~* "^(.*).vip9999.top$" ) {
                set $user $1;
                return 301 https://www.vip9999.top/$user;
        }

    }

    # Settings for a TLS enabled server.
    server {
        listen       443 ssl;
        server_name  www.vip9999.top;

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

        #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
            root           /usr/share/nginx/html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
        ssl on;
        ssl_certificate cert/214025315060640.pem;
        ssl_certificate_key cert/214025315060640.key;
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;
        }

#五、Apache 的 https ( rewrite )拓展

[root@localhost ~]# yum -y install httpd mod_ssl
[root@localhost ~]# vim /etc/httpd/conf.d/vip9999.conf

image.png


# Nginx 的 location 指令详解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值