setting Rails environment hash variables for Rack middleware

215 views Asked by At

I have a Rails app being served on standalone Passenger behind an nginx proxy. Because of this setup, the environment hash needs a bit of tweaking to work with a piece of rack middleware we're using (rack-cas). Specifically I have to set env['SERVER_PORT'] = '443' and env['HTTPS'] = 'on' in the middleware's call method (we don't want Passenger using SSL, would rather the nginx proxy handle it).

I can do all that easy enough in the middleware but can I do it in the Rails app so I don't have to customize the middleware?

1

There are 1 answers

0
Dave On

Turns out it can all be done from the nginx proxy's config, don't have to touch the app or middleware at all:

    location /foo/ {
        proxy_pass http://0.0.0.0:SOME_PORT/foo/;

        proxy_buffering off;
        proxy_http_version 1.1;

        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Host $host; # more robust than http_host
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # this ensures your app's env is correct
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Proto https; # add this if you always want the redirects to go to HTTPS
    }