0

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the suggestion, yet the problem stands. When I try reaching https://0.0.0.0:8443/api/news/list, everything works fine, but when I try reaching www.example.com/api/news/list it returns the same issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.