1.nginx模塊化php
Nginx涉及到的模塊分爲核心模塊、標準HTTP模塊、可選HTTP模塊、郵件服務模塊以及第三方模塊等五大類。css
https://coding.net/u/aminglinux/p/nginx/git/blob/master/4z/module.mdhtml
[root@centos-03 objs]# ls ngx_modules.c ngx_modules.c [root@centos-03 objs]#
2.模塊目錄linux
[root@centos-03 objs]# cd src [root@centos-03 src]# ls core event http mail misc os stream [root@centos-03 src]#
3.nginx WEB請求機制nginx
https://coding.net/u/aminglinux/p/nginx/git/blob/master/4z/IO.mdgit
線程是進程的一個子單元,線程比進程好的地方是能夠節省更多的資源,假如我開一個進程耗費的資源是20兆,那我開10個線程也是佔用20兆,這樣我一樣的內存能夠開更多的線程出來,每一個線程也是處理一個請求,線程也有弊端,須要和其餘的線程共享內存,穩定性不是很好 github
同步:同步、異步發生在當客戶端發起請求後,服務端處理客戶端的請求時。 同步機制,是指客戶端發送請求後,須要等待服務端(內核)返回信息後,再繼續發送下一個請求。 在同步機制中,全部的請求在服務器端獲得同步,即發送方和接收方對請求的處理步調是一致的。web
異步:異步機制,是指客戶端發出一個請求後,不等待服務端(內核)返回信息,就繼續發送下一個請求。 在異步機制中,全部來自發送方的請求造成一個隊列,接收方處理完後再通知發送方。正則表達式
阻塞:阻塞與非阻塞發生在IO調度中,好比內核到磁盤IO。 阻塞方式下,進程/線程在獲取最終結果以前,被系統掛起了,也就是所謂的阻塞了,在阻塞過程當中該進程什麼都幹不了, 直到最終結果反饋給它時,它才恢復運行狀態。chrome
4.nginx事件驅動模型
https://coding.net/u/aminglinux/p/nginx/git/blob/master/4z/event.md
事件驅動模型是實現異步非阻塞的一個手段。事件驅動模型中,一個進程(線程)就能夠了。 對於web服務器來講,客戶端A的請求鏈接到服務端時,服務端的某個進程(Nginx worker process)會處理該請求, 此進程在沒有返回給客戶端A結果時,它又去處理了客戶端B的請求。 服務端把客戶端A以及客戶端B發來的請求做爲事件交給了「事件收集器」, 而「事件收集器」再把收集到的事件交由「事件發送器」發送給「事件處理器」進行處理。 最後「事件處理器」處理完該事件後,通知服務端進程,服務端進程再把結果返回給客戶端A、客戶端B。 在這個過程當中,服務端進程作的事情屬於用戶級別的,而事件處理這部分工做屬於內核級別的。 也就是說這個事件驅動模型是須要操做系統內核來做爲支撐的。
5.Nginx架構
Nginx服務器使用 master/worker 多進程模式。 主進程(Master process)啓動後,會接收和處理外部信號; 主進程啓動後經過fork() 函數產生一個或多個子進程(work process),每一個子進程會進行進程初始化、 模塊調用以及對事件的接收和處理等工做。
1.nginx配置文件包括三部分:events上面的是全局部分、events、http(http裏面有server,每個server就是一個虛擬主機,一個http裏面有多個server,能夠跑多個站點,通常咱們都是在http最下面加一條include vhost/*.conf,把全部的虛擬主機配置寫到這個目錄裏面(http裏面默認的server也註釋掉))
[root@centos-03 conf]# ls /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf [root@centos-03 conf]#
2.建立vhost目錄,而後在vhost目錄下建立.conf文件,咱們這裏全部的站點文件都放到data/wwwroot/目錄下
[root@centos-03 conf]# mkdir vhost [root@centos-03 conf]#
[root@centos-03 conf]# mkdir -p /data/wwwroot/ [root@centos-03 conf]#
[root@centos-03 conf]# mkdir /data/wwwroot/www.1.com [root@centos-03 conf]#
[root@centos-03 vhost]# cd vhost/
[root@centos-03 vhost]# vim 1.conf^C [root@centos-03 vhost]# cat 1.conf server { listen 80; server_name www.1.com; root /data/wwwroot/www.1.com; } [root@centos-03 vhost]#
[root@centos-03 www.1.com]# cd /data/wwwroot/www.1.com/
[root@centos-03 www.1.com]# vim index.html^C [root@centos-03 www.1.com]# cat index.html www.1.com [root@centos-03 www.1.com]#
3.檢查下配置文件是否有錯
[root@centos-03 conf]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@centos-03 conf]#
4.從新加載配置文件
[root@centos-03 conf]# /usr/local/nginx/sbin/nginx -s reload [root@centos-03 conf]#
5.測試(至關於咱們的www.1.com解析到了127.0.0.1上面了)
[root@centos-03 conf]# curl -x127.0.0.1:80 www.1.com www.1.com [root@centos-03 conf]#
6.這裏咱們用任何域名訪問nginx都指向了www.1.com,這是由於nginx有一個默認虛擬主機的說法(訪問沒有配置的域名會指向其中一個虛擬主機),咱們須要配置一個默認虛擬主機作一個限制,拒絕訪問。
[root@centos-03 conf]# curl -x127.0.0.1:80 www.a.com www.1.com [root@centos-03 conf]# curl -x127.0.0.1:80 www.b.com www.1.com [root@centos-03 conf]#
[root@centos-03 vhost]# cd vhost/^C [root@centos-03 vhost]# cp 1.conf default.conf^C [root@centos-03 vhost]# vim default.conf ^C [root@centos-03 vhost]# cat default.conf server { listen 80 default_server; deny all; } [root@centos-03 vhost]#
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -s reload [root@centos-03 vhost]# curl -x127.0.0.1:80 www.a.com <html> <head><title>403 Forbidden</title></head> <body bgcolor="white"> <center><h1>403 Forbidden</h1></center> <hr><center>nginx/1.14.0</center> </body> </html> [root@centos-03 vhost]# curl -x127.0.0.1:80 www.b.com <html> <head><title>403 Forbidden</title></head> <body bgcolor="white"> <center><h1>403 Forbidden</h1></center> <hr><center>nginx/1.14.0</center> </body> </html> [root@centos-03 vhost]# curl -x127.0.0.1:80 www.1.com www.1.com [root@centos-03 vhost]#
7.泛解析server配置全部的xxx.1.com類型訪問都會到1.com/index.html上
server {
listen 80;
server_name *.1.com;
root /data/wwwroot/1.com;
}
8.基於端口的虛擬主機
[root@centos-03 vhost]# cp 1.conf 2.conf
[root@centos-03 vhost]# vim 2.conf ^C [root@centos-03 vhost]# cat 2.conf server { listen 8080; server_name www.1.com; index index.html; root /data/wwwroot/www.1.com_8080; } [root@centos-03 vhost]#
[root@centos-03 vhost]# mkdir /data/wwwroot/www.1.com_8080
[root@centos-03 vhost]# vim /data/wwwroot/www.1.com_8080/index.html^C [root@centos-03 vhost]# cat !$ cat /data/wwwroot/www.1.com_8080/index.html 8080 [root@centos-03 vhost]#
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -s reload [root@centos-03 vhost]#
[root@centos-03 vhost]# curl -x127.0.0.1:8080 www.1.com 8080 [root@centos-03 vhost]# curl -x127.0.0.1:80 www.1.com www.1.com [root@centos-03 vhost]#
https://coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/if.md
1.if指令條件判斷語句由Nginx內置變量、邏輯判斷符號和目標字符串三部分組成。 其中,內置變量是Nginx固定的非自定義的變量,如,$request_method, $request_uri等。 邏輯判斷符號,有=, !=, ~, ~*, !~, !~* !表示相反的意思,~爲匹配符號,它右側爲正則表達式,區分大小寫,而~*爲不區分大小寫匹配。 目標字符串能夠是正則表達式,一般不用加引號,但表達式中有特殊符號時,好比空格、花括號、分號等,須要用單引號引發來。
if ($request_method = POST) //當請求的方法爲POST時,直接返回405狀態碼 { return 405; //在該示例中並未用到rewrite規則,if中支持用return指令。 } if ($http_user_agent ~ MSIE) //user_agent帶有MSIE字符的請求,直接返回403狀態碼 { return 403; } 若是想同時限制多個user_agent,還能夠寫成這樣 if ($http_user_agent ~ "MSIE|firefox|spider") { return 403; } if(!-f $request_filename) //當請求的文件不存在,將會執行下面的rewrite規則 { rewrite 語句; } if($request_uri ~* 'gid=\d{9,12}/') //\d表示數字,{9,12}表示數字出現的次數是9到12次,如gid=123456789/就是符合條件的。 { rewrite 語句; }
https://coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/break.md
兩個指令用法相同,但含義不一樣,須要放到rewrite規則的末尾,用來控制重寫後的連接是否繼續被nginx配置執行(主要是rewrite、return指令)。 示例1(連續兩條rewrite規則): server{ listen 80; server_name test.com; root /tmp/123.com; rewrite /1.html /2.html ; rewrite /2.html /3.html ; } 當咱們請求1.html時,最終訪問到的是3.html,兩條rewrite規則前後執行。
1.咱們用1.conf虛擬機作實驗,建立1.html、2.html、3.html
[root@centos-03 vhost]# cd /data/wwwroot/www.1.com [root@centos-03 www.1.com]# ls index.html [root@centos-03 www.1.com]# touch 1.html 2.html 3.html [root@centos-03 www.1.com]# ls 1.html 2.html 3.html index.html [root@centos-03 www.1.com]# echo 111 > 1.html [root@centos-03 www.1.com]# echo 222 > 2.html [root@centos-03 www.1.com]# echo 333 > 3.html [root@centos-03 www.1.com]#
2.配置1.conf,開啓rewrite日誌添加rewrite規則,經過日誌記錄咱們查看rewrite執行過程,開啓error_log notice級別
[root@centos-03 www.1.com]# cd /usr/local/nginx/conf/ [root@centos-03 conf]# vim vhost/1.conf [root@centos-03 conf]# cat vhost/1.conf server { listen 80; server_name www.1.com; index index.html; root /data/wwwroot/www.1.com; rewrite_log on; rewrite /1.html /2.html; (這樣會連續執行rewrite規則) rewrite /2.html /3.html; } [root@centos-03 conf]#
[root@centos-03 conf]# vim nginx.conf
#error_log logs/error.log;
error_log logs/error.log notice;
#error_log logs/error.log info;
[root@centos-03 conf]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@centos-03 conf]# /usr/local/nginx/sbin/nginx -s reload [root@centos-03 conf]# curl -x127.0.0.1:80 www.1.com/1.html 333 [root@centos-03 conf]#
[root@centos-03 conf]# less ../logs/error.log 2018/07/26 17:33:21 [notice] 11371#0: *27 "/1.html" matches "/1.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com" 2018/07/26 17:33:21 [notice] 11371#0: *27 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com" 2018/07/26 17:33:21 [notice] 11371#0: *27 "/2.html" matches "/2.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com" 2018/07/26 17:33:21 [notice] 11371#0: *27 rewritten data: "/3.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com" (END)
3.咱們想讓執行完一個rewrite後就終止能夠用break或last,這樣匹配到222就不往下匹配了
[root@centos-03 conf]# vim vhost/1.conf [root@centos-03 conf]# cat vhost/1.conf server { listen 80; server_name www.1.com; index index.html; root /data/wwwroot/www.1.com; rewrite_log on; rewrite /1.html /2.html last; rewrite /2.html /3.html; } [root@centos-03 conf]# [root@centos-03 conf]# /usr/local/nginx/sbin/nginx -s reload [root@centos-03 conf]# !curl curl -x127.0.0.1:80 www.1.com/1.html 222 [root@centos-03 conf]#
[root@centos-03 conf]# tail ../logs/error.log 2018/07/26 18:43:07 [notice] 9429#0: start worker processes 2018/07/26 18:43:07 [notice] 9429#0: start worker process 11428 2018/07/26 18:43:07 [notice] 11371#0: gracefully shutting down 2018/07/26 18:43:07 [notice] 11371#0: exiting 2018/07/26 18:43:07 [notice] 11371#0: exit 2018/07/26 18:43:07 [notice] 9429#0: signal 17 (SIGCHLD) received from 11371 2018/07/26 18:43:07 [notice] 9429#0: worker process 11371 exited with code 0 2018/07/26 18:43:07 [notice] 9429#0: signal 29 (SIGIO) received 2018/07/26 18:43:15 [notice] 11428#0: *28 "/1.html" matches "/1.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com" 2018/07/26 18:43:15 [notice] 11428#0: *28 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com" [root@centos-03 conf]#
4.當使用了location的時候last和break不同,不加break和last的狀況
[root@centos-03 conf]# vim vhost/1.conf ^C [root@centos-03 conf]# cat vhost/1.conf server { listen 80; server_name www.1.com; index index.html; root /data/wwwroot/www.1.com; rewrite_log on; location / { rewrite /1.html /2.html; rewrite /2.html /3.html; } location /2.html { rewrite /2.html /a.html; } location /3.html { rewrite /3.html /b.html; } } [root@centos-03 conf]#
[root@centos-03 conf]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@centos-03 conf]# /usr/local/nginx/sbin/nginx -s reload [root@centos-03 conf]# !curl curl -x127.0.0.1:80 www.1.com/1.html <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.14.0</center> </body> </html> [root@centos-03 conf]#
[root@centos-03 conf]# !tail tail ../logs/error.log 2018/07/26 19:24:53 [notice] 9429#0: signal 29 (SIGIO) received 2018/07/26 19:25:02 [notice] 11465#0: *29 "/1.html" matches "/1.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com" 2018/07/26 19:25:02 [notice] 11465#0: *29 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com" 2018/07/26 19:25:02 [notice] 11465#0: *29 "/2.html" matches "/2.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com" 2018/07/26 19:25:02 [notice] 11465#0: *29 rewritten data: "/3.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com" 2018/07/26 19:25:02 [notice] 11465#0: *29 "/3.html" matches "/3.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com" 2018/07/26 19:25:02 [notice] 11465#0: *29 rewritten data: "/b.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com" 2018/07/26 19:25:02 [notice] 11465#0: *29 "/1.html" does not match "/b.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com" 2018/07/26 19:25:02 [notice] 11465#0: *29 "/2.html" does not match "/b.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com" 2018/07/26 19:25:02 [error] 11465#0: *29 open() "/data/wwwroot/www.1.com/b.html" failed (2: No such file or directory), client: 127.0.0.1, server: www.1.com, request:
"GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com" [root@centos-03 conf]#
5.加上break的狀況
[root@centos-03 conf]# vim vhost/1.conf ^C [root@centos-03 conf]# cat vhost/1.conf server { listen 80; server_name www.1.com; index index.html; root /data/wwwroot/www.1.com; rewrite_log on; location / { rewrite /1.html /2.html break; rewrite /2.html /3.html; } location /2.html { rewrite /2.html /a.html; } location /3.html { rewrite /3.html /b.html; } } [root@centos-03 conf]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@centos-03 conf]# /usr/local/nginx/sbin/nginx -s reload [root@centos-03 conf]# !curl curl -x127.0.0.1:80 www.1.com/1.html 222 [root@centos-03 conf]# !tail tail ../logs/error.log 2018/07/26 19:36:11 [notice] 9429#0: start worker processes 2018/07/26 19:36:11 [notice] 9429#0: start worker process 11475 2018/07/26 19:36:11 [notice] 11465#0: gracefully shutting down 2018/07/26 19:36:11 [notice] 11465#0: exiting 2018/07/26 19:36:11 [notice] 11465#0: exit 2018/07/26 19:36:11 [notice] 9429#0: signal 17 (SIGCHLD) received from 11465 2018/07/26 19:36:11 [notice] 9429#0: worker process 11465 exited with code 0 2018/07/26 19:36:11 [notice] 9429#0: signal 29 (SIGIO) received 2018/07/26 19:36:19 [notice] 11475#0: *30 "/1.html" matches "/1.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com" 2018/07/26 19:36:19 [notice] 11475#0: *30 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com" [root@centos-03 conf]#
6.把break改爲last試試
[root@centos-03 conf]# vim vhost/1.conf ^C [root@centos-03 conf]# cat vhost/1.conf server { listen 80; server_name www.1.com; index index.html; root /data/wwwroot/www.1.com; rewrite_log on; location / { rewrite /1.html /2.html last; rewrite /2.html /3.html; } location /2.html { rewrite /2.html /a.html; } location /3.html { rewrite /3.html /b.html; } } [root@centos-03 conf]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@centos-03 conf]# /usr/local/nginx/sbin/nginx -s reload [root@centos-03 conf]# !curl curl -x127.0.0.1:80 www.1.com/1.html <html> <head><title>404 Not Found</title></head> <body bgcolor="white"> <center><h1>404 Not Found</h1></center> <hr><center>nginx/1.14.0</center> </body> </html> [root@centos-03 conf]# !tail tail ../logs/error.log 2018/07/26 19:39:05 [notice] 9429#0: signal 17 (SIGCHLD) received from 11475 2018/07/26 19:39:05 [notice] 9429#0: worker process 11475 exited with code 0 2018/07/26 19:39:05 [notice] 9429#0: signal 29 (SIGIO) received 2018/07/26 19:39:12 [notice] 11484#0: *31 "/1.html" matches "/1.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com" 2018/07/26 19:39:12 [notice] 11484#0: *31 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com" 2018/07/26 19:39:12 [notice] 11484#0: *31 "/2.html" matches "/2.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com" 2018/07/26 19:39:12 [notice] 11484#0: *31 rewritten data: "/a.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com" 2018/07/26 19:39:12 [notice] 11484#0: *31 "/1.html" does not match "/a.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com" 2018/07/26 19:39:12 [notice] 11484#0: *31 "/2.html" does not match "/a.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com" 2018/07/26 19:39:12 [error] 11484#0: *31 open() "/data/wwwroot/www.1.com/a.html" failed (2: No such file or directory), client: 127.0.0.1, server: www.1.com, request:
"GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com" [root@centos-03 conf]#
https://coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/return.md
該指令通常用於對請求的客戶端直接返回響應狀態碼。在該做用域內return後面的全部nginx配置都是無效的。 可使用在server、location以及if配置中。 除了支持跟狀態碼,還能夠跟字符串或者url連接。
1.直接返回狀態碼
[root@centos-03 conf]# vim vhost/default.conf ^C [root@centos-03 conf]# cat vhost/default.conf server { listen 80 default_server; return 403; } [root@centos-03 conf]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@centos-03 conf]# /usr/local/nginx/sbin/nginx -s reload [root@centos-03 conf]# curl -x127.0.0.1:80 fjldsfjdsajfl <html> <head><title>403 Forbidden</title></head> <body bgcolor="white"> <center><h1>403 Forbidden</h1></center> <hr><center>nginx/1.14.0</center> </body> </html> [root@centos-03 conf]# curl -x127.0.0.1:80 fjldsfjdsajfl -I HTTP/1.1 403 Forbidden Server: nginx/1.14.0 Date: Thu, 26 Jul 2018 12:42:18 GMT Content-Type: text/html Content-Length: 169 Connection: keep-alive [root@centos-03 conf]#
2.在if中使用return直接返回404
[root@centos-03 conf]# vim vhost/1.conf ^C [root@centos-03 conf]# cat vhost/1.conf server { listen 80; server_name www.1.com; index index.html; root /data/wwwroot/www.1.com; rewrite_log on; if ($request_uri ~ "\.htpasswd|\.bak") { return 404; rewrite /(.*) /aaa.txt; #該行配置不會被執行。 } } [root@centos-03 conf]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@centos-03 conf]# /usr/local/nginx/sbin/nginx -s reload [root@centos-03 conf]# curl -x127.0.0.1:80 www.1.com/12/.htpasswd -I HTTP/1.1 404 Not Found Server: nginx/1.14.0 Date: Thu, 26 Jul 2018 12:50:11 GMT Content-Type: text/html Content-Length: 169 Connection: keep-alive [root@centos-03 conf]#
3.返回字符串
[root@centos-03 conf]# vim vhost/1.conf ^C [root@centos-03 conf]# cat vhost/1.conf server { listen 80; server_name www.1.com; index index.html; root /data/wwwroot/www.1.com; rewrite_log on; if ($request_uri ~ "\.htpasswd|\.bak") { return 200 "ok"; rewrite /(.*) /aaa.txt; #該行配置不會被執行。 } } [root@centos-03 conf]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@centos-03 conf]# /usr/local/nginx/sbin/nginx -s reload [root@centos-03 conf]# curl -x127.0.0.1:80 www.1.com/12/.htpasswd -I HTTP/1.1 200 OK Server: nginx/1.14.0 Date: Thu, 26 Jul 2018 12:53:53 GMT Content-Type: application/octet-stream Content-Length: 2 Connection: keep-alive [root@centos-03 conf]# curl -x127.0.0.1:80 www.1.com/12/.htpasswd ok[root@centos-03 conf]#
4.返回url
[root@centos-03 conf]# vim vhost/1.conf ^C [root@centos-03 conf]# cat vhost/1.conf server { listen 80; server_name www.1.com; index index.html; root /data/wwwroot/www.1.com; rewrite_log on; if ($request_uri ~ "\.htpasswd|\.bak") { return 200 "<html><script>window.location.href='//$host$request_uri';</script></html>"; rewrite /(.*) /aaa.txt; #該行配置不會被執行。 } } [root@centos-03 conf]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@centos-03 conf]# /usr/local/nginx/sbin/nginx -s reload [root@centos-03 conf]# curl -x127.0.0.1:80 www.1.com/12/.htpasswd <html><script>window.location.href='//www.1.com/12/.htpasswd';</script></html>[root@centos-03 conf]# curl -x127.0.0.1:80 www.1.com/12/.htpasswd -I HTTP/1.1 200 OK Server: nginx/1.14.0 Date: Thu, 26 Jul 2018 13:07:28 GMT Content-Type: application/octet-stream Content-Length: 78 Connection: keep-alive [root@centos-03 conf]#
5.301跳轉
[root@centos-03 conf]# vim vhost/1.conf ^C [root@centos-03 conf]# cat vhost/1.conf server { listen 80; server_name www.1.com; index index.html; root /data/wwwroot/www.1.com; rewrite_log on; if ($request_uri ~ "\.htpasswd|\.bak") { return 301 http://www.baidu.com; rewrite /(.*) /aaa.txt; #該行配置不會被執行。 } } [root@centos-03 conf]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@centos-03 conf]# /usr/local/nginx/sbin/nginx -s reload [root@centos-03 conf]# curl -x127.0.0.1:80 www.1.com/12/.htpasswd -I HTTP/1.1 301 Moved Permanently Server: nginx/1.14.0 Date: Thu, 26 Jul 2018 13:12:38 GMT Content-Type: text/html Content-Length: 185 Connection: keep-alive Location: http://www.baidu.com [root@centos-03 conf]#
https://coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/rewrite_ruler.md
1.格式:rewrite regex replacement [flag]
* rewrite配置能夠在server、location以及if配置段內生效 * regex是用於匹配URI的正則表達式,其不會匹配到$host(域名) * replacement是目標跳轉的URI,能夠以http://或者https://開頭,也能夠省略掉$host,直接寫$request_uri部分(即請求的連接) * flag,用來設置rewrite對URI的處理行爲,其中有break、last、rediect、permanent,其中break和last在前面已經介紹過, rediect和permanent的區別在於,前者爲臨時重定向(302),然後者是永久重定向(301),對於用戶經過瀏覽器訪問,這二者的效果是一致的。 可是,對於搜索引擎蜘蛛爬蟲來講就有區別了,使用301更有利於SEO。因此,建議replacemnet是以http://或者https://開頭的flag使用permanent。
例一:
location / { rewrite /(.*) http://www.aming.com/$1 permanent; } 說明:.*爲正則表達式,用()括起來,在後面的URI中能夠調用它,第一次出現的()用$1調用,第二次出現的()用$2調用,以此類推。
例二:
location / { rewrite /.* http://www.aming.com$request_uri permanent; } 說明:在replacement中,支持變量,這裏的$request_uri就是客戶端請求的連接
例三:
server{ listen 80; server_name www.123.com; root /tmp/123.com; index index.html; rewrite /(.*) /abc/$1 redirect; } 說明:本例中的rewrite規則有問題,會造連續循環,最終會失敗,解決該問題有兩個方案。 關於循環次數,經測試發現,curl 會循環50次,chrome會循環80次,IE會循環120次,firefox會循環20次。
server{ listen 80; server_name www.123.com; root /tmp/123.com; index index.html; rewrite /(.*) /abc/$1 break; } 說明:在rewrite中使用break,會避免循環。
server{ listen 80; server_name www.123.com; root /tmp/123.com; index index.html; if ($request_uri !~ '^/abc/') { rewrite /(.*) /abc/$1 redirect; } } 說明:加一個條件限制,也能夠避免產生循環
https://coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/variable.md
$args 請求中的參數,如www.123.com/1.php?a=1&b=2的$args就是a=1&b=2 $content_length HTTP請求信息裏的」Content-Length」 $conten_type HTTP請求信息裏的」Content-Type」 $document_root nginx虛擬主機配置文件中的root參數對應的值 $document_uri 當前請求中不包含指令的URI,如www.123.com/1.php?a=1&b=2的$document_uri就是1.php,不包含後面的參數 $host 主機頭,也就是域名 $http_user_agent 客戶端的詳細信息,也就是瀏覽器的標識,用curl -A能夠指定 $http_cookie 客戶端的cookie信息 $limit_rate 若是nginx服務器使用limit_rate配置了顯示網絡速率,則會顯示,若是沒有設置, 則顯示0 $remote_addr 客戶端的公網ip $remote_port 客戶端的port $remote_user 若是nginx有配置認證,該變量表明客戶端認證的用戶名 $request_body_file 作反向代理時發給後端服務器的本地資源的名稱 $request_method 請求資源的方式,GET/PUT/DELETE等 $request_filename 當前請求的資源文件的路徑名稱,至關因而$document_root/$document_uri的組合 $request_uri 請求的連接,包括$document_uri和$args $scheme 請求的協議,如ftp,http,https $server_protocol 客戶端請求資源使用的協議的版本,如HTTP/1.0,HTTP/1.1,HTTP/2.0等 $server_addr 服務器IP地址 $server_name 服務器的主機名 $server_port 服務器的端口號 $uri 和$document_uri相同 $http_referer 客戶端請求時的referer,通俗講就是該請求是經過哪一個連接跳過來的,用curl -e能夠指定
1.$args
[root@centos-03 vhost]# vim 1.conf ^C [root@centos-03 vhost]# cat 1.conf server { listen 80; server_name www.1.com; index index.html; root /data/wwwroot/www.1.com; return 200 "$args"; } [root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -s reload [root@centos-03 vhost]# curl -x127.0.0.1:80 'www.1.com/1.php?a=1&b=2' a=1&b=2[root@centos-03 vhost]#
2.$content_length
[root@centos-03 vhost]# curl -x127.0.0.1:80 'www.1.com/1.php?a=1&b=2' -I HTTP/1.1 200 OK Server: nginx/1.14.0 Date: Thu, 26 Jul 2018 13:48:13 GMT Content-Type: application/octet-stream Content-Length: 7 Connection: keep-alive [root@centos-03 vhost]#
3.$http_user_agent
[root@centos-03 vhost]# vim 1.conf ^C [root@centos-03 vhost]# cat 1.conf server { listen 80; server_name www.1.com; index index.html; root /data/wwwroot/www.1.com; return 200 "$http_user_agent"; } [root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -s reload [root@centos-03 vhost]# curl -x127.0.0.1:80 'www.1.com/1.php?a=1&b=2' curl/7.29.0[root@centos-03 vhost]# curl -A "USERANGET" -x127.0.0.1:80 'www.1.com/1.php?a=1&b=2' USERANGET[root@centos-03 vhost]#
[root@centos-03 vhost]# vim 1.conf ^C
[root@centos-03 vhost]# cat 1.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
rewrite /(.*) http://www.baidu.com/$1 permanent;
access_log /tmp/1.log;
}
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@centos-03 vhost]# curl -x127.0.0.1:80 'www.1.com/a'
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.14.0</center>
</body>
</html>
[root@centos-03 vhost]# curl -x127.0.0.1:80 'www.1.com/a' -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.0
Date: Thu, 26 Jul 2018 14:21:52 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://www.baidu.com/a
[root@centos-03 vhost]#
示例2(帶條件的): server{ listen 80; server_name www.aminglinux.com aminglinux.com; if ($host != 'www.aminglinux.com') { rewrite /(.*) http://www.aminglinux.com/$1 permanent; } ....... } 示例3(http跳轉到https): server{ listen 80; server_name www.aminglinux.com; rewrite /(.*) https://www.aminglinux.com/$1 permanent; ....... } 示例4(域名訪問二級目錄) server{ listen 80; server_name bbs.aminglinux.com; rewrite /(.*) http://www.aminglinux.com/bbs/$1 last; ....... } 示例5(靜態請求分離) server{ listen 80; server_name www.aminglinux.com; location ~* ^.+.(jpg|jpeg|gif|css|png|js)$ { rewrite /(.*) http://img.aminglinux.com/$1 permanent; } ....... } 或者: server{ listen 80; server_name www.aminglinux.com; if ( $uri ~* 'jpg|jpeg|gif|css|png|js$') { rewrite /(.*) http://img.aminglinux.com/$1 permanent; } ....... }
[root@centos-03 vhost]# vim 1.conf ^C
[root@centos-03 vhost]# cat 1.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
if ( $uri ~* (jpg|gif|jpeg)$){
rewrite /(.*) http://www.baidu.com/$1 permanent;
}
access_log /tmp/1.log;
}
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@centos-03 vhost]# curl -x127.0.0.1:80 'www.1.com/1.jpg' -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.14.0
Date: Thu, 26 Jul 2018 14:44:15 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://www.baidu.com/1.jpg
[root@centos-03 vhost]#
示例6 server{ listen 80; server_name www.aminglinux.com; location ~* ^.+.(jpg|jpeg|gif|css|png|js|rar|zip|flv)$ { valid_referers none blocked server_names *.aminglinux.com aminglinux.com *.aming.com aming.com; if ($invalid_referer) { rewrite /(.*) http://img.aminglinux.com/images/forbidden.png; } } ....... } 說明:*這裏是通配,跟正則裏面的*不是一個意思,none指的是referer不存在的狀況(curl -e 測試), blocked指的是referer頭部的值被防火牆或者代理服務器刪除或者假裝的狀況, 該狀況下,referer頭部的值不以http://或者https://開頭(curl -e 後面跟的referer不以http://或者https://開頭)。 或者: location ~* ^.+.(jpg|jpeg|gif|css|png|js|rar|zip|flv)$ { valid_referers none blocked server_names *.aminglinux.com *.aming.com aminglinux.com aming.com; if ($invalid_referer) { return 403; } }
1.當咱們用域名www.1.com訪問時返回404正確,用www.2.com訪問返回403沒權限
[root@centos-03 vhost]# vim 1.conf ^C [root@centos-03 vhost]# cat 1.conf server { listen 80; server_name www.1.com; index index.html; root /data/wwwroot/www.1.com; access_log /tmp/1.log; location ~* ^.+.(jpg|jpeg|gif|css|png|js|rar|zip|flv)$ { valid_referers none blocked server_names *.1.com 1.com; (白名單域名) if ($invalid_referer) { return 403; } } } [root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -s reload [root@centos-03 vhost]# curl -x127.0.0.1:80 'www.1.com/1.jpg' -I HTTP/1.1 404 Not Found Server: nginx/1.14.0 Date: Thu, 26 Jul 2018 15:13:06 GMT Content-Type: text/html Content-Length: 169 Connection: keep-alive [root@centos-03 vhost]# curl -x127.0.0.1:80 'www.2.com/1.jpg' -I HTTP/1.1 403 Forbidden Server: nginx/1.14.0 Date: Thu, 26 Jul 2018 15:13:48 GMT Content-Type: text/html Content-Length: 169 Connection: keep-alive [root@centos-03 vhost]#
[root@centos-03 vhost]# curl -e "http://www.1.com" -x127.0.0.1:80 'www.1.com/1.jpg' -I HTTP/1.1 404 Not Found Server: nginx/1.14.0 Date: Thu, 26 Jul 2018 15:19:00 GMT Content-Type: text/html Content-Length: 169 Connection: keep-alive [root@centos-03 vhost]# curl -e "http://www.2.com" -x127.0.0.1:80 'www.1.com/1.jpg' -I HTTP/1.1 403 Forbidden Server: nginx/1.14.0 Date: Thu, 26 Jul 2018 15:19:09 GMT Content-Type: text/html Content-Length: 169 Connection: keep-alive [root@centos-03 vhost]#
示例7(discuz僞靜態): location / { rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last; rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last; rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last; rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last; rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last; rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/index.php?action=$2&value=$3 last; }
示例8: location /{ set $rule 0; if ($document_uri !~ '^/abc') { set $rule "${rule}1"; } if ($http_user_agent ~* 'ie6|firefox') { set $rule "${rule}2"; } if ($rule = "012") { rewrite /(.*) /abc/$1 redirect; } }
[root@centos-03 vhost]# vim 1.conf ^C
[root@centos-03 vhost]# cat 1.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
access_log /tmp/1.log;
location ~* ^.+.(jpg|jpeg|gif|css|png|js|rar|zip|flv)$
{
valid_referers none blocked server_names *.1.com 1.com;
if ($invalid_referer)
{
return 403;
}
}
set $a 0;
if ($request_uri !~ "^/abc/")
{
set $a "${a}1";
}
if ($http_user_agent ~ 'IE|chrome')
{
set $a "${a}2";
}
if ($a = "012")
{
return 406;
}
}
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@centos-03 vhost]# curl -x127.0.0.1:80 -A "chrome" www.1.com/1.html -I
HTTP/1.1 406 Not Acceptable
Server: nginx/1.14.0
Date: Thu, 26 Jul 2018 15:46:05 GMT
Content-Type: text/html
Content-Length: 179
Connection: keep-alive
[root@centos-03 vhost]#
[root@centos-03 vhost]# curl -x127.0.0.1:80 -A "chrome" www.1.com/abc/1.html -I
HTTP/1.1 404 Not Found
Server: nginx/1.14.0
Date: Thu, 26 Jul 2018 15:50:34 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
[root@centos-03 vhost]#
https://coding.net/u/aminglinux/p/nginx/git/blob/master/location/ruler.md
1.安裝echo-nginx-module模塊,這樣咱們就可使用echo命令了,首先用git把源碼克隆下來
[root@centos-03 src]# cd /usr/local/src/ [root@centos-03 src]# yum install -y git [root@centos-03 src]# git clone https://github.com/openresty/echo-nginx-module.git 正克隆到 'echo-nginx-module'... remote: Counting objects: 2991, done. remote: Total 2991 (delta 0), reused 0 (delta 0), pack-reused 2991 接收對象中: 100% (2991/2991), 1.13 MiB | 152.00 KiB/s, done. 處理 delta 中: 100% (1607/1607), done. [root@centos-03 src]#
[root@centos-03 src]# ls echo-nginx-module filebeat-6.3.1-x86_64.rpm nginx-1.14.0 nginx-1.14.0.tar.gz [root@centos-03 src]# cd nginx-1.14.0 [root@centos-03 nginx-1.14.0]# make clean rm -rf Makefile objs [root@centos-03 nginx-1.14.0]# /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.14.0 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) configure arguments: --prefix=/usr/local/nginx [root@centos-03 nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --add-module=/usr/local/src/echo-nginx-module/
[root@centos-03 nginx-1.14.0]# make && make install
[root@centos-03 nginx-1.14.0]# /etc/init.d/nginx restart Restarting nginx (via systemctl): [ 肯定 ] [root@centos-03 nginx-1.14.0]# /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.14.0 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) configure arguments: --prefix=/usr/local/nginx --add-module=/usr/local/src/echo-nginx-module/ [root@centos-03 nginx-1.14.0]#
2.測試echo命令是否能用
[root@centos-03 vhost]# vim 1.conf ^C [root@centos-03 vhost]# cat 1.conf server { listen 80; server_name www.1.com; index index.html; root /data/wwwroot/www.1.com; access_log /tmp/1.log; location /abc/ { echo 123; } } [root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -s reload [root@centos-03 vhost]# curl -x127.0.0.1:80 www.1.com/abc/www 123 [root@centos-03 vhost]#
location語法
nginx location語法規則:location [=|~|~*|^~] /uri/ { … } nginx的location匹配的變量是$uri
符號 說明 = 表示精確匹配 ^~ 表示uri以指定字符或字符串開頭 ~ 表示區分大小寫的正則匹配 ~* 表示不區分大小寫的正則匹配 / 通用匹配,任何請求都會匹配到
= 高於 ^~ 高於 ~* 等於 ~ 高於 /
location = "/12.jpg" { ... } 如: www.aminglinux.com/12.jpg 匹配 www.aminglinux.com/abc/12.jpg 不匹配 location ^~ "/abc/" { ... } 如: www.aminglinux.com/abc/123.html 匹配 www.aminglinux.com/a/abc/123.jpg 不匹配 location ~ "png" { ... } 如: www.aminglinux.com/aaa/bbb/ccc/123.png 匹配 www.aminglinux.com/aaa/png/123.html 匹配 location ~* "png" { ... } 如: www.aminglinux.com/aaa/bbb/ccc/123.PNG 匹配 www.aminglinux.com/aaa/png/123.html 匹配 location /admin/ { ... } 如: www.aminglinux.com/admin/aaa/1.php 匹配 www.aminglinux.com/123/admin/1.php 不匹配
有些資料上介紹location支持不匹配 !~, 如: location !~ 'png'{ ... } 這是錯誤的,location不支持 !~ 若是有這樣的需求,能夠經過if來實現, 如: if ($uri !~ 'png') { ... } 注意:location優先級小於if
1.echo 在生產環境下不用,只是用作測試
優先級測試域名會匹配到兩個location規則因爲~*的優先級高於/優先級,因此最終匹配到了~*
[root@centos-03 vhost]# vim 1.conf ^C
[root@centos-03 vhost]# cat 1.conf
server {
listen 80;
server_name www.1.com;
index index.html;
root /data/wwwroot/www.1.com;
access_log /tmp/1.log;
location /abc/
{
echo "/";
}
location ~* abc
{
echo "~*";
}
}
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@centos-03 vhost]# curl -x127.0.0.1:80 www.1.com/abc/www
~*
[root@centos-03 vhost]#
[root@centos-03 vhost]# vim 1.conf ^C (^~優先級高於~*) [root@centos-03 vhost]# cat 1.conf server { listen 80; server_name www.1.com; index index.html; root /data/wwwroot/www.1.com; access_log /tmp/1.log; location ^~ /abc { echo "^~"; } location ~* abc { echo "~*"; } } [root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -s reload [root@centos-03 vhost]# curl -x127.0.0.1:80 www.1.com/abc/www ^~ [root@centos-03 vhost]#
[root@centos-03 vhost]# vim 1.conf ^C (等於號優先級高於^~) [root@centos-03 vhost]# cat 1.conf server { listen 80; server_name www.1.com; index index.html; root /data/wwwroot/www.1.com; access_log /tmp/1.log; location ^~ /abc { echo "^~"; } location = /abc/1.html { echo "="; } } [root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -s reload [root@centos-03 vhost]# curl -x127.0.0.1:80 www.1.com/abc/1.html = [root@centos-03 vhost]#