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.
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
Rails.env.development? be 'safer'? I'd want my production to be the default, the development the exception.
Nov 5, 2012 at 15:35
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
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;
}
}