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.  
     
  8.  
    # 第二個必選規則是處理靜態文件請求,這是nginx作為http服務器的強項
  9.  
    # 有兩種配置模式,目錄匹配或后綴匹配,任選其一或搭配使用
  10.  
    location ^~ /static/ {                     //以xx開頭
  11.  
    root /webroot/static/;
  12.  
    }
  13.  
    location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ { //以xx結尾
  14.  
    root /webroot/res/;
  15.  
    }
  16.  
     
  17.  
    #第三個規則就是通用規則,用來轉發動態請求到后端應用服務器
  18.  
    #非靜態文件請求就默認是動態請求,自己根據實際把握
  19.  
    location / {
  20.  
    proxy_pass http://tomcat:8080/
  21.  
    }

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.  
     
  6.  
     
  7.  
    location / {                
  8.  
    root e:wwwroot;            //思路:通過/將所有的請求,轉發給root處理
  9.  
    index index.html;
  10.  
    }
  11.  
    }

 

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; } } 


免責聲明!

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



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