Alexander Mladzhov
Alexander Mladzhov

Reputation: 605

Creating proxy in nginx

I am trying to make a proxy which redirects www.example.com/api/... to https://0.0.0.0:8443/api/ I have tried this:

server {
        server_name         example.com www.example.com;
        listen 443          ssl http2 default_server;
        add_header          Strict-Transport-Security "max-age=31536000" always;

        ssl_certificate     /etc/nginx/ssl/ssl-bundle.crt;
        ssl_certificate_key /etc/nginx/ssl/example_com.key;

        ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;

        error_page 404 = @foobar;

        location @foobar {
           rewrite  .*  / permanent;
        }

        location /api/ {
            proxy_pass https://0.0.0.0:8443/api/;
        }

        location / {
            root /usr/share/nginx/html;
            index index.html;
        }
    }

The result from nginx is 502 Bad Gateway any help is much appreciated.

Upvotes: 0

Views: 41

Answers (1)

Zach Bloomquist
Zach Bloomquist

Reputation: 5881

502 Bad Gateway indicates that the destination server you're proxying to is not responding, or is responding with an error.

Looking at your location block:

    location /api/ {
        proxy_pass https://0.0.0.0:8443/api/;
    }

proxy_pass will pass the full pathname, so if a user visited http://example.com/api/test, it would get proxied to https://0.0.0.0:8443/api/api/test. Note the doubling-up of the /api/. Try changing your proxy_pass to:

proxy_pass https://0.0.0.0:8443/

...and you won't have this issue. Perhaps this is the error causing your backend to return an error to nginx.

If this does not work, you must check your backend logs for more information.

Upvotes: 2

Related Questions