【问题标题】:NGINX : Using Upstream server nameNGINX:使用上游服务器名称
【发布时间】:2021-06-14 00:08:21
【问题描述】:

我在 Ubuntu 上运行 nginx/1.19.6。

我正在努力让上游模块工作而不返回 404。

我的 *.conf 文件位于 /etc/nginx/conf.d/

文件 factory_upstream.conf:

upstream factoryserver  {
    server  factory.service.corp.com:443;
}

文件 factory_service.conf:

server
{
    listen 80;
    root /data/www;

    proxy_cache factorycache;
    proxy_cache_min_uses 1;
    proxy_cache_methods GET HEAD POST;

    proxy_cache_valid 200  72h;
    #proxy_cache_valid any  5m;

    location /factory/ {
        access_log /var/log/nginx/access-factory.log  main;
        proxy_set_header x-apikey abcdefgh12345678;
        ### Works when expressed as a literal.#   proxy_pass https://factory.service.corp.com/;
        ### 404 when using the upstream name.
        proxy_pass https://factoryserver/;
    }
}

我将错误日志设置为调试,但在重新加载配置并尝试调用后,错误日志中没有新记录。

nginx -t     # Scans OK
nginx -s reload    # no errors
cat /var/log/nginx/error.log
...
2021/03/16 11:29:52 [notice] 26157#26157: signal process started
2021/03/16 11:38:20 [notice] 27593#27593: signal process started

access-factory.log 确实记录了请求:

127.1.2.3 --;[16/Mar/2021:11:38:50 -0400];";GET /factory/api/manifest/get-full-manifest/PID/904227100052 HTTP/1.1" ";/factory/api/manifest/get-full-manifest/PID/904227100052" ;404; - ;/factory/api/manifest/get-full-manifest/PID/904227100052";-" ";ID="c4cfc014e3faa803c8fe136d6eae347d ";-" ";10.8.9.123:443" ";404" ";-"

为了帮助调试,我缓存了 404 错误,“proxy_cache_valid any 5m;”在上面的例子中注释掉了: 当我使用上游名称时,缓存文件包含以下内容:

<@#$ non-printable characters $%^>
KEY: https://factoryserver/api/manifest/get-full-manifest/PID/904227100052
HTTP/1.1 404 Not Found
Server: nginx/1.17.8
Date: Tue, 16 Mar 2021 15:38:50 GMT
...

密钥包含名称“factoryserver”,我不知道这是否重要。可以? 服务器版本和我输入命令 nginx -v 看到的不一样,即:nginx version: nginx/1.19.6 缓存文件和命令行中的版本差异是否说明了什么?

当我切换回 proxy_pass 中的文字服务器名称时,我会收到带有请求数据的 200 响应。缓存文件中的 Key 然后包含文字上游服务器名称。

<@#$ non-printable characters $%^>
KEY: https://factory.service.corp.com/api/manifest/get-full-manifest/PID/904227100052
HTTP/1.1 200
Server: nginx/1.17.8
Date: Tue, 16 Mar 2021 15:59:30 GMT
...

我将有多个上游服务器,每个都提供不同的服务。该配置将部署到多个工厂,每个工厂都有自己的上游服务器。 我希望部署团队只需更新 *_upstream.conf 文件,并将 *_service.conf 文件从部署站点到部署站点保持静态。

  • factory_upstream.conf
  • product_upstream.conf
  • shipping_upstream.conf
  • abc123_upstream.conf

为什么我在使用命名上游服务器时会收到 404?

【问题讨论】:

    标签: nginx nginx-upstreams


    【解决方案1】:

    请提供有关您代理传递请求的服务器配置的更多信息。我现在看到的唯一区别是您在上游服务器中指定了端口 (443)。

    【讨论】:

    • 你的意思是我不应该指定443端口吗?由于我的 proxy_pass 指定了 https? 'proxy_pass https : //factoryserver/;'
    • 是的。否则,如何包含上游配置?您是否尝试将上游配置文件“factory_upstream.conf”直接添加到您的工厂服务文件配置中:“factory_service.conf” 如果Nginx版本不同,则表示另一台服务器已应答。所以代理通行证是有效的,但它有错误的主机。
    【解决方案2】:

    基于缓存响应中的 nginx 版本与您在命令行上看到的不匹配,似乎 404 可能来自上游服务器。即,您的代理正在工作,但上游服务器返回 404。为了进一步排除故障,我将检查上游服务器的 nginx 日志以及传入的请求是否符合您的预期。

    请注意,在使用 proxy_pass 时,最后是否有 / 会有很大的不同。使用尾部斜杠,nginx 将其视为它应该将上游请求发送到的 URI,它不包括与位置块 (/factory/) 匹配的 URI,而没有,它包括完整的 URI。

    proxy_pass https://factoryserver/ 导致https://factory.service.corp.com:443/

    proxy_pass https://factoryserver 导致https://factory.service.corp.com:443/factory/

    文档:https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/

    因此,可能在使用上游和指定文字服务器名称之间切换时,您会无意中与尾部斜杠不一致。这是一个非常容易错过的细节,尤其是当您不知道它很重要时。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 2011-08-13
      • 2017-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多