調試nginx匹配規則的一個思路
思路簡介:通過rewrite方法,將匹配結果rewrite爲指定內容,根據最終結果達到日誌輸出的效果
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 9089;
server_name localhost;
# root /usr/local/html;
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
# root /usr/local/html;
#}
location = /abc {
root /usr/local/html;
rewrite '^/[a-z].*' /a;
break;
}
location /abc {
root /usr/local/html;
rewrite '^/[a-z].*' /b;
break;
}
location /abcd {
root /usr/local/html;
rewrite '^/[a-z].*' /c;
break;
}
location ^~ /abcde {
root /usr/local/html;
rewrite '^/[a-z].*' /d;
break;
}
location ~ /abcdef {
root /usr/local/html;
rewrite '^/[a-z].*' /e;
break;
}
location ~* /abcdefg {
root /usr/local/html;
rewrite '^/[a-z].*' /f;
break;
}
location ~* /abcdf {
root /usr/local/html;
rewrite '^/[a-z].*' /g;
break;
}
location ~ /abcdfgh {
root /usr/local/html;
rewrite '^/[a-z].*' /h;
break;
}
location ~ /abcdgh {
root /usr/local/html;
rewrite '^/[a-z].*' /i;
break;
}
location ~* /abcdgh {
root /usr/local/html;
rewrite '^/[a-z].*' /j;
break;
}
location ~* \.txt {
root /usr/local/html;
}
location ^~ /ffhh {
root /usr/local/html;
rewrite '^/[fh].*' /k;
break;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
結果頁a的內容:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>welcome</title>
</head>
<body>
<h1>----------------- a -----------------</h1>
<h1>hello world ! where are you from ?</h1>
</body>
</html>
結果頁b的內容:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>welcome</title>
</head>
<body>
<h1>----------------- b -----------------</h1>
<h1>hello world ! where are you from ?</h1>
</body>
</html>
其他c、d、e等結果頁的內容類似
對以上配置的執行結果示例:
curl localhost:9089/abc // a
curl localhost:9089/abcll // b
curl localhost:9089/abcdll // c
curl localhost:9089/abcde // d
curl localhost:9089/abcdef // d
curl localhost:9089/abcdefll // d
頁面e、f是不可能匹配到的了,因爲都被d攔截了
curl localhost:9089/abcdf // g
curl localhost:9089/abcdfgh // g
頁面h也是不可能匹配到的了,因爲都被g攔截了
curl localhost:9089/abcdgh // i
頁面j也是不可能匹配到的了,因爲都被i攔截了
附錄:
以下內容引用至nginx官方文檔
A location can either be defined by a prefix string, or by a regular expression. Regular expressions are specified with the preceding “~*” modifier (for case-insensitive matching), or the “~” modifier (for case-sensitive matching). To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered. Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used.
location blocks can be nested, with some exceptions mentioned below.
For case-insensitive operating systems such as macOS and Cygwin, matching with prefix strings ignores a case (0.7.7). However, comparison is limited to one-byte locales.
Regular expressions can contain captures (0.7.40) that can later be used in other directives.
If the longest matching prefix location has the “^~” modifier then regular expressions are not checked.
Also, using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates. For example, if a “/” request happens frequently, defining “location = /” will speed up the processing of these requests, as search terminates right after the first comparison. Such a location cannot obviously contain nested locations.
翻譯總結一下location的匹配規則:
location的匹配方式可以通過字符串前綴或正則表達式的形式表示。當接收到一個請求時,nginx將按以下步驟來選擇location:
1、nginx先按字符串前綴的形式查找匹配的location,選中並記住匹配度最長的那個location。
2、如果被選中的location使用了“=”修飾符,則將當前選中結果作爲最終結果,停止繼續查找;
3、如果被選中的location使用了“^~”修飾符,則將當前選中結果作爲最終結果,停止繼續查找;
4、如果被選中的location未使用“=”或“^~”修飾符,則繼續查找,執行步驟5;
5、然後,再按照正則的定義順序查找匹配的location,選中第一個符合要求的正則location。