Nginx服务的地址重写

调整Nginx服务器配置,实现:

1.所有访问a.html的请求,重定向到b.html;

2.所有访问Nginx服务器(192.168.4.1)的请求重定向至www.baidu.com;

3.所有访问Nginx服务器(192.168.4.1)/下面子页面,重定向至www.baidu.com/下相同的页面.

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

总结地址重写的格式有:

rewrite 旧地址 新地址 [选项];

last 不再读其他rewrite

break 不再读其他语句,结束请求

redirect 临时重定向

permament 永久重定向

1. 所有访问a.html的请求,重定向到b.html

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf

......

    server {

               listen 80;

               server_name localhost;

               rewrite /a.html /b.html redirect;    //地址跳转到b.html         

               location / {

                              root html;

                              index index.html index.htm;

                            }

               }

# echo "test page" > /usr/local/nginx/html/b.html   //在网页文件里写入数据,用于测试

# /usr/local/nginx/sbin/nginx -s reload   //重启,加载配置

[root@client ~]# firefox http://192.168.4.5/a.html    //客户端测试,访问a.html,但浏览器地址栏会自动跳转到b.html的页面

2.所有访问Nginx服务器(192.168.4.1)的请求重定向至www.baidu.com

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf

......

    server {

               listen 80;

               server_name localhost;

               rewrite  ^/  http://baidu.com/ ;    //地址跳转到百度        

               location / {

                              root html;

                              index index.html index.htm;

                            }

               }

# /usr/local/nginx/sbin/nginx -s reload   //重启,加载配置

[root@room9pc01 ~]# firefox http://192.168.4.1   //真机上访问Nginx服务器,浏览器地址栏自动跳转至www.baidu.com

3.所有访问Nginx服务器(192.168.4.1)/下面子页面,重定向至www.baidu.com/下相同的页面

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf

......

    server {

               listen 80;

               server_name localhost;

               rewrite  ^/(.*)$  http://baidu.com/ $1;    //地址跳转到百度下的子页面,这里用到了正则,即以/开头,以任意内容结尾(.*)$        

               location / {

                              root html;

                              index index.html index.htm;

                            }

               }

# /usr/local/nginx/sbin/nginx -s reload   //重启,加载配置

[root@room9pc01 ~]# firefox http://192.168.4.1/test   //真机上访问Nginx服务器下的子页面,浏览器地址栏自动跳转至www.baidu.com/子页面

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

4.1创建网页目录以及对应的页面文件,用于测试:

# echo "I am normal page" > /usr/local/nginx/html/test.html

# mkdir -p /usr/local/nginx/html/firefox/

# echo "wo shi firefox page" > /usr/local/nginx/html/firefox/test.html

4.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) {    //变量$http_user_agent能识别客户端的firefox浏览器,这里的符号~示意正则匹配,符号*示意不区分大小写

                                                              rewrite ^/(.*)/firefox/$1;    

                                                             }

               }

# /usr/local/nginx/sbin/nginx -s reload   //重启,加载配置

# firefox http://192.168.4.1/test.html     //使用火狐浏览器访问Nginx服务器

# curl http://192.168.4.1/test.html    //本地curl访问Nginx服务器

结束.

posted @ 2019-06-25 16:07  巴州夜雨  阅读(3894)  评论(0编辑  收藏  举报