LINUX 地址重写

匿名 (未验证) 提交于 2019-12-02 21:56:30

3.1 问题
沿用练习二,通过调整Nginx服务端配置,实现以下目标:
1.所有访问a.html的请求,重定向到b.html;
2.所有访问192.168.4.5的请求重定向至www.tmooc.cn
3.所有访问192.168.4.5/下面子页面,重定向至www.tmooc.cn/下相同的页面;
4.实现firefox与curl访问相同页面文件,返回不同的内容。
3.2 方案
关于Nginx服务器的地址重写,主要用到的配置参数是rewrite:
rewrite regex replacement flag
rewrite 旧地址 新地址 [选项]
3.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:修改配置文件(访问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 

2)重新加载配置文件

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

3)客户端测试

1.[root@client ~]# 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.} 

2)重新加载配置文件

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

3)客户端测试(仔细观察浏览器地址栏的变化)
1.[root@client ~]# firefox http://192.168.4.5/a.html
步骤三:修改配置文件(访问192.168.4.5的请求重定向至www.tmooc.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.tmooc.cn/;
    7.location / {
    8. root html;
    9.index index.html index.htm;
    10.# rewrite /a.html /b.html redirect;
    11.}
    12.}

2)重新加载配置文件

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

3)客户端测试(真实机测试,真实机才可以连接tmooc)
1.[root@room9pc01 ~]# firefox http://192.168.4.5
步骤四:修改配置文件(访问192.168.4.5/下面子页面,重定向至www.tmooc.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.tmooc.cn/$1;
    8.location / {
    9. root html;
    10.index index.html index.htm;
    11.# rewrite /a.html /b.html redirect;
    12.}
    13.}

2)重新加载配置文件

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

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

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

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

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

    1.[root@proxy ~]# echo “I am Normal page” > /usr/local/nginx/html/test.html
    2.
    3.[root@proxy ~]# mkdir -p /usr/local/nginx/html/firefox/
    4.[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 (KaTeX parse error: Expected '}', got 'EOF' at end of input: …2.rewrite ^(.*) /firefox/$1;
    13.}
    14.}

3)重新加载配置文件

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

4)客户端测试

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

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

文章来源: https://blog.csdn.net/LYJ_man/article/details/90522077
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!