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.

3 Answers 3

17

You can explicitly define them separately and test for the environment

  if Rails.env.production?
    map.resources :purchases, :requirements => {:protocol => "https"}
  else
    map.resources :purchases
  end

Note, if you're on older versions of Rails, use ENV['RAILS_ENV'] == production instead

3
  • 1
    In general, using Rails.env.production? is the way to go. It could also be used in a before_filter that redirects to https, but this works just as well. Oct 5, 2009 at 20:33
  • 1
    Thanks. For some reason, the fact that routes.rb is just a regular old Ruby file never hits me. This is great. Oct 5, 2009 at 20:39
  • 2
    I am just guessing here, but wouldn't Rails.env.development? be 'safer'? I'd want my production to be the default, the development the exception.
    – Mosselman
    Nov 5, 2012 at 15:35
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

-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;
        }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.