安裝第三方模塊echo-nginx-module
//安裝該模塊,可以支持在nginx配置文件中支持echo命令
git clone https://github.com/openresty/echo-nginx-module.git
//到Nginx安裝目錄中,make clean
[root@localhost src]# wget https://github.com/openresty/echo-nginx-module/archive/v0.6.tar.gz
[root@localhost src]# tar xzf v0.6.tar.gz
[root@localhost src]# ls
echo-nginx-module-0.6 nginx-1.14.0 nginx-1.14.0.tar.gz v0.6.tar.gz
[root@localhost ~]# /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@localhost nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --add-module=/usr/local/src/echo-nginx-module
[root@localhost nginx-1.14.0]# make && make install
//echo 要在location語句中使用
location /abc/{
echo 123
}
nginx location語法規則:location [=|~|~*|^~] /uri/ { … }
nginx的location匹配的變量是$uri
| 符號 | 說明 |
| = | 表示精確匹配 |
| ^~ | 表示uri以指定字符或字符串開頭 |
| ~ | 表示區分大小寫的正則匹配 |
| ~* | 表示不區分大小寫的正則匹配 |
| / | 通用匹配,任何請求都會匹配到 |
規則優先級
= 高於 ^~ 高於 ~* 等於 ~ 高於 /
規則示例
location = "/12.jpg" { ... }
如:
www.123.com/12.jpg 匹配
www.123.com/abc/12.jpg 不匹配
location ^~ "/abc/" { ... }
如:
www.123.com/abc/123.html 匹配
www.123.com/a/abc/123.jpg 不匹配
location ~ "png" { ... }
如:
www.123.com/aaa/bbb/ccc/123.png 匹配
www.123.com/aaa/png/123.html 匹配
location ~* "png" { ... }
如:
www.123.com/aaa/bbb/ccc/123.PNG 匹配
www.123.com/aaa/png/123.html 匹配
location /admin/ { ... }
如:
www.123.com/admin/aaa/1.php 匹配
www.123.com/123/admin/1.php 不匹配
有些資料上介紹location支持不匹配 !~,
如: location !~ 'png'{ ... }
這是錯誤的,location不支持 !~
如果有這樣的需求,可以通過if來實現,
如: if ($uri !~ 'png') { ... }
注意:location優先級小於if
