301 redirect with custom content problem

Maxim Dounin mdounin at mdounin.ru
Fri Nov 30 07:43:24 UTC 2012


Hello!

On Fri, Nov 30, 2012 at 12:53:03PM +1100, Robert Mueller wrote:

> Hi
> 
> I'm trying to setup a permanent redirect from http -> https. As a
> fallback for a small number of users where https is blocked, I'd like to
> show them a message.
> 
> I thought an error_page 301 handler would allow me to do this, but I'm
> having trouble making it work:
> 
>   server {
>     listen *:80 default;
>     rewrite ^ https://example.com$uri permanent;
>   }
> 
>   error_page 301 /foo/bar/301.html;
> 
> Seems to confuse the redirect. It seems to completely replace the
> redirect Location header with foo/bar/301.html rather than actually
> serving that content.

This way you'll end up with two 301 redirects due to rewrite being 
executed again for /foo/bar/301.html.

Try this instead:

    server {
        listen 80 default;

        location / {
            error_page 301 /foo/bar/301.html;
            return 301 "https://example.com$request_uri";
        }
               
        location = /foo/bar/301.html {
            # static
        }
    }


-- 
Maxim Dounin
http://nginx.com/support.html



More information about the nginx mailing list