Nginx--------地址重写

版权声明:观极著作 https://blog.csdn.net/weixin_41619143/article/details/88432276

修改配置文件(访问a.html重定向到b.html)

1)修改Nginx服务配置:

  1. [root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
  2. .. ..
  3. server {
  4.            listen 80;
  5.           server_name localhost;
  6. location / {
  7.            root html;
  8.            index index.html index.htm;
  9.            rewrite /a.html /b.html;            
  10.          }
  11.   }
  12.  
  13. [root@proxy ~]# echo "BB" > /usr/local/nginx/html/b.html
# /usr/local/nginx/sbin/nginx -s reload
# firefox http://192.168.4.5/a.html

访问a.html重定向到b.html(跳转地址栏)

1)修改Nginx服务配置:

  1. [root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
  2. .. ..
  3. server {
  4.          listen 80;
  5.          server_name localhost;
  6. location / {
  7.                 root html;
  8.                 index index.html index.htm;
  9.                rewrite /a.html /b.html redirect;            
  10.              }
  11.   }

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

1) 修改Nginx服务配置

  1. [root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
  2. .. ..
  3. server {
  4.             listen 80;
  5.            server_name localhost;
  6.             rewrite ^/ http://www.caizhan99.cn/;
  7. location / {
  8.             root html;
  9.            index index.html index.htm;
  10. # rewrite /a.html /b.html redirect;
  11.              }
  12.     }

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

1) 修改Nginx服务配置

  1. [root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
  2.  
  3. .. ..
  4. server {
  5. listen 80;
  6. server_name localhost;
  7. rewrite ^/(.*)$ http://www.caizhan99.cn/$1;
  8. location / {
  9.     root html;
  10.     index index.html index.htm;
  11. # rewrite /a.html /b.html redirect;
  12.        }
  13. }


01.[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
02.
03... ..
04.server {
05.        listen       80;
06.        server_name  localhost;
07.rewrite ^/(.*)$ http://www.caizhan99.cn/$1;
08.location / {
09.    root   html;
10.    index  index.html index.htm;
11.# rewrite /a.html  /b.html  redirect;
12.}
13.}

修改配置文件(实现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服务配置

  1. [root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
  2. .. ..
  3. server {
  4.           listen 80;
  5.           server_name localhost;
  6. location / {
  7.     root html;
  8.     index index.html index.htm;
  9.      }
  10. #这里,~符号代表正则匹配,*符号代表不区分大小写
  11.       if ($http_user_agent ~* firefox) {            //识别客户端firefox浏览器
  12.       rewrite ^(.*)$ /firefox/$1;
  13.      }
  14.   }
~]# vim /usr/local/nginx/conf/nginx.conf
02... ..
03.server {
04.        listen       80;
05.        server_name  localhost;
06.location / {
07.    root   html;
08.index  index.html index.htm;
09.}
10.#这里,~符号代表正则匹配,*符号代表不区分大小写
11.if ($http_user_agent ~* firefox) {            //识别客户端firefox浏览器
12.rewrite ^(.*)$ /firefox/$1;
13.}
14.}

地址重写格式【总结】

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

last 不再读其他rewrite

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

redirect 临时重定向

permament 永久重定向

猜你喜欢

转载自blog.csdn.net/weixin_41619143/article/details/88432276