Nginx usptream 302 redirect

Hello,
Is there an easy way to configure nginx upstream to follow 302 instead
of
send them to the browser?

I tried with this config:

http {
proxy_intercept_errors on;

proxy_cache_path /home/toli/nginx/run/cache keys_zone=zone_c1:256m

inactive=5d max_size=30g;

upstream up_servers {
    server 127.0.1.1:8081;
}

server {
    listen 127.0.0.1:8080;

    location / {
        proxy_cache zone_c1;
        proxy_pass http://127.0.1.1:8081;
        proxy_temp_path tmp ;

        error_page 301 302 307 @redir;
    }

    location @redir {
        proxy_cache zone_c1;
        proxy_pass $upstream_http_location;
        proxy_temp_path tmp ;
    }
}

}

Unfortunately it do not work. I receive “500: Internal Server Error.”
and in the logs I have [invalid URL prefix in “”]

From dev mail list Maxim advised me to backup $upstream_http_location in
other variable and I did it but the result was the same - 500 internal
server error. The config after the fix is:

http {
proxy_intercept_errors on;

proxy_cache_path /home/toli/nginx/run/cache keys_zone=zone_c1:256m

inactive=5d max_size=30g;

upstream up_servers {
    server 127.0.1.1:8081;
}

server {
    listen 127.0.0.1:8080;

    location / {
        proxy_cache zone_c1;
        proxy_pass http://127.0.1.1:8081;
        proxy_temp_path tmp ;
        set $foo $upstream_http_location;
        error_page 301 302 307 @redir;
    }

    location @redir {
        proxy_cache zone_c1;
        proxy_pass $foo;
        proxy_temp_path tmp ;
    }
}

}

Do you have any idea how this could be achieved.

Hello!

On Fri, Oct 04, 2013 at 05:33:57PM +0300, Anatoli M. wrote:

[…]

From dev mail list Maxim advised me to backup $upstream_http_location in
other variable and I did it but the result was the same - 500 internal
server error. The config after the fix is:

[…]

        proxy_pass $foo;
        proxy_temp_path tmp ;
    }

You need “set … $upstream_http_location” to be executed after a
request to an upstream was done, so you need in location @redir,
not in location /.


Maxim D.
http://nginx.org/en/donation.html

Hello,

Very sorry to reply on this thread again.

I have the same requirement, but I still can not get nginx to follow the
302 redirect as I want.

My configuration:

upstream backend {
server 10.255.199.60:1220;
}

server {
listen 1220;
server_name localhost;

    location / {
            proxy_pass http://backend;
            error_page 301 302 307 @redir;
            #set $foo $upstream_http_location;
    }

    location @redir {
            set $foo $upstream_http_location;
            proxy_pass $foo;
    }

}

And when I use curl to test , I got:

curl -v

http://101.36.99.131:1220/vds48/export/data/videos_vod/v16/videos/0/00092/791/2/2.m3u8

  • About to connect() to 101.36.99.131 port 1220 (#0)
  • Trying 101.36.99.131… connected
  • Connected to 101.36.99.131 (101.36.99.131) port 1220 (#0)

GET /vds48/export/data/videos_vod/v16/videos/0/00092/791/2/2.m3u8 HTTP/1.1
User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.13.6.0
zlib/1.2.3 libidn/1.18 libssh2/1.4.2
Host: 101.36.99.131:1220
Accept: /

< HTTP/1.1 302 Found
< Server: nginx/1.4.4
< Date: Thu, 19 Mar 2015 01:34:15 GMT
< Content-Length: 0
< Connection: keep-alive
< Cache-Control: no-cache
< Location:
http://10.255.199.43:1220/vds48/export/data/videos_vod/v16/videos/0/00092/791/2/2.m3u8
<

  • Connection #0 to host 101.36.99.131 left intact
  • Closing connection #0

What’s the problem?
Thanks.

MC

Maxim D. wrote in post #1123508:

Hello!

On Fri, Oct 04, 2013 at 05:33:57PM +0300, Anatoli M. wrote:

[…]

From dev mail list Maxim advised me to backup $upstream_http_location in
other variable and I did it but the result was the same - 500 internal
server error. The config after the fix is:

[…]

        proxy_pass $foo;
        proxy_temp_path tmp ;
    }

You need “set … $upstream_http_location” to be executed after a
request to an upstream was done, so you need in location @redir,
not in location /.


Maxim D.
nginx: donation