【问题标题】:NGINX - Error 502 unless User-Agent header is setNGINX - 错误 502 除非设置了 User-Agent 标头
【发布时间】:2020-06-02 18:51:05
【问题描述】:

我对 NGINX 有一个非常奇怪的问题。

我们根据 URL 路由到不同的服务器。这一切都很好。使用浏览器,一切都很好。

奇怪的是,当我尝试从工具(如 Postman、Insomnia、Curl 等)向 API 发出 GET 请求时。如果 User-Agent 标头未欺骗浏览器,我会收到 502 Bad网关错误。

为什么会这样?我需要从另一个后端以编程方式调用此 API,但我必须设置 User-Agent 标头,否则它将失败。 (将用户代理设置为“curl”在浏览器中不起作用)

这是我的 NGINX 配置

    server {
        listen 443 ssl;
        server_name 10.10.10.10;
        ssl_certificate /https/10.10.10.10.crt;
        ssl_certificate_key /https/10.10.10.10.key;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers HIGH:!aNULL:!MD5;

    access_log              /var/log/nginx/10.10.10.10-access.log;
    error_log               /var/log/nginx/10.10.10.10-error.log error;

        location /abc {
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_set_header Host $http_host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Frame-Options SAMEORIGIN;
                proxy_pass http://10.3.0.2:80;
        }

        location /def {
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Frame-Options SAMEORIGIN;
                proxy_pass http://10.3.0.3:80;
        }
}

来自浏览器的请求完全正常。

来自 Insomnia 的请求与用户代理欺骗(效果很好!)

来自 Insomnia 的请求,没有欺骗用户代理(错误 502!?)

  • 注意:我在此请求中设置 cookie,因为它们由后端的服务器使用。不确定这是否与用户代理问题有关...

谢谢!

【问题讨论】:

    标签: http nginx cookies http-headers user-agent


    【解决方案1】:

    对不起。找到了。我的菜鸟错误!根本不是 nginx 的问题。

    如果用户不在 Chrome 上,Flask 服务器实际上有一条线来提供“更改您的浏览器”消息,以避免浏览器兼容性问题。不幸的是,browser_splash 文件已被删除,因此在这种情况下它没有提供任何服务。没有注意到这已提交:

    if request.user_agent.browser != 'chrome':
      return render_template('browser_splash.html')
    

    【讨论】:

      猜你喜欢
      • 2014-05-03
      • 1970-01-01
      • 1970-01-01
      • 2017-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-26
      相关资源
      最近更新 更多