nginx 配置多个反向代理配置二级目录反向代理

前提:

有时候配置好的反向代理不能改动,并且权重较高的,比如:

   #PROXY-CONF-START
    location ^~ / {
      
      
      proxy_pass http://127.0.0.1:3000;
      proxy_set_header Host $http_host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Real-Port $remote_port;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Forwarded-Host $host;
      proxy_set_header X-Forwarded-Port $server_port;
      proxy_set_header REMOTE-HOST $remote_addr;
      
      proxy_buffering off;
      proxy_connect_timeout 60s;
    proxy_send_timeout 600s;
    proxy_read_timeout 600s;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection $connection_upgrade;
      
      
      
      
      
    }

调整 location 优先级

Nginx 的 location 优先级规则为:‌精确匹配 = > 前缀匹配 ^~ > 正则匹配 ~/~* > 普通前缀匹配‌。你可以利用这一特性,例如:

# 优先匹配 /files/ 路径下的静态资源
location ^~ /files/ {
    root /www/dk_project/wwwroot/ai.hnnonggou.com;
}

# 其他请求走反向代理
location / {
    proxy_pass http://127.0.0.1:3000;
}

精确路径匹配(优先级最高)

# 精确匹配根目录下的 example.txt 文件
location = /example.txt {
    root /www/dk_project/wwwroot/ai.baidu.com;  # 文件实际路径为:根目录 + /example.txt
    access_log off;  # 可选:关闭日志记录
}

使用更高优先级的精确匹配(Exact Match)

# 精确匹配根目录的 example.txt
location = /example.txt {
    root /www/dk_project/wwwroot/ai.hnnonggou.com;
}

# 匹配 /static/ 目录下的所有请求
location ^~ /static/ {
    root /www/dk_project/wwwroot/ai.hnnonggou.com;
}

# 反向代理其他所有请求(优先级低于精确匹配)
location ^~ / {
    proxy_pass http://127.0.0.1:3000;
    # 其他代理配置...
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

toooooop8

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值