nginx-地址重写

案例:地址重写
  • 1 问题

沿用练习二,通过调整Nginx服务端配置,实现以下目标:
所有访问a.html的请求,重定向到b.html;
所有访问192.168.4.5的请求重定向至www.tmooc.cn;
所有访问192.168.4.5/下面子页面,重定向至www.tmooc.cn/下相同的页面;
实现firefox与curl访问相同页面文件,返回不同的内容。

  • 2 方案

关于Nginx服务器的地址重写,主要用到的配置参数是rewrite:
rewrite regex replacement flag
rewrite 旧地址 新地址 [选项]

  • 案例要求:
    访问http://www.360buy.com会自动跳转至http://www.jd.com。
    访问http://www.baidu.com会自动跳转至https://www.baidu.com。
    3 步骤

实现此案例需要按照如下步骤进行。
步骤一:修改配置文件(访问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;            
location / {
    
    
    root   html;
index  index.html index.htm;
}
}
[root@proxy ~]# echo "BB" > /usr/local/nginx/html/b.html

2)重新加载配置文件

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

3)客户端测试

[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
#请先确保nginx是启动状态,否则运行该命令会报错,报错信息如下:
#[error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)

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

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

步骤三:修改配置文件(访问192.168.4.5的请求重定向至www.tmooc.cn)

  1. 修改Nginx服务配置
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
    
    
        listen       80;
        server_name  localhost;
rewrite ^/  http://www.tmooc.cn/;
location / {
    
    
    root   html;
index  index.html index.htm;
# rewrite /a.html  /b.html  redirect;
}
}

2)重新加载配置文件

[root@proxy ~]# /usr/local/nginx/sbin/nginx  -s  reload
#请先确保nginx是启动状态,否则运行该命令会报错,报错信息如下:
#[error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)

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

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

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

  1. 修改Nginx服务配置
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
    
    
        listen       80;
        server_name  localhost;
rewrite ^/(.*)$  http://www.tmooc.cn/$1;
location / {
    
    
    root   html;
index  index.html index.htm;
}
}

2)重新加载配置文件

[root@proxy ~]# /usr/local/nginx/sbin/nginx  -s  reload
#请先确保nginx是启动状态,否则运行该命令会报错,报错信息如下:
#[error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)

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

[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
  1. 修改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
#请先确保nginx是启动状态,否则运行该命令会报错,报错信息如下:
#[error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)

4)客户端测试

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

5)地址重写格式【总结】
rewrite 旧地址 新地址 [选项];
last 不再读其他rewrite
break 不再读其他语句,结束请求
redirect 临时重定向
permanent 永久重定向

猜你喜欢

转载自blog.csdn.net/weixin_45942735/article/details/104561413