LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

nginx中有关 location 模块的配置示例说明

freeflydom
2025年7月1日 16:22 本文热度 93

一、匹配规则 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的内容​

转自https://www.cnblogs.com/hahaha111122222/p/18835262


该文章在 2025/7/1 16:22:31 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2025 ClickSun All Rights Reserved