Alex Rivera | Logout

Changing a Rails route based on deployment type

Asked 2009-10-05T19:29:09.973
14

Is there a good way of modifying a route based on the deployment type?

Basically, I have a route that has a :requirements => {:protocol => "https"}, and I'd like that to only happen in production, but not in development.

Edit
Report

2 Answers

3

Add a constant to the top of your route file like:

ROUTES_PROTOCOL = (Rails.env.production? ? "https" : "http")

And then just do:

:protocol => ROUTES_PROTOCOL 

for the routes that require https

answered 2012-07-20T16:44:53.140
-1

It's better to stick to what the current protocol is.

If your production environment involves apache or nginx for the static assets and ssl, make sure the X-FORWARDED_PROTO https header is sent to the worker when the client query is on the https port.

That way the workers will be are ware that ssl is acually handled externally and they can generate links with the correct protocol.

I know it would be better on serverfault than here, but here's an example nginx config file that forces https and sets the correct headers for ssl management in unicorn workers :

upstream WEBAPP_NAME {
      server unix:/path/to/webapp/tmp/sockets/unicorn.sock fail_timeout=0;
    }

server {
    listen       4343;
    server_name  example.com;

    root /path/to/webapp/public;
    access_log /path/to/logs/nginx-access.log;
    error_log /path/to/logs/nginx-error.log;
    rewrite_log on;

    ssl                  on;
    # redirect when http request is done on https port
    error_page 497 https://example.com:4343$request_uri;
    ssl_certificate      cert.pem;
    ssl_certificate_key  cert.key;

    ssl_session_timeout  5m;

    ssl_protocols  SSLv2 SSLv3 TLSv1;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers   on;

 location ~ ^/assets/ {
      expires 1y;
      add_header Cache-Control public;

      add_header ETag "";
      break;
    }


 location / {
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $http_host;
          proxy_set_header X-FORWARDED_PROTO https;

          proxy_pass http://WEBAPP_NAME;
          proxy_redirect default;
        }
}
answered 2012-03-06T11:50:09.007

Your Answer