我有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的请求。 几乎准确2我们3次,请求得到适当的redirect,但每三次,我得到这个错误:
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"
任何帮助将不胜感激! 谢谢。
也许你的配置中有“localhost”而不是“127.0.0.1”。 你可以指定一个解析器,并设置ipv6关闭。
resolver 8.8.8.8 4.2.2.2 ipv6=off;
如果您的端口8081上的web服务也在nginx上,那么启用IPv6可以解决这个问题。
server { listen 8081; listen [::]:8081; .... }