微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Nginx 代理传递指令:上游错误中的无效端口

如何解决Nginx 代理传递指令:上游错误中的无效端口

我正在使用 Nginx 进行负载平衡。这是我的配置

upstream web_backend {
        least_conn;
        server localhost:8001  max_fails=3 fail_timeout=60s;
        server localhost:8002  max_fails=3 fail_timeout=60s;
}


server {
    listen 8545;

    server_name _;

    location / {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_pass http://web_backend;
    }
}


server {
        listen 8001;
        server_name localhost;
        location / {
        proxy_pass https://some_other_url/v3/cxnmcdinwrtyf93vcwdiyfx8q6xqwxv9qg7c93fgcb;

        }
}

server {
        listen 8002;
        server_name localhost;
        location / {
        proxy_pass 'https://chipdunk-dude:gorgeous-serpents-clubbed-orphans@nd-657-555-555-777.dogify.com';
        }
}

如你所见,8002 端口的 url 很奇怪(甚至不知道这种 url 叫什么) 因为它在 url 中有“:”,Nginx 给了我这个错误

Nginx: [emerg] invalid port in upstream "chipdunk-dude:gorgeous-serpents-clubbed-orphans@nd-657-555-555-777.dogify.com" in /etc/Nginx/sites-enabled/default:60

8001 端口的 url 工作正常。

解决方法

@ 之前的所有内容都是 userinfo,它应该由浏览器编码并根据 RFC 7617 作为单独的请求标头包含。

Nginx 不是浏览器,不能为你做。

您可以将其转换为 Base64 并使用 proxy_set_header 设置 Authorization 标头。

例如:

proxy_set_header Authorization "Basic Y2hpcGR1bmstZHVkZTpnb3JnZW91cy1zZXJwZW50cy1jbHViYmVkLW9ycGhhbnM=";
proxy_pass https://nd-657...;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。