Redirection in NGINX to an external site

Viewed 26

I need to do a redirect in NGINX, but when the PROXY_PASS occurs, the external site makes a redirect that I need to capture the full url, to make a new PROXY_PASS (including /stream).

My code is like this, but it doesn't work, it returns a 505 error.

server {
    server_name mydomain.com;
    listen 443 ssl http2 default_server;
    listen [::]:443 ssl http2 default_server ipv6only=on;

    ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem;
    include /etc/nginx/snippets/ssl.conf;

        location /stream {
        add_header Access-Control-Allow-Origin *;
                proxy_pass         http://externalSite.com/stream;
                proxy_http_version 1.1;
                proxy_set_header   Upgrade $http_upgrade;
                proxy_set_header   Connection keep-alive;
                proxy_set_header   Host $host;
                proxy_cache_bypass $http_upgrade;
                proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header   X-Forwarded-Proto $scheme;
            proxy_intercept_errors on;
            error_page 301 302 307 = @handle_redirect;
        }

        location @handle_redirect {
                add_header Access-Control-Allow-Origin *;
                set $saved_redirect_location '$upstream_http_location';
                proxy_pass ${saved_redirect_location}/stream;
                proxy_set_header Host            $host;
                proxy_set_header X-Forwarded-For $remote_addr;
        }
}

The external site to redirect to https://anotherSite{XX}.com/any, but I only need the PROXY_PASS for https://anotherSite{XX}.com/stream.

obs. {XX} can change!

I need only https://anotherSite{XX}.com/stream (without /any).

obs: maybe it can influence something, the content of https://anotherSite{XX}.com/any is a SHOUTCast stream, but it may not influence anything.

I'm very grateful.

0 Answers
Related