Nginx proxy_pass 配置转发 / 路径

发布时间: 更新时间: 总字数:1230 阅读时间:3m 作者: IP上海 分享 网址

Nginx proxy_pass uri 的 / 路径详细介绍。

Nginx 配置 proxy_pass 转发的 / 路径问题

在 nginx 中配置 proxy_pass 时,如果是按照^~匹配路径时,要注意 proxy_pass 后的 url 最后的 / :

  • 当加上了 /,相当于绝对根路径,则 nginx 不会把 location 中匹配的路径部分代理走
  • 如果没有 /,相当于相对路径,则会把匹配的路径部分也给代理走

示例

有 /

location ^~ /proxy_api/ {
    proxy_cache api_cache;
    proxy_set_header Host api.xiexianbin.cn;
    proxy_pass http://api.xiexianbin.cn/;
}

如上面的配置,如果请求的 urlhttp://your_domain/proxy_api/load.php,会被代理成 http://api.xiexianbin.cn/load.php

无 /

location ^~ /proxy_api/ {
    proxy_cache api_cache;
    proxy_set_header Host api.xiexianbin.cn;
    proxy_pass http://api.xiexianbin.cn;
}

如上面的配置,则会被代理到http://api.xiexianbin.cn/proxy_api/load.php

其他实现方式

当然,我们可以用如下的 rewrite 来实现 / 的功能:

location ^~ /proxy_api/ {
    proxy_cache api_cache;
    proxy_set_header Host api.xiexianbin.cn;
    rewrite /proxy_api/(.+)$ /$1 break;
    proxy_pass http://api.xiexianbin.cn;
}

其他问题

Forwarded 配置

location /abc/ {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-Host $http_host;
    proxy_pass http://<ip>:<port>/;
}

Referer policy 问题

location /abc/ {
    # add_header 'Referrer-Policy' 'no-referrer-when-downgrade';
    # add_header 'Referrer-Policy' 'unsafe-url';
    # add_header 'Referrer-Policy' 'strict-origin';
    # proxy_set_header Referer $http_referer;
    proxy_set_header Referer http://<ip>:<port>$request_uri;
    proxy_redirect off;
    proxy_pass http://<ip>:<port>/;
}

CORS 跨域请求代理

  • 一般配置
location /abc/ {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Headers' 'X-Requested-With';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
    add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With' always;
    proxy_pass http://<ip>:<port>/;
}
  • nginx 反向代理支持 CORS(跨域请求访问)

CORS 机制跨域会首先发送一个 preflight(方法为 OPTIONS)请求,该请求成功(http code20x)后才会发送真正的请求, Nginx 可以采用如下配置代理:

server {
    ...
    location / {
        # 针对浏览器的 OPTIONS 预请求直接返回 20x,否则会被403 forbidden--invalie CORS request
        if ( $request_method = 'OPTIONS' ) {
            return 200; # 204
        }

        proxy_pass  http://<ip:port>/;
    }
    ...
}

若解决不了,也可采用 4 层代理

aaa from origin bbb has been blocked by CORS policy: response to preflight request doesn’t pass access control check no Access-Control-Allow-Private-Network header was present in the preflight response for this private network request targeting the local address space

错误是因为 Chrome 浏览器启用了 Private Network Access (PNA) 安全机制。

当公网网站(origin bbb)试图请求你局域网/私有网络内的资源(origin aaa,例如 localhost 或 192.168.x.x)时,浏览器会发送一个预检请求(OPTIONS),并要求服务器必须显式允许这种公网调内网的行为。

解决方法:在 Nginx aaa service 配置中处理 OPTIONS 请求,并添加 Access-Control-Allow-Private-Network 响应头。

请在你的 Nginx serverlocation 配置块中添加以下内容:

location / {
    # 1. 允许跨域请求的来源 (建议指定具体域名,调试时可用 *)
    add_header 'Access-Control-Allow-Origin' '$http_origin' always;

    # 2. 关键修复:允许私有网络访问,我是一个内网服务,但我明确允许来自公网的这个请求访问我
    add_header 'Access-Control-Allow-Private-Network' 'true' always;

    # 3. 其他标准 CORS 头
    add_header 'Access-Control-Allow-Credentials' 'true' always;
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
    add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;

    # 4. 单独处理 OPTIONS 预检请求,浏览器在正式请求前会先发一个 `OPTIONS` 请求来探路。这个 `if` 块专门用来拦截这个探路请求,并直接返回 `204 No Content`(成功),同时带上允许的头信息。
    if ($request_method = 'OPTIONS') {
        # 针对 OPTIONS 请求必须再次下发这些头,因为 Nginx 的 if 块可能会导致外层 header 失效
        add_header 'Access-Control-Allow-Origin' '$http_origin' always;
        add_header 'Access-Control-Allow-Private-Network' 'true' always;
        add_header 'Access-Control-Allow-Credentials' 'true' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;

        # 预检请求不需要包体,直接返回 204
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain; charset=utf-8';
        add_header 'Content-Length' 0;
        return 204;
    }

    # ... 这里是你原本的 proxy_pass 或其他业务逻辑 ...
    # proxy_pass http://localhost:8080;
}

nginx -t 检测慢

strace -T -f -o /tmp/trace.log nginx -t

若为 DNS 超时,可采用如下配置规避

http {
    # 1. 配置 DNS 服务器地址 (如 Google DNS 或内网 DNS)
    # 2. valid=30s 强制覆盖 DNS 记录的 TTL,让 Nginx 缓存解析结果 30 秒
    # 3. ipv6=off 如果不需要 IPv6 解析可以关闭,减少查询开销
    resolver 8.8.8.8 1.1.1.1 valid=30s ipv6=off;

    # 设置解析超时时间(可选,默认 30s)
    resolver_timeout 2s;

    server {
        location /proxy {
            set $backend "www.example.com";

            # 当 proxy_pass 使用变量时,Nginx 会使用 resolver 重新解析域名
            proxy_pass http://$backend;
        }
    }
}
本文总阅读量 次 本站总访问量 次 本站总访客数
Home Archives Categories Tags Statistics