生活随笔
收集整理的這篇文章主要介紹了
Nginx--------地址重写
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?
修改配置文件(訪問a.html重定向到b.html)
1)修改Nginx服務(wù)配置:
[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(跳轉(zhuǎn)地址欄)
1)修改Nginx服務(wù)配置:
[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的請(qǐng)求重定向至www.caizhan99.cn)
1) 修改Nginx服務(wù)配置
[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服務(wù)配置
[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.}
修改配置文件(實(shí)現(xiàn)curl和火狐訪問相同鏈接返回的頁面不同)
1) 創(chuàng)建網(wǎng)頁目錄以及對(duì)應(yīng)的頁面文件:
[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服務(wù)配置
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf.. ..server {? ? ? ? ? listen 80;? ? ? ? ? server_name localhost;location / {????root html;? ? index index.html index.htm;? ? ?}#這里,~符號(hào)代表正則匹配,*符號(hào)代表不區(qū)分大小寫? ? ? if ($http_user_agent ~* firefox) {????????????//識(shí)別客戶端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.#這里,~符號(hào)代表正則匹配,*符號(hào)代表不區(qū)分大小寫
11.if ($http_user_agent ~* firefox) { //識(shí)別客戶端firefox瀏覽器
12.rewrite ^(.*)$ /firefox/$1;
13.}
14.}
地址重寫格式【總結(jié)】
rewrite 舊地址 新地址 [選項(xiàng)];
last 不再讀其他rewrite
break 不再讀其他語句,結(jié)束請(qǐng)求
redirect 臨時(shí)重定向
permament 永久重定向
?
總結(jié)
以上是生活随笔為你收集整理的Nginx--------地址重写的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。