1. 程式人生 > 實用技巧 >nginx location配置詳細解釋

nginx location配置詳細解釋

nginx location配置詳細解釋

server_name _; #不啟用域名

指令-熱啟動

Nginx重新讀取配置的命令

nginx -s reload

看文件的方法

gzip壓縮檔案模組的使用:

參考:nginx官方文件-》Modules reference-》ngx_http_gzip_module

語法詳解

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

  • =開頭表示精確匹配

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

  • ~開頭表示區分大小寫的正則匹配以xx結尾

  • ~*開頭表示不區分大小寫的正則匹配以xx結尾

  • !~!~*分別為區分大小寫不匹配及不區分大小寫不匹配 的正則

  • /通用匹配,任何請求都會匹配到。

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

首先精確匹配 =-》其次以xx開頭匹配^~-》然後是按檔案中順序的正則匹配-》最後是交給 / 通用匹配。

當有匹配成功時候,停止匹配,按當前匹配規則處理請求。

例子,有如下匹配規則:

location = / {
   #規則A
}
location = /login {
   #規則B
}
location ^~ /static/ {
   #規則C
}
location ~ \.(gif|jpg|png|js|css)$ {
   #規則D,注意:是根據括號內的大小寫進行匹配。括號內全是小寫,只匹配小寫
}
location ~* \.png$ {
   #規則E
}
location !~ \.xhtml$ {
   #規則F
}
location !~* \.xhtml$ {
   #規則G
}
location / {
   #規則H
}

那麼產生的效果如下:

訪問根目錄/, 比如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作為方向代理伺服器存在。

所以實際使用中,個人覺得至少有三個匹配規則定義,如下:

  1. #直接匹配網站根,通過域名訪問網站首頁比較頻繁,使用這個會加速處理,官網如是說。
  2. #這裡是直接轉發給後端應用伺服器了,也可以是一個靜態首頁
  3. # 第一個必選規則
  4. location = / {
  5. proxy_pass http://tomcat:8080/index
  6. }
  7. # 第二個必選規則是處理靜態檔案請求,這是nginx作為http伺服器的強項
  8. # 有兩種配置模式,目錄匹配或字尾匹配,任選其一或搭配使用
  9. location ^~ /static/ { //以xx開頭
  10. root /webroot/static/;
  11. }
  12. location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ { //以xx結尾
  13. root /webroot/res/;
  14. }
  15. #第三個規則就是通用規則,用來轉發動態請求到後端應用伺服器
  16. #非靜態檔案請求就預設是動態請求,自己根據實際把握
  17. location / {
  18. proxy_pass http://tomcat:8080/
  19. }

nginx的其他配置資訊介紹

三、ReWrite語法

last– 基本上都用這個Flag。
break– 中止Rewirte,不在繼續匹配
redirect– 返回臨時重定向的HTTP狀態302
permanent– 返回永久重定向的HTTP狀態301

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

-f!-f用來判斷是否存在檔案
-d!-d用來判斷是否存在目錄
-e!-e用來判斷是否存在檔案或目錄
-x!-x用來判斷檔案是否可執行

2、下面是可以用作判斷的全域性變數

例:http://localhost:88/test1/test2/test.php

  1. $host:localhost
  2. $server_port:88
  3. $request_uri:http://localhost:88/test1/test2/test.php
  4. $document_uri:/test1/test2/test.php
  5. $document_root:D:\nginx/html
  6. $request_filename:D:\nginx/html/test1/test2/test.php

附:一些可用的全域性變數

  1. $args
  2. $content_length
  3. $content_type
  4. $document_root
  5. $document_uri
  6. $host
  7. $http_user_agent
  8. $http_cookie
  9. $limit_rate
  10. $request_body_file
  11. $request_method
  12. $remote_addr
  13. $remote_port
  14. $remote_user
  15. $request_filename
  16. $request_uri
  17. $query

一些常用的配置

1、普通的(靜態的)http伺服器

這樣如果訪問http://localhost 就會預設訪問到E盤wwwroot目錄下面的index.html,如果一個網站只是靜態頁面的話,那麼就可以通過這種方式來實現部署。

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. client_max_body_size 1024M;
  5. location / {
  6. root e:wwwroot;//思路:通過/將所有的請求,轉發給root處理
  7. index index.html;
  8. }
  9. }

2、反向代理

localhost的時候,就相當於訪問localhost:8080了

server {  
    listen       80;                                                         
    server_name  localhost;                                               
    client_max_body_size 1024M;

    location / {
        proxy_pass http://localhost:8080;   
        proxy_set_header Host $host:$server_port; //思路:通過/,將所有的請求,轉發給第3方處理
    }
}

既然伺服器可以直接HTTP訪問,為什麼要在中間加上一個反向代理,不是多此一舉嗎?反向代理有什麼作用?

負載均衡、虛擬主機等,都基於反向代理實現,當然反向代理的功能也不僅僅是這些。

3、Redirect(重定向)語法

  1. server {
  2. listen 80;
  3. server_name start.igrow.cn;
  4. index index.html index.php;
  5. root html;
  6. if ($http_host !~ "^star\.igrow\.cn$" {
  7. rewrite ^(.*) http://star.igrow.cn$1 redirect;
  8. }
  9. }

4、防盜鏈

location ~* \.(gif|jpg|png|bmp)$ {
    valid_referers none blocked *.ttlsa.com server_names ~\.google\. ~\.baidu\.;
    if ($invalid_referer) {
        return 403;
        #rewrite ^/ http://www.ttlsa.com/403.jpg;
    }
}

5、根據檔案型別設定過期時間

  1. location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
  2. if (-f $request_filename) { //只能是檔案,因為這用-f判斷了
  3. expires 1h;
  4. break;
  5. }
  6. }

6、設定圖片快取(過期)時間

7、禁止訪問某個目錄

  1. location ~* \.(txt|doc)${
  2. root /data/www/wwwroot/linuxtone/test; #所有使用者都禁止訪問這個目錄
  3. deny all;
  4. }

8、隱藏版本號的作用

通過你所用的版本,找其漏洞,進行攻擊你

在http中新增該配置:server_tokens off;

9、配置https

1、去阿里雲/騰訊雲申請免費的

2、下載證書

3、證書放到/usr/local/nginx目錄下(就是和conf同級,nginx.conf預設的配置檔案的上一級)

4、在vhost目錄下加入配置檔案

server {
 listen 443;
 server_name lampol.edu0532.cn; #改域名
 ssl on;
 root /home/www/xcxtp5/public; #改專案路徑
 ssl_certificate ../certbo/1523694051089.pem;    #改證書路徑
 ssl_certificate_key ../certbo/1523694051089.key; #改私鑰路徑
 ssl_session_timeout 5m;
 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
 ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
 ssl_prefer_server_ciphers on;
        location / {
            index  index.html index.htm index.php;
            autoindex  on;
            # 偽靜態配置  
            if (!-e $request_filename) {
                rewrite  ^(.*)$  /index.php?s=$1  last;
                break;
            }
        }

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include        fastcgi.conf;
        }

}

10、動靜分離

思路:動、靜態的檔案,請求時匹配不同的目錄

當訪問gif,jpeg時 直接訪問e:wwwroot;,正則自行配置

server {  
  listen       80;  
  server_name  localhost;  

  location / {  
      root   e:wwwroot;  
      index  index.html;  
  }  

  # 所有靜態請求都由nginx處理,存放目錄為html  
  location ~ .(gif|jpg|jpeg|png|bmp|swf|css|js)$ {  
      root    e:wwwroot;  
  }  

  # 所有動態請求都轉發給tomcat處理  
  location ~ .(jsp|do)$ {  
      proxy_pass  http://test;  
  }  

  error_page   500 502 503 504  /50x.html;  
  location = /50x.html {  
      root   e:wwwroot;  
  }  
}