Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting to SSL using nginx

Tags:

nginx

I have http:// and https:// on the same host like the following:

server {

    listen   80;
    listen   443 ssl;

    ...
    ...
}

What I need to do is redirecting users who access my shop to https://. The problem is I have many languages:

https://mydomain.com/en/shop https://mydomain.com/fr/shop etc...

I tried this and it didn't work (nginx: configuration file /etc/nginx/nginx.conf test failed):

if ($server_port = 80) {
    location (en|fr)/shop {
        rewrite ^ https://$host$request_uri permanent;
    }
}
like image 543
Adam Silver Avatar asked Aug 30 '13 00:08

Adam Silver


People also ask

How do I redirect traffic to HTTPS nginx?

Redirect HTTP to HTTPS version for Specified domain in Nginx Server_name domain-name.com www.domain-name.com – it specifies the domain names. So, replace it with your website domain name that you want to redirect. Return 301 https://domain-name.com$request_uri – it moves the traffic to the HTTPS version of the site.

What is return 301 in nginx?

Temporary and Permanent Nginx Redirect Explained To map this change, the redirects response code 301 is used for designating the permanent movement of a page. These kinds of redirects are helpful when the user wants to change the domain name and no longer wants a browser to access it.


1 Answers

It would also be more of an NGINX best practice to do a 301 redirect instead of using the if statement (see Server name on http://wiki.nginx.org/Pitfalls). I created a gist with an nginx.conf configured for SSL, Rails and Unicorn

https://gist.github.com/Austio/6399964

Here would be the relevant section for yours.

server {
    listen      80;
    server_name domain.com;
    return 301  https://$host$request_uri;
}
like image 180
Austio Avatar answered Oct 05 '22 06:10

Austio