Nginx代理传递 - 禁用ipv6

时间:2014-10-09 07:15:43

标签: nginx reverse-proxy ipv6 ipv4 proxypass

我有如下nginx conf:

server {
listen       127.0.0.1:8080;
server_name  127.0.0.1;

client_max_body_size 20m;

include /etc/nginx/conf.d/services/*.conf;

# redirect server error pages to the static page /50x.html                                                                                                                                              
#                                                                                                                                                                                                       
error_page   500 /500.json;
error_page   501 /501.json;
error_page   502 /502.json;
error_page   503 /503.json;
error_page   504 /504.json;
error_page   404 /404.json;

location = /500.json {
    root   /opt/lj/nginx;
}
location = /501.json {
    root   /opt/lj/nginx;
}
location = /502.json {
    root   /opt/lj/nginx;
}
location = /503.json {
    root   /opt/lj/nginx;
}
location = /504.json {
    root   /opt/lj/nginx;
}
location = /404.json {
    root  /opt/lj/nginx;
}
}

在conf.d / services目录中,我有一个文件:

location /api/auth {proxy_pass     http://127.0.0.1:8081;}

我有一个上游进程在8081上侦听请求。几乎准确地说我们3次,请求被适当地重定向,但每三次,我得到这个错误:

2014/10/09 03:03:52 [alert] 4572#0: *16 socket() failed (97: Address family not supported by protocol) while connecting to upstream, client: 127.0.0.1, server: 127.0.0.1, request: "GET /api/auth/1.0/companies/1/clients HTTP/1.1", upstream: "http://[::1]:8081/api/auth/1.0/companies/1/clients", host: "localhost:8080"

我不知道如何让nginx只转发到上游理解的IPv4地址。

访问日志:(请注意,3次尝试中有1次导致500次,其余次传递)

127.0.0.1 - portalAdmin [09/Oct/2014:02:56:37 -0400] "GET /api/auth/1.0/companies/1/clients HTTP/1.1" 500 107 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36" "98.207.234.244"
127.0.0.1 - portalAdmin [09/Oct/2014:02:57:38 -0400] "GET /api/auth/1.0/companies/1/clients HTTP/1.1" 200 2 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36" "98.207.234.244"
127.0.0.1 - portalAdmin [09/Oct/2014:02:57:39 -0400] "GET /api/auth/1.0/companies/1/clients HTTP/1.1" 200 2 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36" "98.207.234.244"
127.0.0.1 - portalAdmin [09/Oct/2014:02:57:41 -0400] "GET /api/auth/1.0/companies/1/clients HTTP/1.1" 500 107 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36" "98.207.234.244"
127.0.0.1 - portalAdmin [09/Oct/2014:03:03:44 -0400] "GET /api/auth/1.0/companies/1/clients HTTP/1.1" 200 2 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36" "98.207.234.244"
127.0.0.1 - portalAdmin [09/Oct/2014:03:03:45 -0400] "GET /api/auth/1.0/companies/1/clients HTTP/1.1" 200 2 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36" "98.207.234.244"
127.0.0.1 - portalAdmin [09/Oct/2014:03:03:46 -0400] "GET /api/auth/1.0/companies/1/clients HTTP/1.1" 500 107 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36" "98.207.234.244"
127.0.0.1 - portalAdmin [09/Oct/2014:03:03:50 -0400] "GET /api/auth/1.0/companies/1/clients HTTP/1.1" 200 2 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36" "98.207.234.244"
127.0.0.1 - portalAdmin [09/Oct/2014:03:03:51 -0400] "GET /api/auth/1.0/companies/1/clients HTTP/1.1" 200 2 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36" "98.207.234.244"
127.0.0.1 - portalAdmin [09/Oct/2014:03:03:52 -0400] "GET /api/auth/1.0/companies/1/clients HTTP/1.1" 500 107 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36" "98.207.234.244"

任何帮助将不胜感激!感谢。

2 个答案:

答案 0 :(得分:6)

也许你的配置中某处有“localhost”而不是“127.0.0.1”。您可以指定一个解析器并将ipv6设置为off。

resolver 8.8.8.8 4.2.2.2 ipv6=off;

如果端口8081上的web服务也在nginx上,那么为此启用IPv6可以解决问题。

server {
    listen 8081;
    listen [::]:8081;
    ....
}

答案 1 :(得分:0)

我有同样的问题。 更改 Ipv6only = on; ==>到==> Ipv6only = off; 对我不起作用。 当我完全删除“Ipv6only = off / on”时 - 它开始像魅力一样工作!

尝试删除以完全删除它并重新加载nginx服务。