Nginx高级技术:地址重写

一、概念:

(1)什么是地址重写:获得一个来访的URL请求,然后改成服务器可以处理的另一个URL的过程。

(2)地址重写的好处:

1.缩短URL,隐藏实际路径提高安全性
2.易于用户记忆和键入

3.易于被搜索引擎收录

(3)rewrite语法:
rewrite基本语句:
-rewrite regex replacement flag
-if (条件){...}

(4)rewrite选项
-rewrite regex replacement flag
-flag:baeak、last、redirect、permanent
-last::停止执行其他重写规则,根据URL继续搜索其他的location,地址栏不改变

-break:停止执行其他的重写规则,完成本次请求
-redirect:302临时重定向,地址栏改变,爬虫不更新URL
-permanent:301永久重定向,地址栏改变,爬虫更新URL

二、案例
所有访问a.html的请求,重定向到b.html;

所有访问192.168.4.5的请求重定向至www.baidu.cn;

所有访问192.168.4.5/下面子页面,重定向至www.baidu.cn/下相同的页面;

实现firefox与curl访问相同页面文件,返回不同的内容。

(一)步骤:

1.首先安装安装好nginx服务:https://editor.csdn.net/md/?articleId=1043002122.修改Nginx服务配置:

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
        listen       80;
        server_name  localhost;
rewrite /a.html  /b.html;            
location / {
 	   root   html;
index  index.html index.htm;
}
}
[root@proxy ~]# echo "BB" > /usr/local/nginx/html/b.html

3.重新加载配置文件

[root@proxy ~]# /usr/local/nginx/sbin/nginx  -s  reload

4.客户端测试

[root@client ~]# firefox  http://192.168.4.5/a.html

(二)访问a.html重定向到b.html(跳转地址栏)
1)修改Nginx服务配置:

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
  		     listen       80;
  		     server_name  localhost;
rewrite /a.html  /b.html  redirect;            
location / {
  	  	root   html;
index  index.html index.htm;
}
}


2)重新加载配置文件

[root@proxy ~]# /usr/local/nginx/sbin/nginx  -s  reload

3)客户端测试(仔细观察浏览器地址栏的变化)

[root@client ~]# firefox  http://192.168.4.5/a.html

(三)修改配置文件(访问192.168.4.5的请求重定向至www.baidu.com)

1) 修改Nginx服务配置

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
        listen       80;
        server_name  localhost;
rewrite ^/  http://www.baidu.com/;
location / {
    root   html;
index  index.html index.htm;
# rewrite /a.html  /b.html  redirect;
}
}

2)重新加载配置文件

[root@proxy ~]# /usr/local/nginx/sbin/nginx  -s  reload

3)客户端测试(真实机测试,真实机才可以连接baidu)


(四)修改配置文件(访问192.168.4.5/下面子页面,重定向至www.baidu.com/下相同的页面)

1) 修改Nginx服务配置

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
        listen       80;
        server_name  localhost;
rewrite ^/(.*)$  http://www。baidu.com/$1;
location / {
    root   html;
index  index.html index.htm;
}
}

2)重新加载配置文件

[root@proxy ~]# /usr/local/nginx/sbin/nginx  -s  reload

3)客户端测试(真实机测试,真实机才可以连接baidu)

[root@room9pc01 ~]# firefox  http://192.168.4.5
[root@room9pc01 ~]# firefox  http://192.168.4.5/test

(五) 修改配置文件(实现curl和火狐访问相同链接返回的页面不同)

1) 创建网页目录以及对应的页面文件:

[root@proxy ~]# echo "I am Normal page" > /usr/local/nginx/html/test.html
[root@proxy ~]# mkdir  -p  /usr/local/nginx/html/firefox/
[root@proxy ~]# echo "firefox page" > /usr/local/nginx/html/firefox/test.html

2) 修改Nginx服务配置

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
        listen       80;
        server_name  localhost;
location / {
    root   html;
index  index.html index.htm;
}
#这里,~符号代表正则匹配,*符号代表不区分大小写
if ($http_user_agent ~* firefox) {          		#识别客户端firefox浏览器
rewrite ^(.*)$  /firefox/$1;
}
}

3)重新加载配置文件

[root@proxy ~]# /usr/local/nginx/sbin/nginx  -s  reload

4)客户端测试

[root@client ~]# firefox  http://192.168.4.5/test.html
[root@client ~]# curl     http://192.168.4.5/test.html