<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                語法規則:?location \[=|~|~\*|^~\] /uri/ { … } 語法規則:?location空格\[=|~|~\*|^~\]空格/uri/ { … } \=?開頭表示精確匹配 ^~?開頭表示uri以某個常規字符串開頭,理解為匹配 url路徑即可。nginx不對url做編碼,因此請求為/static/20%/aa,可以被規則^~ /static/ /aa匹配到(注意是空格)。 ~?開頭表示區分大小寫的正則匹配 ~\* ?開頭表示不區分大小寫的正則匹配 !~和!~\*分別為區分大小寫不匹配及不區分大小寫不匹配?的正則 /?通用匹配,任何請求都會匹配到。 多個location配置的情況下匹配順序為(參考資料而來,還未實際驗證,試試就知道了,不必拘泥,僅供參考): 首先匹配 =,其次匹配^~, 其次是按文件中順序的正則匹配,最后是交給 / 通用匹配。當有匹配成功時候,停止匹配,按當前匹配規則處理請求。 ### ginx正則相關 ~????? 為區分大小寫字母的匹配。 ~\*???? 不區分大小寫字母的匹配(匹配aa的同時也匹配AA、Aa、aA)。 !~???? 與~相反,表示區分大小寫的不匹配 !~\*??? 與~\*相反,表示不分區大小寫的不匹配 . ??? 匹配除換行符以外的任意字符 \w ??? 匹配字母、數字、下劃線或漢字 \s ??? 匹配任意的空白符 \d ??? 匹配數字 \b ??? 匹配單詞的開始或結束 ^ ??? 匹配字符串的開始 $ ??? 匹配字符串的結束 \* ??? 重復零次或更多次 \+ ??? 重復一次或更多次 ? ??? 重復零次或一次 {n} ??? 重復n次 {n,} ??? 重復n次或更多次 {n,m} ??? 重復n到m次 \*? ??? 重復任意次,但盡可能少重復 +? ??? 重復1次或更多次,但盡可能少重復 ?? ??? 重復0次或1次,但盡可能少重復 {n,m}? ??? 重復n到m次,但盡可能少重復 {n,}? ??? 重復n次以上,但盡可能少重復 \W ??? 匹配任意不是字母,數字,下劃線,漢字的字符 \S ??? 匹配任意不是空白符的字符 \D ??? 匹配任意非數字的字符 \B ??? 匹配不是單詞開頭或結束的位置 [^x] ??? 匹配除了x以外的任意字符 [^aeiou] ??? 匹配除了aeiou這幾個字母以外的任意字符 捕獲 ??? (exp) ??? 匹配exp,并捕獲文本到自動命名的組里 (?exp) ??? 匹配exp,并捕獲文本到名稱為name的組里,也可以寫成(?'name'exp) (?:exp) ??? 匹配exp,不捕獲匹配的文本,也不給此分組分配組號 零寬斷言 ??? (?=exp) ??? 匹配exp前面的位置 (?<=exp) ??? 匹配exp后面的位置 (?!exp) ??? 匹配后面跟的不是exp的位置 (?<!exp) ??? 匹配前面不是exp的位置 注釋 ??? (?#comment) ??? 這種類型的分組不對正則表達式的處理產生任何影響,用于提供注釋讓人閱讀 ## location匹配順序 1. "="前綴指令匹配,如果匹配成功,則停止其他匹配 2. 普通字符串指令匹配,順序是從長到短,匹配成功的location如果使用^~,則停止其他匹配(正則匹配) 3. 正則表達式指令匹配,按照配置文件里的順序,成功就停止其他匹配 4. 如果第三步中有匹配成功,則使用該結果,否則使用第二步結果 ## 注意點 1. 匹配的順序是先匹配普通字符串,然后再匹配正則表達式。另外普通字符串匹配順序是根據配置中字符長度從長到短,也就是說使用普通字符串配置的location順序是無關緊要的,反正最后nginx會根據配置的長短來進行匹配,但是需要注意的是正則表達式按照配置文件里的順序測試。找到第一個比配的正則表達式將停止搜索。 2. 一般情況下,匹配成功了普通字符串location后還會進行正則表達式location匹配。有兩種方法改變這種行為,其一就是使用“=”前綴,這時執行的是嚴格匹配,并且匹配成功后立即停止其他匹配,同時處理這個請求;另外一種就是使用“^~”前綴,如果把這個前綴用于一個常規字符串那么告訴nginx 如果路徑匹配那么不測試正則表達式。 ## 匹配模式及順序 ```   location = /uri?   =開頭表示精確匹配,只有完全匹配上才能生效。   location ^~ /uri?  ^~ 開頭對URL路徑進行前綴匹配,并且在正則之前。   location ~ pattern? ~開頭表示區分大小寫的正則匹配。   location ~* pattern? ~*開頭表示不區分大小寫的正則匹配。   location /uri?    不帶任何修飾符,也表示前綴匹配,但是在正則匹配之后。   location /?     通用匹配,任何未匹配到其它location的請求都會匹配到,相當于switch中的default。? ``` ## 案例 測試"^~"和"~",nginx配置如下。瀏覽器輸入http://localhost/helloworld/test,返回601。如將#1注釋,#2打開,瀏覽器輸入http://localhost/helloworld/test,返回603。注:#1和#2不能同時打開,如同時打開,啟動nginx會報nginx: \[emerg\] duplicate location "/helloworld"...,因為這兩個都是普通字符串。 ~~~ location ^~ /helloworld { #1 return 601; } #location /helloworld { #2 # return 602; #} location ~ /helloworld { return 603; } ~~~ 測試普通字符串的長短(普通字符串的匹配與順序無關,與長短有關)。瀏覽器輸入http://localhost/helloworld/test/a.html,返回601。瀏覽器輸入http://localhost/helloworld/a.html,返回602。 ~~~ location /helloworld/test/ { #1 return 601; } location /helloworld/ { #2 return 602; } ~~~ 測試正則表達式的順序(正則匹配與順序相關)。瀏覽器輸入http://localhost/helloworld/test/a.html,返回602;將#2和#3調換順序,瀏覽器輸入http://localhost/helloworld/test/a.html,返回603 ~~~ location /helloworld/test/ { #1 return 601; } location ~ /helloworld { #2 return 602; } location ~ /helloworld/test { #3 return 603; } ~~~ 例子,有如下匹配規則: ~~~ location = / { #規則A (訪問網站根目錄才會走這比如http://localhost/) =開頭表示精確匹配 } location = /login { #規則B(http://localhost/login) } location ^~ /static/ { #規則C (http://localhost/static/a.html) ^~開頭表示以常規字符串開頭的url路徑 } location ~ \.(gif|jpg|png|js|css)$ { #規則D (訪問 http://localhost/a.gif, http://localhost/b.jpg 將匹配規則D和規則E,但是規則D順序優先,規則E不起作用,而 http://localhost/static/c.png 則優先匹配到 規則C) ~開頭表示區分大小寫的匹配 } location ~* \.png$ { #規則E (訪問 http://localhost/a.PNG 則匹配規則E, 而不會匹配規則D,因為規則E不區分大小寫) ~*開頭表示不區分大小寫的正則匹配 } location !~ \.xhtml$ { #規則F !~開頭表示區分大小寫的不匹配 } location !~* \.xhtml$ { #規則G !~*開頭表示不區分大小寫的不匹配 } location / { #規則H (http://localhost/register) } ~~~ 訪問 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作為方向代理服務器存在。 那么產生的效果如下: 訪問根目錄/, 比如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作為方向代理服務器存在。 所以實際使用中,通常至少有三個匹配規則定義,如下: ~~~ #直接匹配網站根,通過域名訪問網站首頁比較頻繁,使用這個會加速處理,官網如是說。 #這里是直接轉發給后端應用服務器了,也可以是一個靜態首頁 # 第一個必選規則 location = / { proxy_pass http://tomcat:8080/index } # 第二個必選規則是處理靜態文件請求,這是nginx作為http服務器的強項 # 有兩種配置模式,目錄匹配或后綴匹配,任選其一或搭配使用 location ^~ /static/ { root /webroot/static/; } location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ { root /webroot/res/; } #第三個規則就是通用規則,用來轉發動態請求到后端應用服務器 #非靜態文件請求就默認是動態請求,自己根據實際把握 #畢竟目前的一些框架的流行,帶.php,.jsp后綴的情況很少了 location / { proxy_pass http://tomcat:8080/ } ~~~ 三、ReWrite語法 last– 基本上都用這個Flag。 break– 中止Rewirte,不在繼續匹配 redirect– 返回臨時重定向的HTTP狀態302 permanent– 返回永久重定向的HTTP狀態301 注:last和break最大的不同在于 \- break是終止當前location的rewrite檢測,而且不再進行location匹配 - last是終止當前location的rewrite檢測,但會繼續重試location匹配并處理區塊中的rewrite規則 1、下面是可以用來判斷的表達式: \-f和!-f用來判斷是否存在文件 \-d和!-d用來判斷是否存在目錄 \-e和!-e用來判斷是否存在文件或目錄 \-x和!-x用來判斷文件是否可執行 2、下面是可以用作判斷的全局變量 $args #這個變量等于請求行中的參數。 $content\_length #請求頭中的Content-length字段。 $content\_type #請求頭中的Content-Type字段。 $document\_root #當前請求在root指令中指定的值。 $host #請求主機頭字段,否則為服務器名稱。 $http\_user\_agent #客戶端agent信息 $http\_cookie #客戶端cookie信息 $limit\_rate #這個變量可以限制連接速率。 $request\_body\_file #客戶端請求主體信息的臨時文件名。 $request\_method #客戶端請求的動作,通常為GET或POST。 $remote\_addr #客戶端的IP地址。 $remote\_port #客戶端的端口。 $remote\_user #已經經過Auth Basic Module驗證的用戶名。 $request\_filename #當前請求的文件路徑,由root或alias指令與URI請求生成。 $query\_string #與$args相同。 $scheme #HTTP方法(如http,https)。 $server\_protocol #請求使用的協議,通常是HTTP/1.0或HTTP/1.1。 $server\_addr #服務器地址,在完成一次系統調用后可以確定這個值。 $server\_name #服務器名稱。 $server\_port #請求到達服務器的端口號。 $request\_uri #包含請求參數的原始URI,不包含主機名,如:”/foo/bar.php?arg=baz”。 $uri #不帶請求參數的當前URI,$uri不包含主機名,如”/foo/bar.html”。 $document\_uri #與$uri相同。 例:http://localhost:88/test1/test2/test.php $host:localhost $server\_port:88 $request\_uri:http://localhost:88/test1/test2/test.php $document\_uri:/test1/test2/test.php $document\_root:D:\\nginx/html $request\_filename:D:\\nginx/html/test1/test2/test.php 四、Redirect語法 多目錄轉成參數 abc.domian.com/sort/2 => abc.domian.com/index.php?act=sort&name=abc&id=2 ~~~ if ($host ~* (.*)\.domain\.com) { set $sub_name $1; rewrite ^/sort\/(\d+)\/?$ /index.php?act=sort&cid=$sub_name&id=$1 last; } ~~~ 目錄對換 /123456/xxxx -> /xxxx?id=123456 1.?????rewrite ^/(\\d+)/(.+)/ /$2?id=$1 last; 例如下面設定nginx在用戶使用ie的使用重定向到/nginx-ie目錄下: ~~~ if ($http_user_agent ~ MSIE) { rewrite ^(.*)$ /nginx-ie/$1 break; } ~~~ 目錄自動加“/” ~~~ if (-d $request_filename){ rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent; } ~~~ 禁止htaccess ~~~ location ~/\.ht { deny all; } ~~~ 禁止多個目錄 ~~~ location ~ ^/(cron|templates)/ { deny all; break;中止,不在繼續匹配 } ~~~ 禁止以/data開頭的文件 可以禁止/data/下多級目錄下.log.txt等請求; ~~~ location ~ ^/data { deny all; } ~~~ 禁止單個目錄 不能禁止.log.txt能請求 ~~~ location /searchword/cron/ { deny all; } ~~~ 禁止單個文件 ~~~ location ~ /data/sql/data.sql { deny all; } ~~~ 給favicon.ico和robots.txt設置過期時間; 這里為favicon.ico為99 天,robots.txt為7天并不記錄404錯誤日志 ~~~ location ~(favicon.ico) { log_not_found off; expires 99d; break; } location ~(robots.txt) { log_not_found off; expires 7d; break; } ~~~ 設定某個文件的過期時間;這里為600秒,并不記錄訪問日志 ~~~ location ^~ /html/scripts/loadhead_1.js { access_log off; root /opt/lampp/htdocs/web; expires 600; break; } ~~~ 文件反盜鏈并設置過期時間 這里的return 412 為自定義的http狀態碼,默認為403,方便找出正確的盜鏈的請求 “rewrite ^/ http://leech.c1gstudio.com/leech.gif;”顯示一張防盜鏈圖片 “access\_log off;”不記錄訪問日志,減輕壓力 “expires 3d”所有文件3天的瀏覽器緩存 ~~~ location ~* ^.+\.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ { valid_referers none blocked *.c1gstudio.com *.c1gstudio.net localhost 208.97.167.194; if ($invalid_referer) { rewrite ^/ http://leech.c1gstudio.com/leech.gif; return 412; break; } access_log off; root /opt/lampp/htdocs/web; expires 3d; break; } ~~~ 只充許固定ip訪問網站,并加上密碼 ~~~ root /opt/htdocs/www; allow 208.97.167.194; allow 222.33.1.2; allow 231.152.49.4; deny all; auth_basic "C1G_ADMIN"; auth_basic_user_file htpasswd; ~~~ 將多級目錄下的文件轉成一個文件,增強seo效果 /job-123-456-789.html 指向/job/123/456/789.htm ~~~ rewrite ^/job-([0-9]+)-([0-9]+)-([0-9]+)\.html$ /job/$1/$2/jobshow_$3.html last; ~~~ 將根目錄下某個文件夾指向2級目錄 如/shanghaijob/ 指向 /area/shanghai/ 如果你將last改成permanent,那么瀏覽器地址欄顯是 /location/shanghai/ ~~~ rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last; ~~~ 上面例子有個問題是訪問/shanghai 時將不會匹配 ~~~ rewrite ^/([0-9a-z]+)job$ /area/$1/ last; rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last; ~~~ 這樣/shanghai 也可以訪問了,但頁面中的相對鏈接無法使用, 如./list\_1.html真實地址是/area /shanghia/list\_1.html會變成/list\_1.html,導至無法訪問。 那我加上自動跳轉也是不行咯 (-d $request\_filename)它有個條件是必需為真實目錄,而我的rewrite不是的,所以沒有效果 ~~~ if (-d $request_filename){ rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent; } ~~~ 知道原因后就好辦了,讓我手動跳轉吧 ~~~ rewrite ^/([0-9a-z]+)job$ /$1job/ permanent; rewrite ^/([0-9a-z]+)job/(.*)$ /area/$1/$2 last; ~~~ 文件和目錄不存在的時候重定向: ~~~ if (!-e $request_filename) { proxy_pass http://127.0.0.1; } ~~~ 域名跳轉 ~~~ server { listen 80; server_name jump.c1gstudio.com; index index.html index.htm index.php; root /opt/lampp/htdocs/www; rewrite ^/ http://www.c1gstudio.com/; access_log off; } ~~~ 多域名轉向 ~~~ server_name www.c1gstudio.com www.c1gstudio.net; index index.html index.htm index.php; root /opt/lampp/htdocs; if ($host ~ "c1gstudio\.net") { rewrite ^(.*) http://www.c1gstudio.com$1 permanent; } ~~~ 三級域名跳轉 ~~~ if ($http_host ~* "^(.*)\.i\.c1gstudio\.com$") { rewrite ^(.*) http://top.yingjiesheng.com$1; break; } ~~~ 域名鏡向 ~~~ server { listen 80; server_name mirror.c1gstudio.com; index index.html index.htm index.php; root /opt/lampp/htdocs/www; rewrite ^/(.*) http://www.c1gstudio.com/$1 last; access_log off; } ~~~ Nginx 服務器開啟status頁面檢測服務狀態開啟后直接在瀏覽器的網站url地址后加上nginx_status 如:http://101.201.238.125:8080/nginx_status ~~~ #Nginx 服務器開啟status頁面檢測服務狀態 location /nginx_status { stub_status on; #并禁用了訪問寫入log文件 access_log off; #對訪問者進行過濾 #allow 223.xxx.xxx.xxx; deny all; } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看