步骤:
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 永久重定向