在 nginx 中,为什么我无法将网址用作已用作代理通行证的代理通行证

In nginx why I'm unable to use a web address as a proxy pass which is already used as a proxy pass

我的 nginx 配置中有以下服务器块:

server {
        listen 80; #default_server;
        listen [::]:80; #default_server;

        client_max_body_size  20M;
        client_header_timeout 600;
        client_body_timeout   600;
        keepalive_timeout     600;
        server_name example1-proxy.in;

    location / {
                proxy_pass http://example1.in;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header Forwarded $proxy_add_forwarded;
                proxy_set_header X-Forwarded-For "";
                proxy_connect_timeout       600;
                proxy_send_timeout          600;
                proxy_read_timeout          600;
                send_timeout                600;
        }

}
server {
        listen 80; #default_server;
        listen [::]:80; #default_server;

        client_max_body_size  20M;
        client_header_timeout 600;
        client_body_timeout   600;
        keepalive_timeout     600;
        server_name example2-proxy.in;

    location / {
                proxy_pass http://example2.in;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header Forwarded $proxy_add_forwarded;
                proxy_set_header X-Forwarded-For "";
                proxy_connect_timeout       600;
                proxy_send_timeout          600;
                proxy_read_timeout          600;
                send_timeout                600;
        }

}

我想在另一个 nginx 配置中将 example1-proxy 和 example2-proxy 配置为代理传递,所以我做了以下操作:

server {
            listen 80; #default_server;
            listen [::]:80; #default_server;
    
            client_max_body_size  20M;
            client_header_timeout 600;
            client_body_timeout   600;
            keepalive_timeout     600;
            server_name example5.in;
    
        location /temp1/ {
                    proxy_pass http://example1-proxy.in;
                    proxy_set_header Host $host;
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header Forwarded $proxy_add_forwarded;
                    proxy_set_header X-Forwarded-For "";
                    proxy_connect_timeout       600;
                    proxy_send_timeout          600;
                    proxy_read_timeout          600;
                    send_timeout                600;
            }
            location /temp2/ {
                    proxy_pass http://example2-proxy.in;
                    proxy_set_header Host $host;
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header Forwarded $proxy_add_forwarded;
                    proxy_set_header X-Forwarded-For "";
                    proxy_connect_timeout       600;
                    proxy_send_timeout          600;
                    proxy_read_timeout          600;
                    send_timeout                600;
            }
    
    }

当我点击 url 应该调用 http://example5.in/temp2, the http://example2-proxy.in 但我收到以下错误:

{
    "timestamp": 1643470058798,
    "status": 403,
    "error": "Forbidden",
    "message": "Access Denied",
    "path": "/temp2/some path to api "
}

我不明白为什么上面的配置不起作用,请提出任何解决方案。

当你使用代理传递时,NGINX 会查看目的地 url, 如果它发现目标 url 不包含 URI(域名后的部分,例如 /temp),这是您的情况,因为您要转发到“http://example2-proxy.in”,那么NGINX 会将它最初收到的任何 URI 添加到下一个服务器。 在您的情况下,将导致对 http://example2-proxy.in/temp2/

的请求

如果你想发送请求到http://example2-proxy.in/ you need to specify the URI for the destination server such as http://example2-proxy.in/

这是来自 NGINX 网站的快照here

解决了,实际上是在第二个nginx配置中:

location /temp1/ {
                    proxy_pass http://example1-proxy.in;
                    proxy_set_header Host $host;

$host 应该被 example1-proxy.in 替换,只有这样这个服务器块才会被激活。所以正确的配置将是:

location /temp1/ {
                    proxy_pass http://example1-proxy.in;
                    proxy_set_header Host example1-proxy.in;