代理 (nginx) 显示错误网关错误

hel*_*y77 18 proxy nginx gateway

我有一个在 上运行的服务(docker 注册表)port 5000,我已经安装了 nginx 以将 http 请求从 重定向80805000。如果我卷曲localhost:5000它可以工作,但是当我卷曲它时,localhost:8080我会收到一个错误的网关错误。

nginx配置文件:

upstream docker-registry {
 server localhost:5000;
}

server {
 listen 8080;
 server_name registry.mydomain.com;

 proxy_set_header Host       $http_host; 
 proxy_set_header X-Real-IP  $remote_addr; 
 client_max_body_size 0; 
 chunked_transfer_encoding on;

 location / {

     proxy_pass http://docker-registry;
 }
 location /_ping {
     auth_basic off;
     proxy_pass http://docker-registry;
 }
 location /v1/_ping {
     auth_basic off;
     proxy_pass http://docker-registry;
 }

}
Run Code Online (Sandbox Code Playgroud)

/var/log/nginx/error.log我有:

[crit] 15595#0: *1 connect() to [::1]:5000 failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1, server: registry.mydomain.com, request: "GET / HTTP/1.1", upstream: "http://[::1]:5000/", host: "localhost:8080"
Run Code Online (Sandbox Code Playgroud)

任何的想法?

小智 59

我假设它是一个 Linux 机器,所以很可能 SELinux 正在阻止连接,因为没有允许连接的策略。

你应该可以运行

# setsebool -P httpd_can_network_connect true

然后重启nginx。

  • 您刚刚修复了 _my_ 问题,即使您没有修复 hellb0y77 的问题。SE_LINUX 再次来袭! (2认同)

Gre*_*gor 5

根据错误消息,我怀疑 localhost:5000 是否被解析为您可能不想要的 ipv6 地址。您可以尝试将其更改为 127.0.0.1:5000

编辑:在您的 proxy_pass 行中,您是否可能缺少 URL 的一部分?尝试添加 $request_uri 所以它可能是:

proxy_pass http://docker-registry/$request_uri;
Run Code Online (Sandbox Code Playgroud)

或者可能:

proxy_pass http://docker-registry$request_uri;
Run Code Online (Sandbox Code Playgroud)

不确定哪一个最正确。

另一件事要考虑。您的配置表明:

server_name registry.mydomain.com;
Run Code Online (Sandbox Code Playgroud)

因此, localhost:8080 可能不匹配。为了测试,您可以将其更改为:

server_name registry.mydomain.com localhost;
Run Code Online (Sandbox Code Playgroud)

然后将匹配 localhost:8080 以及您的域。我假设 registry.mydomain.com 只是一个例子,你可以把你的真实服务器 FQDN 放在那里。