计划

计划

我正在使用 NGINX 作为反向代理,并刚刚建立了一个用 Go 编写的新服务。

该服务有两个端点

GET /tracking/ping

POST /tracking/customer

在 NGINX 中,我使用以下命令来代理请求

location /v1/location/ {
    proxy_pass http://path-to-tracking-service:8181/;
}

curl两个端点如下所示时,我得到不同的结果。

终点GET /tracking/ping

curl -X GET https://example.com/v1/location/tracking/ping
"Pong!"

'POST /tracking/customer' 端点

curl -H "Content-Type: application/json" -d '{"userId":"1234"}'  https://example.com/v1/location/tracking/customer
<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.9.12</center>
</body>

不确定为什么会发生这种情况。我正在代理我拥有的其他服务,POST请求工作正常。

这里是nginx.conf

用户 nginx;worker_processes 1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    proxy_connect_timeout       600;
proxy_send_timeout          600;
proxy_read_timeout          600;
send_timeout                600;

    #gzip  on;

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

    #server {
        #include /etc/nginx/sites-enabled/*;
    #}

    server {
        listen 80;
        server_name *.example.com;
        #return 301 https://$host$request_uri;
        include /etc/nginx/sites-enabled/*;
    }

    server {


    #listen 80;
    listen 443 ssl;
    server_name *.example.com;

    ssl_certificate           /etc/ssl/example.crt;
    ssl_certificate_key       /etc/ssl/example.key;

    #ssl on;
    ssl_session_cache  builtin:1000  shared:SSL:10m;
    ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
    ssl_prefer_server_ciphers on;

    include /etc/nginx/sites-enabled/*;
    }
}

我有单独的文件被链接,/sites-enabled其中包括我的proxy_params声明。

其中两个如下

location /v1/location/ {
    proxy_pass http://example.com:8181/;
}

location /v1/ {
    proxy_pass http://example.com:8282/;
}

/v1我可以看到它们可能存在一个问题,即两个代理都感到困惑,但它对于GET端点有效。

编辑

有些人提到它可能会引发恐慌,因此我检查了 go 容器的 docker 日志,并得到了以下内容

location-tracking-staging-1 | 2016-03-14T02:35:33.580963673Z 2016/03/14 02:35:33 http: panic serving 10.7.1.5:35613: no reachable servers
location-tracking-staging-1 | 2016-03-14T02:35:33.581005488Z goroutine 97 [running]:
location-tracking-staging-1 | 2016-03-14T02:35:33.581012905Z net/http.(*conn).serve.func1(0xc820057b00)
location-tracking-staging-1 | 2016-03-14T02:35:33.581017348Z    /usr/local/go/src/net/http/server.go:1389 +0xc1
location-tracking-staging-1 | 2016-03-14T02:35:33.581030498Z panic(0x81e620, 0xc82013c5e0)
location-tracking-staging-1 | 2016-03-14T02:35:33.581034545Z    /usr/local/go/src/runtime/panic.go:426 +0x4e9
location-tracking-staging-1 | 2016-03-14T02:35:33.581038792Z main.RepoCreateVendorLocation(0xc82011ecb8, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
location-tracking-staging-1 | 2016-03-14T02:35:33.581042502Z    /go/src/location-tracking/repo.go:19 +0x178
location-tracking-staging-1 | 2016-03-14T02:35:33.581047145Z main.VendorLocationCreate(0x7f8a4366d978, 0xc8200c2ea0, 0xc820119260)
location-tracking-staging-1 | 2016-03-14T02:35:33.581050747Z    /go/src/location-tracking/handlers.go:63 +0x47b
location-tracking-staging-1 | 2016-03-14T02:35:33.581054911Z net/http.HandlerFunc.ServeHTTP(0x9965b0, 0x7f8a4366d978, 0xc8200c2ea0, 0xc820119260)
location-tracking-staging-1 | 2016-03-14T02:35:33.581058786Z    /usr/local/go/src/net/http/server.go:1618 +0x3a
location-tracking-staging-1 | 2016-03-14T02:35:33.581062770Z github.com/gorilla/mux.(*Router).ServeHTTP(0xc820010640, 0x7f8a4366d978, 0xc8200c2ea0, 0xc820119260)
location-tracking-staging-1 | 2016-03-14T02:35:33.581066604Z    /go/src/github.com/gorilla/mux/mux.go:103 +0x270
location-tracking-staging-1 | 2016-03-14T02:35:33.581070176Z net/http.serverHandler.ServeHTTP(0xc820056300, 0x7f8a4366d978, 0xc8200c2ea0, 0xc820119260)
location-tracking-staging-1 | 2016-03-14T02:35:33.581073992Z    /usr/local/go/src/net/http/server.go:2081 +0x19e
location-tracking-staging-1 | 2016-03-14T02:35:33.581077629Z net/http.(*conn).serve(0xc820057b00)
location-tracking-staging-1 | 2016-03-14T02:35:33.581081221Z    /usr/local/go/src/net/http/server.go:1472 +0xf2e
location-tracking-staging-1 | 2016-03-14T02:35:33.581084811Z created by net/http.(*Server).Serve
location-tracking-staging-1 | 2016-03-14T02:35:33.581088336Z    /usr/local/go/src/net/http/server.go:2137 +0x44e

答案1

我的 NGINX 在 POST 请求时也出现了类似的失败,但 GET 请求却运行正常。

修复方法是添加防火墙规则以允许 HTTP(S) 流量通过主机的防火墙。

这可能不适用于您的特定配置。

答案2

计划

配置出现“502 Bad Gateway”问题

  • cat /etc/nginx/sites-enabled/example.local

server {
        location /random {
                proxy_pass http://127.0.0.1:5000;
        }
}

root@sf:/var/www# curl -d“foo=bar&bin=baz”http://本地主机:5000

<html><body><h1>hi!</h1></body></html>

root@sf:/var/www# curl -d“foo=bar&bin=baz”https://example.local/random

<html>
<head><title>502 Bad Gateway</title></head>
....

解决方案

更改后的 nginx 配置:

server {
...
        error_page 502 =200 @502;

        location /random {
                proxy_pass http://127.0.0.1:5000;
        }


        location @502 {
                root /var/www/sf_random;
                proxy_method GET;
                proxy_pass http://127.0.0.1:5000;
        }
}
  • 配置语法验证

    nginx -t

  • 验证成功后

    服务 nginx 重启

我让它工作了:)

相关内容