3

I am using NGINX as a reverse proxy and just put up a new service written in Go.

The service has two endpoints

GET /tracking/ping

POST /tracking/customer

In NGINX, I am using the following to proxy the request

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

When curl the two endpoints such as the following, I get different results.

The GET /tracking/ping endpoint

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

The 'POST /tracking/customer` endpoint

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>

Not sure why this would happen. I am proxying other services I have and POST requests work perfectly fine.

Here is the nginx.conf

user 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/*;
    }
}

I have separate files that are being linked to /sites-enabled that include my proxy_params declarations.

Two of them are the following

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

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

I could see their maybe being an issue with it getting confused by the /v1 on both the proxies, but it works for the GET endpoint.

EDIT

Some people have brought up the point that it may be panicking so I checked the docker logs for the go container and got the following

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
TheJediCowboy
  • 613
  • 3
  • 8
  • 12
  • Please post your nginx config, especially the relevant location parts. – Tim Mar 13 '16 at 21:48
  • @Tim I have updated my post with `nginx.conf` as well as included the proxies defined under `/sites-enabled` – TheJediCowboy Mar 13 '16 at 22:22
  • Did your program panic? – Michael Hampton Mar 13 '16 at 22:35
  • @MichaelHampton not sure what you mean? As in a Go panic? (I am still very new to Go...) – TheJediCowboy Mar 13 '16 at 22:46
  • Yes, a Go panic. You should take a look and see if it's actually still running... – Michael Hampton Mar 13 '16 at 22:51
  • As usually with nginx just run debug binary and add debug keyword to a error_log option – ALex_hha Mar 13 '16 at 22:52
  • Do you have the "headers more" module compiled in? That lets you add a header to be sure you're hitting the location block you expect. However it seems like you just have a bad proxy_pass in there. Curl the url (port 8xxx) directly from the box and see what happens. – Tim Mar 13 '16 at 22:55
  • @Tim if it was a bad proxy, wouldn't I not be able to hit the `GET /ping` endpoint I exposed. I am able to hit this endpoint fine. So the proxy has to be working in some regard – TheJediCowboy Mar 14 '16 at 02:22
  • @MichaelHampton It is still running because I can hit the `GET /ping` endpoint through that proxy. – TheJediCowboy Mar 14 '16 at 02:23
  • @MichaelHampton I edited my POST with some logs that may suggest the program panicked. I don't have much experience in reading Go logs so I can't tell for sure. – TheJediCowboy Mar 14 '16 at 02:39
  • 2
    Looks like a panic to me. The big clue is that it tells you "panic"! Time to debug your code. – Michael Hampton Mar 14 '16 at 02:45
  • Haha, @MichaelHampton it wouldn't be the first time I was told something and the problem ended up being something completely different! I will debug it and post the answer. – TheJediCowboy Mar 14 '16 at 16:17

2 Answers2

0

My NGINX had similar failure with POST request, while GET request was working fine.

Fix was to add firewall rule to allow HTTP(S) traffic through host machine's firewall.

This might not apply to your particular configuration.

Jman
  • 1
0

Plan

Problem with "502 Bad Gateway" giving config

  • 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://localhost: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>
....

Solution

nginx config after changes:

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;
        }
}
  • config syntax validation

    nginx -t

  • after successful validation

    service nginx restart

And I got it working :)

Erkko
  • 1