版权声明:观极著作 https://blog.csdn.net/weixin_41619143/article/details/88432276
修改配置文件(访问a.html重定向到b.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;
- rewrite /a.html /b.html;
- }
- }
- [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服务配置:
- [root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
- .. ..
- server {
- listen 80;
- server_name localhost;
- location / {
- root html;
- index index.html index.htm;
- rewrite /a.html /b.html redirect;
- }
- }
修改配置文件(访问192.168.4.5的请求重定向至www.caizhan99.cn)
1) 修改Nginx服务配置
- [root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
- .. ..
- server {
- listen 80;
- server_name localhost;
- rewrite ^/ http://www.caizhan99.cn/;
- location / {
- root html;
- index index.html index.htm;
- # rewrite /a.html /b.html redirect;
- }
- }
修改配置文件(访问192.168.4.5/下面子页面,重定向至www.caizhan99.cn/下相同的页面)
1) 修改Nginx服务配置
- [root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
- .. ..
- server {
- listen 80;
- server_name localhost;
- rewrite ^/(.*)$ http://www.caizhan99.cn/$1;
- location / {
- root html;
- index index.html index.htm;
- # rewrite /a.html /b.html redirect;
- }
- }
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服务配置
- [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;
- }
- }
~]# 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 永久重定向