通过调整Nginx服务端.实现地址重写

步骤:
1.修改Nginx配置文件将a.html的请求重定向到b.html 主机IP为172.25.0.11/24
[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;
}
}
[root@proxy ~]# echo “BB” > /usr/local/nginx/html/b.html
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

客户验证访问: [root@client ~]# firefox http://172.25.0.11/a.html

2.修改Nginx配置文件将172.25.0.11的请求重定向到www.test.com

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
… …
server {
listen 80;
server_name localhost;
rewrite ^/ http://www.test.com/;
location / {
root html;
index index.html index.htm;
}
}
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

客户验证访问: [root@client ~]# firefox http://172.25.0.11

3.修改Nginx配置文件将172.25.0.11/子页面,重定向至www.test.com/下相同的子页面
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
… …
server {
listen 80;
server_name localhost;
rewrite ^/(.*)$ http://www.test.com/$1;
location / {
root html;
index index.html index.htm;
}
}
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

4.修改Nginx配置文件实现手机和PC访问相同链接返回的页面不同(尺寸不同)
[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
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
… …
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
#这里,~符号代表正则匹配,*符号代表不区分大小写
if (KaTeX parse error: Expected '}', got 'EOF' at end of input: …器 rewrite ^(.*) /firefox/$1;
}
}
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

5.rewrite命令整理
rewrite 旧地址 新地址 [选项];
last 不再读其他rewrite
break 不再读其他语句,结束请求
redirect 临时重定向
permament 永久重定向

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值