By Tintin
I have a VPS with a Rails 4 application running on Ubuntu, NginX and Unicorn.
As I want all pages to be SSL encrypted, all requests to http:// are forwarded to https:// which is working fine.
This is an excerpt of my NginX configuration:
<pre>
http {
....
server {
listen 80;
server_name example.com;
rewrite ^ https://$server_name$request_uri? permanent;
}
server {
listen 443;
server_name example.com;
root /home/rails/public;
index index.htm index.html;
ssl on;
ssl_certificate /etc/ssl/example.com.crt;
ssl_certificate_key /etc/ssl/example.com.key;
location / {
try_files $uri/index.html $uri.html $uri @app;
}
location @app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
proxy_pass http://app_server;
}
}
}
</pre>
How can I make it that all requests to http://example.com and https://example.com are forwarded to https://www.example.com?
Thanks for any help.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
To redirect both non-www to www and non-ssl to ssl, you want an nginx configuration like: <pre> server { listen 80; listen 443 ssl; server_name example.com; rewrite ^ https://www.example.com$request_uri? permanent; }
server { listen 443 default ssl; server_name www.example.com; root /usr/share/nginx/html; index index.htm index.html;
ssl on;
ssl_certificate /etc/ssl/example.com.crt;
ssl_certificate_key /etc/ssl/example.com.key;
location / {
try_files $uri/index.html $uri.html $uri @app;
}
location @app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
proxy_pass http://app_server;
}
} </pre>
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.