nginx地址重写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。例如当用户访问tianyun.com的80端口时,将其跳转到443端口。

1、Rewrite 相关指令

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

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

1.2 Rewrite flag

rewrite 指令根据表达式来重定向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更好的话,也许地址栏不会更改。

本地解析host文件(windows)
10.8.156.11 www.oneice.com

例1

[root@nginx1 conf.d]# vim real.conf
server {
    listen       80;
    server_name  www.oneice.com;

        location /a {
        root /home/www/html/;
        index   1.html index.htm;
        rewrite .* /b/2.html permanent;
        }

        location /b {
        root    /home/www/html/;
        index   2.html index.htm;
        }
}
[root@nginx1 conf.d]# mkdir /home/www/html/{a,b}
[root@nginx1 conf.d]# echo "hello a" > /home/www/html/a/1.html
[root@nginx1 conf.d]# echo "hello b" > /home/www/html/b/2.html
[root@nginx1 conf.d]# nginx -s reload

浏览器访问http://www.oneice.com/a/1.html

image.png

地址会重写为http://www.oneice.com/b/2.html

例2

#http://www.oneice.com/2019/a/1.html ==> http://www.oneice.com/2020/a/1.html

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

        location /2019/a {
        root /home/www/html/;
        index    1.html;
        rewrite  ^/2019/(.*)$ /2020/$1 permanenet;
        }

        location /2020/a {
        root    /home/www/html/;
        index   1.html;
        }
}
[root@nginx1 conf.d]# mkdir /home/www/html/{2019,2020}
[root@nginx1 conf.d]# mkdir /home/www/html/2019/a
[root@nginx1 conf.d]# mkdir /home/www/html/2020/a
[root@nginx1 conf.d]# echo "hello 2020" > /home/www/html/2020/a/1.html
[root@nginx1 conf.d]# echo "hello 2019" > /home/www/html/2019/a/1.html
[root@nginx1 conf.d]# nginx -s reload

浏览器访问http://www.oneice.com/2019/a/1.html

image.png

地址会重写为http://www.oneice.com/2020/a/1.html

例3

#http://www.oneice.com/a/1.html ==> http://jd.com
server {
    listen       80;
    server_name  www.oneice.com;

        location /2019/a {
                root /home/www/html/;
                if ( $host ~* oneice.com ) {
                rewrite .* http://jd.com permanent;
                }
        }        
}   
[root@nginx1 conf.d]# nginx -s reload

浏览器访问http://www.oneice.com/2019/a/1.html

image.png

会跳转到京东

例4:

# http://www.oneice.com/a/1.html ==> http://jd.com/a/1.html
location /a {
        root /html;
        if ( $host ~* oneice.com ){
        rewrite .* http://jd.com$request_uri permanent;
        }
}
[root@nginx1 conf.d]# nginx -s reload

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

# http://www.qf.com/a/b/c
# $1: /a/b/
# $2: c
# http://$host$1$2/
[root@nginx1 conf.d]# vim real.conf
server {
    listen       80;
    server_name  www.oneice.com;

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

}
[root@nginx1 conf.d]# mkdir -p /home/www/html/a/b/c 
[root@nginx1 conf.d]# echo "hello c" > /home/www/html/a/b/c/index.html
[root@nginx1 conf.d]# nginx -s reload

浏览器访问http://www.oneice.com/a/b/c

image.png

1.3 set指令

set 指令是用于定义一个变量,并且赋值
应用环境:server,location,if

例1:

#http://alice.oneice.com ==> http://www.oneice.com/alice
#http://jack.oneice.com ==> http://www.oneice.com/jack
#本地解析域名10.8.156.11   www.oneice.com   alice.oneice.com   jack.oneice.com
[root@nginx1 conf.d]# vim real.conf
server {
    listen       80;
    server_name  www.oneice.com;

        location / {
                root /home/www/html/;
                index index.html;
                if ( $host ~* ^www.oneice.com$ ) {
                        break;
                }       
                if ( $host ~* "^(.*)\.oneice\.com$" ) {
                        set $user $1;
                        rewrite .* http://www.oneice.com/$user permanent;
                }       
        }
        location /jack {
                root /home/www/html;
                index index.html;
        }
        location /alice {
                root /home/www/html;
                index index.html;
        }
}
[root@nginx1 conf.d]# cd /home/www/html/
[root@nginx1 html]# echo "hello www" > index.html
[root@nginx1 html]# mkdir {jack,alice}
[root@nginx1 html]# echo "hello alice" > alice/index.html
[root@nginx1 html]# echo "hello jack" > jack/index.html
[root@nginx1 conf.d]# nginx -s reload

浏览器访问www.oneice.com

image.png

浏览器访问alice.oneice.com
image.png

浏览器访问jack.oneice.com
image.png

1.4 return 指令

return 指令用于返回状态码给客户端
应用环境:server,location,if

例1

如果访问的.sh结尾的文件则返回403操作拒绝错误
比如访问http://www.oneice.com/1.sh 返回403

[root@nginx1 conf.d]# vim real.conf 
server {
    listen       80;
    server_name  www.oneice.com;

        location / {
                root /home/www/html/;
                index index.html;
        }
        location ~* \.sh$ {
                return 403;
        }
}
[root@nginx1 conf.d]# nginx -s reload

浏览器访问www.oneice.com/1.sh

image.png

例2:80转443端口

做本地解析:10.8.156.11 520.jingfei.xyz

[root@nginx1 conf.d]# vim real.conf
server {
    listen       80;
    server_name  520.jingfei.xyz;
    access_log  /var/log/nginx/http_access.log  main;
    return 301 https://520.jingfei.xyz$request_uri;
}

server {
    listen 443 ssl;
    server_name 520.jingfei.xyz;
    access_log  /var/log/nginx/https_access.log  main;

    #ssl on;
    ssl_certificate   /home/cert/3592798_520.jingfei.xyz.pem;
    ssl_certificate_key  /home/cert/3592798_520.jingfei.xyz.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  /home/www/html;
        index index.html index.htm;
    }
}
[root@nginx1 conf.d]# nginx -s reload

浏览器访问http://520.jingfei.xyz

image.png

你可能感兴趣的:(nginx地址重写rewrite)