nginx - Make rails url_helpers use https in production -
i use rails url_helpers build resources urls in app. example of url in development http://localhost:3000/resource/1
, generated resource_url(resource)
url_helper.
i use nginx in production proxy_pass requests rails app. nginx listens on port 433 https , redirects rails app on port 5000 http:
server { listen 443 ssl; server_name api.staging.zup.me; ssl_certificate /etc/ssl/certs/cert.crt; ssl_certificate_key /etc/ssl/certs/cert.key; location / { proxy_redirect off; proxy_set_header x-real-ip $remote_addr; proxy_set_header host $host:$server_port; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_pass http://localhost:5000; } }
because request rails app receives nginx proxy http, generating urls like, http://example.com/resource/1
, in production want urls use https, example, https://example.com/resource/1
.
what best way make rails generate urls https protocol in production?
you can use redirect in nginx , never touch rails app enable https:
server { listen 80 default_server deferred; server_name myserver.domain; return 301 https://$server_name$request_uri; # enforce https } # https server server { listen 443; server_name myserver.domain; ssl on; ssl_certificate /etc/nginx/ssl/myserver.domain.crt; ssl_certificate_key /etc/nginx/ssl/myserver.domain.key; root /srv/www/myserver; ... }
not sure if it's best way, becuase don't have rails app, nginx.