43 Nginx的location指令
43 Nginx的location指令
43.1 location指令
| 语法 | location [ 空 | = | ~ | ~* | ^~ | @ ] uri{...} |
| 默认值 | _ |
| 位置 | server.location |
uri变量是待匹配的请求字符串,可以不包含正则表达式,也可以包含正则表达式,Nginx服务器在搜索匹配location时,先使用不包含正则表达式进行匹配,找到匹配度最高的一个,然后再通过包含正则表达式的进行匹配,如果能匹配到直接访问,匹配不到,就使用刚才匹配度最高的那个location来处理请求
43.2 空
空:不带符号,要求必须以指定模式、指定头开始
[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf user www; worker_processes 2; error_log logs/error.log; pid logs/nginx.pid; #daemon on; # 默认on events { accept_mutex on; multi_accept on; worker_connections 1024; use epoll; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; gzip on; server { listen 80; server_name localhost; location /abc { default_type text/plain; return 200 'access success'; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } [root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload 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
浏览器访问:http://10.0.0.100/abc

浏览器访问:http://10.0.0.100/abcdef ,只要以 /abc 开头,都能被匹配到

43.3 =
=:用于不包含正则表达式的uri前,必须与指定的模式精确匹配
[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf user www; worker_processes 2; error_log logs/error.log; pid logs/nginx.pid; #daemon on; # 默认on events { accept_mutex on; multi_accept on; worker_connections 1024; use epoll; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; gzip on; server { listen 80; server_name localhost; location =/abc { default_type text/plain; return 200 'access success'; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } [root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload 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
浏览器访问:http://10.0.0.100/abc

浏览器访问:http://10.0.0.100/abcdef ,匹配不到了

注意:
浏览器访问:http://10.0.0.100/abc?name=TOM,?是传参,所以不参与location的匹配,后面会讲可以通过?获取参数

43.4 ~
~:表示当前uri中包含正则表达式,并且区分大小写
[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf user www; worker_processes 2; error_log logs/error.log; pid logs/nginx.pid; #daemon on; # 默认on events { accept_mutex on; multi_accept on; worker_connections 1024; use epoll; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; gzip on; server { listen 80; server_name localhost; location ~^/abc\w$ { default_type text/plain; return 200 'access success'; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } [root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload 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
浏览器访问:http://10.0.0.100/abc ,其中 \w 需要匹配任意字符

浏览器访问:http://10.0.0.100/abc_

浏览器访问:http://10.0.0.100/abcd

浏览器访问:http://10.0.0.100/ABcd ,区分大小写

43.5 ~*
~*:表示当前uri中包含正则表达式,并且不区分大小写
[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf user www; worker_processes 2; error_log logs/error.log; pid logs/nginx.pid; #daemon on; # 默认on events { accept_mutex on; multi_accept on; worker_connections 1024; use epoll; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; gzip on; server { listen 80; server_name localhost; location ~*^/abc\w$ { default_type text/plain; return 200 'access success'; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } [root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload 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
浏览器访问:http://10.0.0.100/abcd

浏览器访问: http://10.0.0.100/ABcd

43.6 ^~
^~:不包含正则表达式的uri前,功能和不加符号的一致(空格),唯一不同的是,如果模式匹配,停止搜索其他模式
当直接使用 /abcd 时,虽然匹配到 /abcd 的 location,但仍会继续向下匹配
[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf user www; worker_processes 2; error_log logs/error.log; pid logs/nginx.pid; #daemon on; # 默认on events { accept_mutex on; multi_accept on; worker_connections 1024; use epoll; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; gzip on; server { listen 80; server_name localhost; location /abcd { default_type text/plain; return 200 'abcd access success'; } location ~*^/abc\w$ { default_type text/plain; return 200 'access success'; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } [root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload 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
浏览器访问: http://10.0.0.100/abcd

当使用 ^~/abcd 时,匹配到 /abcd 的 location 即停止向下匹配
[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf user www; worker_processes 2; error_log logs/error.log; pid logs/nginx.pid; #daemon on; # 默认on events { accept_mutex on; multi_accept on; worker_connections 1024; use epoll; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; gzip on; server { listen 80; server_name localhost; location ^~/abcd { default_type text/plain; return 200 'abcd access success'; } location ~*^/abc\w$ { default_type text/plain; return 200 'access success'; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } [root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload 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
浏览器访问: http://10.0.0.100/abcd

———————————————————————————————————————————————————————————————————————————
无敌小马爱学习
浙公网安备 33010602011771号