第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > Nginx--------地址重写

Nginx--------地址重写

时间:2021-12-18 23:33:55

相关推荐

Nginx--------地址重写

修改配置文件(访问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的请求重定向至)

1) 修改Nginx服务配置

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf.. ..server {listen 80;server_name localhost;rewrite ^/ /;location / {root html;index index.html index.htm;# rewrite /a.html /b.html redirect;} }

修改配置文件(访问192.168.4.5/下面子页面,重定向至/下相同的页面)

1) 修改Nginx服务配置

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf.. ..server {listen 80;server_name localhost;rewrite ^/(.*)$ /$1;location / {root html; index index.html index.htm;# rewrite /a.html /b.html redirect; }}

01.[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf02.03... ..04.server {05. listen 80;06. server_name localhost;07.rewrite ^/(.*)$ /$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.conf02... ..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 永久重定向

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。