【问题标题】:Why do I get 404 on nginx reverse proxy?为什么我在 nginx 反向代理上得到 404?
【发布时间】:2019-06-06 15:43:28
【问题描述】:

以下是我的配置,我在除 well-known 路由之外定义的所有路由上都得到 404,我不明白为什么。

如果我向 http://example.tech/connect 发出请求,我会收到 404,如果我向 http://api.example.tech 发出请求,我也会收到 404。

我看不出哪里出错了,因为这看起来应该可以工作!

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;
    error_log  /var/log/nginx/error.log  warn;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    #REMOVE REFERENCE TO FILES THAT HAVE  "server" and "location" blocks in them so we can do it all in this file
    #include /etc/nginx/conf.d/*.conf;

    # issue with ip and the nginx proxy
    real_ip_header X-Forwarded-For;
    set_real_ip_from 0.0.0.0/0;

    server {
        listen 80;
        listen [::]:80;
        server_name example.tech;

        location /.well-known/openid-configuration {
            proxy_pass https://myapp.net;

            proxy_redirect off;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection keep-alive;
            #proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
            proxy_set_header X-Forwarded-Host $host;
            #proxy_set_header X-Forwarded-Proto $scheme;
            client_max_body_size 10m;
            client_body_buffer_size 128k;
            proxy_connect_timeout 90;
            proxy_send_timeout 90;
            proxy_read_timeout 90;
            proxy_buffers 32 4k;
        }

        location /connect {
            proxy_pass https://myapp.net;

            proxy_redirect off;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection keep-alive;
            #proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
            proxy_set_header X-Forwarded-Host $host;
            #proxy_set_header X-Forwarded-Proto $scheme;
            client_max_body_size 10m;
            client_body_buffer_size 128k;
            proxy_connect_timeout 90;
            proxy_send_timeout 90;
            proxy_read_timeout 90;
            proxy_buffers 32 4k;
        }

        location /auth {
            proxy_pass https://myapp.net;

            proxy_redirect off;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection keep-alive;
            #proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
            proxy_set_header X-Forwarded-Host $host;
            #proxy_set_header X-Forwarded-Proto $scheme;
            client_max_body_size 10m;
            client_body_buffer_size 128k;
            proxy_connect_timeout 90;
            proxy_send_timeout 90;
            proxy_read_timeout 90;
            proxy_buffers 32 4k;
        }
    }

    server {
        listen 80;
        listen [::]:80;
        server_name api.example.tech;

        location /auth/ {
            proxy_pass https://myapp.net;

            proxy_redirect off;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection keep-alive;
            #proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
            proxy_set_header X-Forwarded-Host $host;
            #proxy_set_header X-Forwarded-Proto $scheme;
            client_max_body_size 10m;
            client_body_buffer_size 128k;
            proxy_connect_timeout 90;
            proxy_send_timeout 90;
            proxy_read_timeout 90;
            proxy_buffers 32 4k;
        }
    }
}

【问题讨论】:

    标签: nginx nginx-location nginx-reverse-proxy


    【解决方案1】:

    这里有两个例子,希望能澄清一些事情。

     location /some-path {
        proxy_pass http://server:3000;
     }
    

    在这种情况下,proxied server(目标)必须处理路由/some-path。如果处理其他内容,例如仅/,它将向 Nginx 返回错误。

    一种解决方案是添加尾随/,例如:

     location /some-path {
        proxy_pass http://server:3000/;
     }
    

    现在发送到/some-path 的请求可以(并且必须)由代理服务器端的路由/ 处理。但是,这可能会导致某些服务器出现问题。例如,使用 Expresscurl localhost/some-path 将由 Express 很好地处理,而 curl localhost/some-path/ 将导致 Express 返回 Cannot GET //

    这对于你的目标服务器可能会有所不同,但原理是一样的:如果你只指定服务器,location中的完整路径会传递给服务器,所以必须做相应的处理。

    【讨论】:

      【解决方案2】:

      这是我得到 404 而不是 502 的情况:

      # let's define some proxy pass
      location ~ /.well-known/acme-challenge {
          proxy_pass http://127.0.0.1:5080; # this backend doesn't exist which leads to 502
          proxy_set_header Host $host;
      }
      
      # this is a default directives
      error_page   500 502 503 504  /50x.html; # this is a reason to redirect 502 to 50x.html
      location = /50x.html { # but this file doesn't exist in root so we get 404 instead of 502
          root   /usr/share/nginx/html;
      }
      

      【讨论】:

        【解决方案3】:

        您需要在 proxy_pass 指令中使用特定的 uri,而不是反斜杠。但是在您的情况下,反斜杠充当特定的uri。 Nginx 将'/auth'(例如)替换为'/'(您已添加)。

        其实你说的答案是对的,把proxy_pass http://myapp.net;变成proxy_pass http://myapp.net/;

        原因是proxy_pass 可以在有/没有特定 uri 的情况下以两种不同的方式工作。 More details about this directive 在 nginx.org 上。 Blow 是该链接中引用的一些内容。

        • 如果 proxy_pass 指令是用一个 URI 指定的,那么当一个请求被传递到服务器时,规范化的请求 URI 的一部分 匹配位置被指令中指定的 URI 替换:

          位置/名称/ {
          proxy_pass http://127.0.0.1/remote/;
          }

        • 如果 proxy_pass 没有指定 URI,则请求 URI 以与客户端发送原始请求时相同的格式传递给服务器 请求被处理,或者完整的规范化请求 URI 被传递 处理更改后的 URI 时:

          位置 /some/path/ {
          proxy_pass http://127.0.0.1;
          }

        在您的情况下,proxy_pass 指令中没有 URI,因此 /auth 将被传递给后端服务器。不幸的是,您的后端服务器没有 /auth 资源,因此返回 404。如果您的后端服务器确实有要处理的/auth,那么在请求uri /auth 时您将永远不会收到404 错误。

        【讨论】:

          【解决方案4】:

          由于某种原因,proxy_pass 末尾需要一个正斜杠

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-11-29
            • 1970-01-01
            • 1970-01-01
            • 2019-07-31
            • 1970-01-01
            • 2019-07-09
            • 2016-08-19
            相关资源
            最近更新 更多