詳解nginx配置url重定向-反向代理


https://www.jb51.net/article/99996.htm

 

本文系統:Centos6.5_x64

三台主機:nginx主機,hostname: master.lansgg.com  IP: 192.168.10.128   
          apache主機,hostname: client1.lansgg.com IP:  192.168.10.129

一、nginx 地址重定向

二、nginx 反向代理

1、地址重定向:是指當使用者瀏覽某個網址時,將他導向到另一個網址的技術。常用在把一串很長的網址,轉成較短的網址。因為當要傳播某網站時,常常因為網址太長,不好記憶;又有可能因為換了網路的免費網頁空間,網址又必須要變更,不知情的使用者還以為網站關閉了。這時就可以用網路上的轉址了。這個技術使一個網頁是可借由不同的統一資源定位符(URL)連結。

1.1、這 個模塊允許使用正則表達式重寫URI(需PCRE庫),並且可以根據相關變量重定向和選擇不同的配置。如果這個指令在server字段中指定,那么將在被 請求的location確定之前執行,如果在指令執行后所選擇的location中有其他的重寫規則,那么它們也被執行。如果在location中執行這 個指令產生了新的URI,那么location又一次確定了新的URI。這樣的循環可以最多執行10次,超過以后nginx將返回500錯誤

正則表達式匹配,其中:

* ~ 為區分大小寫匹配

* ~* 為不區分大小寫匹配

* !~和!~*分別為區分大小寫不匹配及不區分大小寫不匹配

文件及目錄匹配,其中:

* -f和!-f用來判斷是否存在文件

* -d和!-d用來判斷是否存在目錄

* -e和!-e用來判斷是否存在文件或目錄

* -x和!-x用來判斷文件是否可執行

flag標記有:
* last 相當於Apache里的[L]標記,表示完成rewrite

* break 終止匹配, 不再匹配后面的規則

* redirect 返回302臨時重定向 地址欄會顯示跳轉后的地址

* permanent 返回301永久重定向 地址欄會顯示跳轉后的地址

一些可用的全局變量有,可以用做條件判斷

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$args, 請求中的參數;
$content_length, HTTP請求信息里的 "Content-Length" ;
$content_type, 請求信息里的 "Content-Type" ;
$document_root, 針對當前請求的根路徑設置值;
$document_uri, 與$uri相同;
$host, 請求信息中的 "Host" ,如果請求中沒有Host行,則等於設置的服務器名;
$limit_rate, 對連接速率的限制;
$request_method, 請求的方法,比如 "GET" "POST" 等;
$remote_addr, 客戶端地址;
$remote_port, 客戶端端口號;
$remote_user, 客戶端用戶名,認證用;
$request_filename, 當前請求的文件路徑名
$request_body_file
$request_uri, 請求的URI,帶查詢字符串;
$query_string, 與$args相同;
$scheme, 所用的協議,比如http或者是https,比如rewrite ^(.+)$ $scheme: //example .com$1 redirect;
$server_protocol, 請求的協議版本, "HTTP/1.0" "HTTP/1.1" ;
$server_addr, 服務器地址,如果沒有用listen指明服務器地址,使用這個變量將發起一次系統調用以取得地址(造成資源浪費);
$server_name, 請求到達的服務器名;
$server_port, 請求到達的服務器端口號;
$uri, 請求的URI,可能和最初的值有不同,比如經過重定向之類的。

rewrite 指令:可以使用在 server, location, if 區域;

語法:rewrite regex replacement flag

按照相關的正則表達式與字符串修改URI,指令按照在配置文件中出現的順序執行。  

可以在重寫指令后面添加標記。  

如果替換的字符串以http://開頭,請求將被重定向,並且不再執行多余的rewrite指令。  

尾部的標記(flag)可以是以下的值:

  • last - 完成重寫指令,之后搜索相應的URI或location。
  • break - 完成重寫指令。
  • redirect - 返回302臨時重定向,如果替換字段用http://開頭則被使用。
  • permanent - 返回301永久重定向。

注 意如果一個重定向是相對的(沒有主機名部分),nginx將在重定向的過程中使用匹配server_name指令的“Host”頭或者 server_name指令指定的第一個名稱,如果頭不匹配或不存在,如果沒有設置server_name,將使用本地主機名,如果你總是想讓nginx 使用“Host”頭,可以在server_name使用“*”通配符(查看http核心模塊中的server_name)。例如:

1
2
3
rewrite ^( /download/ .*) /media/ (.*)\..*$ $1 /mp3/ $2.mp3 last;
rewrite ^( /download/ .*) /audio/ (.*)\..*$ $1 /mp3/ $2.ra last;
return 403;

但是如果我們將其放入一個名為/download/的location中,則需要將last標記改為break,否則nginx將執行10次循環並返回500錯誤。

1
2
3
4
5
location /download/ {
  rewrite ^( /download/ .*) /media/ (.*)\..*$ $1 /mp3/ $2.mp3 break ;
  rewrite ^( /download/ .*) /audio/ (.*)\..*$ $1 /mp3/ $2.ra break ;
  return 403;
}

如果替換字段中包含參數,那么其余的請求參數將附加到后面,為了防止附加,可以在最后一個字符后面跟一個問號:

1
rewrite ^ /users/ (.*)$ /show ?user=$1? last;

注意:大括號({和}),可以同時用在正則表達式和配置塊中,為了防止沖突,正則表達式使用大括號需要用雙引號(或者單引號)。例如要重寫以下的URL:

1
/photos/123456

為:

1
/path/to/photos/12/1234/123456 .png

則使用以下正則表達式(注意引號):

1
rewrite "/photos/([0-9] {2})([0-9] {2})([0-9] {2})" /path/to/photos/ $1/$1$2/$1$2$3.png;

如果指定一個“?”在重寫的結尾,Nginx將丟棄請求中的參數,即變量$args,當使用$request_uri或$uri&$args時可以在rewrite結尾使用“?”以避免nginx處理兩次參數串。  

在rewrite中使用$request_uri將www.example.com重寫到example.com:

1
2
3
4
server {
  server_name www.example.com;
  rewrite ^ http: //example .com$request_uri? permanent;
}

同樣,重寫只對路徑進行操作,而不是參數,如果要重寫一個帶參數的URL,可以使用以下代替:

1
2
3
if ($args ^~ post=100){
  rewrite ^ http: //example .com /new-address .html? permanent;
}

注意$args變量不會被編譯,與location過程中的URI不同(參考http核心模塊中的location)

示例:當訪問www.lansgg.com的時候跳轉到www.Aries.com;

1
2
3
4
5
6
7
8
9
server {
  listen 80 default_server;
  server_name  www.lansgg.com lansgg.com;
  access_log  logs /lansgg .access.log main;
  error_log  logs /lansgg .error.log;
  root   /opt/nginx/nginx/html/lansgg ;
  index index.html;
  rewrite ^/ http: //www .Aries.com/;
  }

break 指令 可使用server, location, if 區域; 中止Rewirte,不在繼續匹配

last 指令 可server, location, if  區域;

last與break的區別在於,last並不會停止對下面location的匹配。

測驗一下break與last的區別

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server {
  listen 80 default_server;
  server_name www.lansgg.com lansgg.com;
  access_log logs /lansgg .access.log main;
  error_log logs /lansgg .error.log;
  root  /opt/nginx/nginx/html/lansgg ;
  index index.html;
  location /c1 .html {
  rewrite /c1 .html /c2 .html break ;
  }
  location /c2 .html {
  return 508;
  }
  }
[root@master sbin] # echo "c1" > /opt/nginx/nginx/html/lansgg/c1.html
[root@master sbin] # echo "c2" > /opt/nginx/nginx/html/lansgg/c2.html

使用break會停止匹配下面的location,直接發起請求www.lansgg.com/c1.html,他會顯示c2的內容;


wKiom1ReJwmg3h4tAABlg5WLTFc650.jpg  

使用last的話,會繼續搜索下面是否有符合條件(符合重寫后的/c2.html請求)的location。此時,/c2.html剛好與面location的條件對應上了,進入花括號{}里面的代碼執行,這里會返回508。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
server {
listen 80 default_server;
server_name www.lansgg.com lansgg.com;
access_log logs /lansgg .access.log main;
error_log logs /lansgg .error.log;
root  /opt/nginx/nginx/html/lansgg ;
index index.html;
location /c1 .html {
rewrite /c1 .html /c2 .html last;
}
location /c2 .html {
return 508;
}
}

使用firebug 可以看到;

wKiom1ReJ_bR_WwhAACvgot1dVM022.jpg

if 指令 可使用server, location 區域;

示例:當訪問http://www.lansgg.com網址的時候跳轉到www.Aries.com;

1
2
3
4
5
6
7
8
9
10
11
server {
  listen 80 default_server;
  server_name  www.lansgg.com lansgg.com;
  access_log  logs /lansgg .access.log main;
  error_log  logs /lansgg .error.log;
  root   /opt/nginx/nginx/html/lansgg ;
  index index.html;
  if ($http_host = www.lansgg.com){
  rewrite (.*) http: //www .Aries.com;
  }
  }

wKioL1ReMavwbbrPAAEAO-or-Mg289.jpg

return 指令 可使用server, location, if  區域

語法:return code  

這個指令結束執行配置語句並為客戶端返回狀態代碼,可以使用下列的值:204,400,402-406,408,410, 411, 413, 416與500-504。此外,非標准代碼444將關閉連接並且不發送任何的頭部。

rewrite_log  指令  可使用server, location, if  區域

啟用時將在error log中記錄notice 標記的重寫日志。

set 指令 可使用server, location, if  區域

語法:set variable value  

指令設置一個變量並為其賦值,其值可以是文本,變量和它們的組合。  

你可以使用set定義一個新的變量,但是不能使用set設置$http_xxx頭部變量的值。

uninitialized_variable_warn 指令 可使用 http, server, location, if  區域

語法:uninitialized_variable_warn on|off  

默認值:uninitialized_variable_warn on  

開啟或關閉在未初始化變量中記錄警告日志。  

事實上,rewrite指令在配置文件加載時已經編譯到內部代碼中,在解釋器產生請求時使用。  

expires 指令 可 http, server, location 區域

語法: expires [time|epoch|max|off]

默認值: expires off

該指令可以控制HTTP應答中的“Expires”和“Cache-Control”的頭標,(起到控制頁面緩存的作用)。可以在time值中使用正數或負數。“Expires”頭標的值將通過當前系統時間加上設定的 time 值來獲得。

epoch 指定“Expires”的值為 1 January, 1970, 00:00:01 GMT。

max 指定“Expires”的值為 31 December 2037 23:59:59 GMT,“Cache-Control”的值為10年。

-1 指定“Expires”的值為 服務器當前時間 -1s,即永遠過期

“Cache-Control”頭標的值由指定的時間來決定:

    負數:Cache-Control: no-cache

    正數或零:Cache-Control: max-age = #, # 為指定時間的秒數s。其他的單位有d(天),h(小時)

"off" 表示不修改“Expires”和“Cache-Control”的值

控制圖片等過期時間為30天,這個時間可以設置的更長。具體視情況而定

1
2
3
location ~ \.(gif|jpg|jpeg|png|bmp|ico)$ {
    log_not_found off; #不記錄404 not found 錯誤日志   expires 30d;
    break ;  }

控制匹配/resource/或者/mediatorModule/里所有的文件緩存設置到最長時間

1
2
3
4
location ~ /(resource|mediatorModule)/ {
   root /opt/demo ;
   expires max;
}

設定某個文件的過期時間;這里為600秒,並不記錄訪問日志

1
2
3
4
5
6
location ^~ /html/scripts/loadhead_1 .js {
access_log off;
root /opt/lampp/htdocs/web ;
expires 600;
break ;
}

設置GZIP

一般情況下壓縮后的html、css、js、PHP、jhtml等文件,大小能降至原來的25%,也就是說,原本一個100k的html,壓縮后只剩下25k。這無疑能節省很多帶寬,也能降低服務器的負載。

在nginx中配置gzip比較簡單

一般情況下只要在nginx.conf的http段中加入下面幾行配置即可

1
2
3
4
gzip on;
gzip_min_length 1000;
gzip_buffers  48k;
gzip_types  text /plain application /x-javascript text /css text /html application /xml ;

可以通過網頁gzip檢測工具來檢測網頁是否啟用了gzip

臨時重定向示例:訪問www.lansgg.com/c 重定向到www.lansgg.com/cc 

編輯nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
server {
   listen 80 default_server;
   server_name  www.lansgg.com lansgg.com;
   access_log  logs /lansgg .access.log main;
   error_log  logs /lansgg .error.log;
   root   /opt/nginx/nginx/html/lansgg ;
   index index.html;
   rewrite ^ /c/ (.*)$ http: //www .lansgg.com /cc/ $1;
   }
    
[root@master lansgg] # tree
.
├── c
│ └── index.html
├── cc
│ └── index.html
├── index.html
└── it.jpg
  
2 directories, 4 files

訪問http://www.lansgg.com/c 會跳轉到http://www.lansgg.com/cc
wKioL1RePF6hWtH5AAFKE7zHvzw477.jpg

302即為臨時重定向;

永久重定向(隱含重定向)

編輯nginx.conf

1
2
3
4
5
6
7
8
9
server {
  listen 80 default_server;
  server_name  www.lansgg.com lansgg.com;
  access_log  logs /lansgg .access.log main;
  error_log  logs /lansgg .error.log;
  root   /opt/nginx/nginx/html/lansgg ;
  index index.html;
  rewrite ^ /c/ (.*)$ /cc/ $1;
  }

訪問 http://www.lansgg.com/c/ 頁面顯示的是跳轉后的頁面,可是url卻沒有變化;firebug也看不到302代碼信息;現在它其實是301;

2、反向代理:是指以代理服務器來接受internet上的連接請求,然后將請求轉發給內部網絡上的服務器,並將從服務器上得到的結果返回給internet上請求連接的客戶端,此時代理服務器對外就表現為一個服務器。

2.1、配置nginx實現反向代理;

需求:訪問http://192.168.10.128/other 返回 apache主機的other目錄下的Index.html

wKioL1Rd21mwBFavAAIkMKYLkDs383.jpg

涉及nginx指令:

語法:proxy_pass URL    

可使用字段:location, location中的if字段       

這個指令設置被代理服務器的地址和被映射的URI,地址可以使用主機名或IP加端口號的形式,例如:proxy_pass http://192.168.10.129/url

2.2、配置nginx配置文件nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
server {
listen 80 default_server;
server_name www.lansgg.com lansgg.com;
access_log logs /lansgg .access.log main;
error_log logs /lansgg .error.log;
root  /opt/nginx/nginx/html/lansgg ;
location / {
  index index.html;
  }
location /other {
proxy_pass   http: //192 .168.10.129 /other ;
proxy_set_header X-Real-IP $remote_addr;
  }
}

2.3、配置client1

1
2
mkdir /var/www/html/other
echo "192.168.10.129" > /var/www/html/other/index .html

2.4、測試;

訪問url:    http://www.lansgg.com/other     你會發現跳轉到了 : http://192.168.10.129/other/

wKiom1Rd6hKzwCh2AAB13d4xNOk488.jpg

查看日志:

1
2
[root@client1 ~] # tail -f /var/log/httpd/access_log
192.168.10.1 - - [06 /Nov/2014 :21:25:44 +0800] "GET /other/ HTTP/1.1" 200 15 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0"

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM