天天看點

nginx location 配置詳細解釋

文法規則: location [=|~|~*|^~] /uri/ { … }

= 開頭表示精确比對

^~ 開頭表示uri以某個正常字元串開頭,了解為比對 url路徑即可。nginx不對url做編碼,是以請求為/static/20%/aa,可以被規則^~ /static/ /aa比對到(注意是空格)。

~ 開頭表示區分大小寫的正則比對

~* 開頭表示不區分大小寫的正則比對

!~和!~*分别為區分大小寫不比對及不區分大小寫不比對 的正則

/ 通用比對,任何請求都會比對到。

多個location配置的情況下比對順序為(參考資料而來,還未實際驗證,試試就知道了,不必拘泥,僅供參考):

首先比對 =,其次比對^~, 其次是按檔案中順序的正則比對,最後是交給 / 通用比對。當有比對成功時候,停止比對,按目前比對規則處理請求。

例子,有如下比對規則:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

<code>location = / {</code>

<code>   </code><code>#規則A</code>

<code>}</code>

<code>location = </code><code>/login</code> <code>{</code>

<code>   </code><code>#規則B</code>

<code>location ^~ </code><code>/static/</code> <code>{</code>

<code>   </code><code>#規則C</code>

<code>location ~ \.(gif|jpg|png|js|css)$ {</code>

<code>   </code><code>#規則D</code>

<code>location ~* \.png$ {</code>

<code>   </code><code>#規則E</code>

<code>location !~ \.xhtml$ {</code>

<code>   </code><code>#規則F</code>

<code>location !~* \.xhtml$ {</code>

<code>   </code><code>#規則G</code>

<code>location / {</code>

<code>   </code><code>#規則H</code>

那麼産生的效果如下:

通路根目錄/, 比如http://localhost/ 将比對規則A

通路 http://localhost/login 将比對規則B,http://localhost/register 則比對規則H

通路 http://localhost/static/a.html 将比對規則C

通路 http://localhost/a.gif, http://localhost/b.jpg 将比對規則D和規則E,但是規則D順序優先,規則E不起作用, 而 http://localhost/static/c.png 則優先比對到 規則C

通路 http://localhost/a.PNG 則比對規則E, 而不會比對規則D,因為規則E不區分大小寫。

通路 http://localhost/a.xhtml 不會比對規則F和規則G,http://localhost/a.XHTML不會比對規則G,因為不區分大小寫。規則F,規則G屬于排除法,符合比對規則但是不會比對到,是以想想看實際應用中哪裡會用到。

通路 http://localhost/category/id/1111 則最終比對到規則H,因為以上規則都不比對,這個時候應該是nginx轉發請求給後端應用伺服器,比如FastCGI(php),tomcat(jsp),nginx作為方向代理伺服器存在。

是以實際使用中,個人覺得至少有三個比對規則定義,如下:

<code>#直接比對網站根,通過域名通路網站首頁比較頻繁,使用這個會加速處理,官網如是說。</code>

<code>#這裡是直接轉發給後端應用伺服器了,也可以是一個靜态首頁</code>

<code># 第一個必選規則</code>

<code>    </code><code>proxy_pass http:</code><code>//tomcat</code><code>:8080</code><code>/index</code>

<code># 第二個必選規則是處理靜态檔案請求,這是nginx作為http伺服器的強項</code>

<code># 有兩種配置模式,目錄比對或字尾比對,任選其一或搭配使用</code>

<code>    </code><code>root </code><code>/webroot/static/</code><code>;</code>

<code>location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {</code>

<code>    </code><code>root </code><code>/webroot/res/</code><code>;</code>

<code>#第三個規則就是通用規則,用來轉發動态請求到後端應用伺服器</code>

<code>#非靜态檔案請求就預設是動态請求,自己根據實際把握</code>

<code>#畢竟目前的一些架構的流行,帶.php,.jsp字尾的情況很少了</code>

<code>    </code><code>proxy_pass http:</code><code>//tomcat</code><code>:8080/</code>

nginx的其他配置資訊介紹

三、ReWrite文法

last – 基本上都用這個Flag。

break – 中止Rewirte,不在繼續比對

redirect – 傳回臨時重定向的HTTP狀态302

permanent – 傳回永久重定向的HTTP狀态301

1、下面是可以用來判斷的表達式:

-f和!-f用來判斷是否存在檔案

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

-e和!-e用來判斷是否存在檔案或目錄

-x和!-x用來判斷檔案是否可執行

2、下面是可以用作判斷的全局變量

<code>$host:localhost</code>

<code>$server_port:88</code>

<code>$request_uri:http:</code><code>//localhost</code><code>:88</code><code>/test1/test2/test</code><code>.php</code>

<code>$document_uri:</code><code>/test1/test2/test</code><code>.php</code>

<code>$document_root:D:\nginx</code><code>/html</code>

<code>$request_filename:D:\nginx</code><code>/html/test1/test2/test</code><code>.php</code>

四、Redirect文法

<code>server {</code>

<code>    </code><code>listen 80;</code>

<code>    </code><code>server_name start.igrow.cn;</code>

<code>    </code><code>index index.html index.php;</code>

<code>    </code><code>root html;</code>

<code>    </code><code>if</code> <code>($http_host !~ </code><code>"^star\.igrow\.cn$"</code> <code>{</code>

<code>        </code><code>rewrite ^(.*) http:</code><code>//star</code><code>.igrow.cn$1 redirect;</code>

<code>    </code><code>}</code>

五、防盜鍊

<code>location ~* \.(gif|jpg|swf)$ {</code>

<code>    </code><code>valid_referers none blocked start.igrow.cn sta.igrow.cn;</code>

<code>    </code><code>if</code> <code>($invalid_referer) {</code>

<code>        </code><code>rewrite ^/ http:</code><code>//</code><code>$host</code><code>/logo</code><code>.png;</code>

六、根據檔案類型設定過期時間

<code>location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {</code>

<code>    </code><code>if</code> <code>(-f $request_filename) {</code>

<code>        </code><code>expires 1h;</code>

<code>        </code><code>break</code><code>;</code>

七、禁止通路某個目錄

<code>location ~* \.(txt|doc)${</code>

<code>root </code><code>/data/www/wwwroot/linuxtone/test</code><code>;</code>

<code>deny all;</code>

附:一些可用的全局變量

<code>$args</code>

<code>$content_length</code>

<code>$content_type</code>

<code>$document_root</code>

<code>$document_uri</code>

<code>$host</code>

<code>$http_user_agent</code>

<code>$http_cookie</code>

<code>$limit_rate</code>

<code>$request_body_file</code>

<code>$request_method</code>

<code>$remote_addr</code>

<code>$remote_port</code>

<code>$remote_user</code>

<code>$request_filename</code>

<code>$request_uri</code>

<code>$query</code>

本文轉自 tanzhenchao 51CTO部落格,原文連結:http://blog.51cto.com/cmdschool/1704229,如需轉載請自行聯系原作者