zoukankan      html  css  js  c++  java
  • 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服务器

    结束.

  • 相关阅读:
    沁恒 CH559 芯片入门指南
    雕刻机制作 PCB 指南
    我的 FPGA 学习历程(14)—— PWM 脉冲宽度调制
    我的 FPGA 学习历程(01)—— FPGA 基础知识和 Quartus 的安装
    我的 FPGA 学习历程(13)—— 电子钟项目
    我的 FPGA 学习历程(12)—— 电子钟项目准备
    我的 FPGA 学习历程(11)—— 实验:按键消抖
    我的 FPGA 学习历程(10)—— 实验:数码管驱动
    我的 FPGA 学习历程(09)—— 时序逻辑入门
    我的 FPGA 学习历程(08)—— 实验:点亮单个数码管
  • 原文地址:https://www.cnblogs.com/liusingbon/p/11083076.html
Copyright © 2011-2022 走看看