$request_uri not working? nginx

rsm asked:

I just configured nginx to redirect http to https like said in How to force or redirect to SSL in nginx? by using:

server {
    listen      80;
    server_name rsm.website www.rsm.website;
    return 301 https://www.rsm.website$request_uri;
}

And the redirect seems to half work because I do get redirected to https, but it seems to loose the $request_uri path. So if an user goes to rsm.website/foo/faa it always get redirected to https://rsm.website

How can I make work the $request_uri variable?

Edit:

It seems that redirecting www.rsm.website/foo works. It only fails when there is no www. before rsm.website

This is the other server block. ( I only have two)

server {

        listen 443 ssl;
        server_name www.rsm.website;

            ssl_certificate /etc/nginx/ssl/certificate.wwww.rsm.website.crt;
        ssl_certificate_key /etc/nginx/ssl/www.rsm.website.deprotected.key;

        client_max_body_size 4G;

        access_log /web/logs/nginx-access.log;
        error_log /web/logs/nginx-error.log;

        location /static/ {
            alias /web/static/;
        }

        location /media/ {
            alias /web/media/;
        }

        location / {

            # an HTTP header important enough to have its own Wikipedia entry:
            # http://en.wikipedia.org/wiki/X-Forwarded-For
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            # enable this if and only if you use HTTPS, this helps Rack
            # set the proper protocol for doing redirects:
            # proxy_set_header X-Forwarded-Proto https;

            # pass the Host: header from the client right along so redirects
            # can be set properly within the Rack application
            proxy_set_header Host $http_host;

            # we don't want nginx trying to do something clever with
            # redirects, we set the Host: header above already.
            proxy_redirect off;

            # set "proxy_buffering off" *only* for Rainbows! when doing
            # Comet/long-poll stuff. It's also safe to set if you're
            # using only serving fast clients with Unicorn + nginx.
            # Otherwise you _want_ nginx to buffer responses to slow
            # clients, really.
            # proxy_buffering off;

            # Try to serve static files from nginx, no point in making an
            # *application* server like Unicorn/Rainbows! serve static files.
            if (!-f $request_filename) {
                proxy_pass http://rsmweb_app_server;
                break;
            }

        }

        # Error pages
        error_page 500 502 503 504 /500.html;
        location = /500.html {
            root /web/static/;
        }
}

My answer:


While your host www.rsm.website resolves to your server’s IP address in the DNS, your host rsm.website does not. It is being answered by another server entirely.

It appears that it is being answered by some server hosted by Namecheap, which is returning a redirect to www. but dropping the path and query string. It does not appear to be capable of preserving them.

You can fix this by fixing the DNS record so that it is an A record pointing to your server, instead of a record pointed at Namecheap’s broken redirector.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.