长沙网站设计哪家好,深圳建站公司告诉你十个建站步骤,网站开发视频代码,网站开发的开发工具目录
一、匹配规则
1.基础匹配
2.精确匹配
3.前缀匹配
4.正则表达式匹配
二、优先级顺序
三、示例
1.精确匹配 2.前缀匹配
3.正则匹配
4.默认匹配
第一种情况#xff1a;proxy_pass最后面没有斜杠,匹配路径有斜杠(/bbb/)
第二种情况#xff1a; proxy_pass最后面…目录
一、匹配规则
1.基础匹配
2.精确匹配
3.前缀匹配
4.正则表达式匹配
二、优先级顺序
三、示例
1.精确匹配 2.前缀匹配
3.正则匹配
4.默认匹配
第一种情况proxy_pass最后面没有斜杠,匹配路径有斜杠(/bbb/)
第二种情况 proxy_pass最后面有斜杠 “/”匹配路径也有斜杠(/bbb/)
第三种情况proxy_pass后面还有其他路径但是最后没有 “/” 匹配路径也有斜杠(/bbb/)
第四种情况 proxy_pass后面还有其他路径但是最后有 “/” 匹配路径也有斜杠(/bbb/)
第五种情况location匹配路径末尾没有 “/”proxy_pass后面也没有“/” 一、匹配规则
1.基础匹配
location / { ... }这是最基本的匹配匹配所有请求。因为它没有指定具体的文件或目录所以通常作为后备选项出现。
2.精确匹配
location /exact/path { ... }精确匹配这个路径。如果请求的 URI 完全等于 /exact/path则使用这个 location 块的配置处理此请求。这具有最高的优先级。
3.前缀匹配
location /prefix/ { ... }前缀匹配请求的 URI 的开始部分。如果请求的 URI 以 /prefix/ 开始这个 location 块将被用来处理请求。location ^~ /prefix/ { ... }前缀匹配但它会停止正则表达式匹配即使正则表达式可能会更具体匹配。如果该 location 匹配Nginx 不会考虑之后的正则 location 块。
4.正则表达式匹配
location ~ /regex/ { ... }大小写敏感的正则匹配。location ~* /regex/ { ... }大小写不敏感的正则匹配。location / { ... } 正则表达式匹配会在普通字符串前缀匹配后进行。如果有多个正则表达式 location 都匹配请求则使用第一个匹配的 location 块。
二、优先级顺序
Nginx 处理请求时 location 匹配的优先级顺序如下
首先进行精确匹配 location 其次按文件中出现顺序匹配所有正则表达式 location ~ 和 location ~*然后进行最长的前缀匹配 location ^~最后是通常的前缀匹配 location /prefix/如果前面的匹配都没有找到就使用默认的 location /
三、示例
1.精确匹配
server {listen 80;listen [::]:80;server_name welcome.com;access_log /etc/nginx/vhost/logs/access_welcome.log;error_log /etc/nginx/vhost/logs/error_welcome.log;location /test.html {root /usr/share/nginx/html;}location /test.html {#通过 root 路径会将条件拼接进来root /var/www/html;index index.html;}error_page 404 /404.html; location /404.html {root /usr/share/nginx/test_html;}error_page 500 502 503 504 /50x.html;location /50x.html {}
}
# 第一个和第二个location匹配条件一样都是/test.html
# 但第二个为精准匹配到静态路径因此第一个不会执行会执行第二个。server {listen 80;listen [::]:80;server_name welcome.com;access_log /etc/nginx/vhost/logs/access_welcome.log;error_log /etc/nginx/vhost/logs/error_welcome.log;location /test.html {#通过alias指定路径无需拼接条件#alias指定的路径结尾要加“/”alias /usr/share/nginx/test_html/;}error_page 404 /404.html; location /404.html {root /usr/share/nginx/test_html;}error_page 500 502 503 504 /50x.html;location /50x.html {}
}# 指定静态资源路径除了使用关键字root还可以用alias。
# 那么root和alias的区别是什么
# 用root属性指定的值是要加入到最终路径中的匹配条件会拼接到路径中
# 用alias属性指定的值不需要加入到最终路径中# 请求的条件为test.html通过root指定的路径为/usr/share/nginx/test_html因此在匹配的时候这个路径下就必须要有test.html这个文件才可以否则就会找不到而报错如果用alias那么通过浏览器进行请求的时候alias也是指定到/usr/share/nginx/test_htm路径下但是会匹配默认的index.html而无须强制匹配test.html。# 不能使用””来进行精确匹配 2.前缀匹配
server {listen 80;listen [::]:80;server_name welcome.com;access_log /etc/nginx/vhost/logs/access_welcome.log;error_log /etc/nginx/vhost/logs/error_welcome.log;location ^~ /a/ {root /data/webroot/;}location ^~ /a/www/ {root /data/webroot/;}}# 当请求为 http://welcome.com/a/ 时访问 /data/webroot/a/ 下的index.html文件
# 当请求为 http://welcome.com/a/www/ 时访问 /data/webroot/a/www/ 下的index.html文件
# 如果^~匹配成功了那么表示阻断正则表达式不再进行正则匹配
3.正则匹配
# 正则表达式匹配大小写敏感
server {listen 80;listen [::]:80;server_name welcome.com;access_log /etc/nginx/vhost/logs/access_welcome.log;error_log /etc/nginx/vhost/logs/error_welcome.log;location ~ \.(gif|jpg|png)$ {root /var/www/images;}error_page 404 /404.html; location /404.html {root /usr/share/nginx/test_html;}error_page 500 502 503 504 /50x.html;location /50x.html {}
}
# 正则表达式匹配大小写不敏感
server {listen 80;listen [::]:80;server_name welcome.com;access_log /etc/nginx/vhost/logs/access_welcome.log;error_log /etc/nginx/vhost/logs/error_welcome.log;location ~* \.(css|js)$ {root /var/www/assets;}error_page 404 /404.html; location /404.html {root /usr/share/nginx/test_html;}error_page 500 502 503 504 /50x.html;location /50x.html {}
}
4.默认匹配
第一种情况proxy_pass最后面没有斜杠,匹配路径有斜杠(/bbb/)
server {listen 80;listen [::]:80;server_name welcome.com;access_log /etc/nginx/vhost/logs/access_welcome.log;error_log /etc/nginx/vhost/logs/error_welcome.log;#默认匹配location /bbb/ {proxy_pass http://127.0.0.1:9190;proxy_redirect off;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}# proxy_pass最后面没有斜杠“/”此时通过浏览器请求http://10.9.2.248/bbb/
# 那么实际访问的地址就是 http://127.0.0.1:9190/bbb/会将匹配路径/bbb一起加过去。
第二种情况 proxy_pass最后面有斜杠 “/”匹配路径也有斜杠(/bbb/)
server {listen 80;listen [::]:80;server_name welcome.com;access_log /etc/nginx/vhost/logs/access_welcome.log;error_log /etc/nginx/vhost/logs/error_welcome.log;#默认匹配location /bbb/ {proxy_pass http://127.0.0.1:9190/;proxy_redirect off;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}
# proxy_pass最后面有斜杠”/”此时通过浏览器请求http://10.9.2.248/bbb/
# 那么实际访问的地址就是 http://127.0.0.1:9190会将/bbb抛弃的
第三种情况proxy_pass后面还有其他路径但是最后没有 “/” 匹配路径也有斜杠(/bbb/)
server {listen 80;listen [::]:80;server_name welcome.com;access_log /etc/nginx/vhost/logs/access_welcome.log;error_log /etc/nginx/vhost/logs/error_welcome.log;#默认匹配location /bbb/ {proxy_pass http://127.0.0.1:9190/ccc;proxy_redirect off;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}
}
# 通过浏览器访问http://10.9.2.248/bbb/index.html
# 实际请求的是http://127.0.0.1/index.html
# 位置是默认路径下不是ccc路径下
# 如果proxy_pass的路径为/ccc/ddd那么实际请求的就是ccc路径下的cccindex.html
第四种情况 proxy_pass后面还有其他路径但是最后有 “/” 匹配路径也有斜杠(/bbb/)
server {listen 80;listen [::]:80;server_name welcome.com;access_log /etc/nginx/vhost/logs/access_welcome.log;error_log /etc/nginx/vhost/logs/error_welcome.log;#默认匹配location /bbb/ {proxy_pass http://127.0.0.1:9190/ccc/;proxy_redirect off;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}
}
# 通过浏览器访问http://10.9.2.248/bbb/index.html
# 实际访问的是http://127.0.0.1/ccc/index.html
第五种情况location匹配路径末尾没有 “/”proxy_pass后面也没有“/”
server {listen 80;listen [::]:80;server_name welcome.com;access_log /etc/nginx/vhost/logs/access_welcome.log;error_log /etc/nginx/vhost/logs/error_welcome.log;#默认匹配location /bbb {proxy_pass http://127.0.0.1:9190;proxy_redirect off;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}
}
# 匹配路径和proxy_pass后都没有”/”
# 访问http://10.9.2.248/bbb
# 默认将请求到http://127.0.0.1:9190/bbb/index.html的内容