Dail
Dail

Reputation: 11

How to redirect HTTP to HTTPS automatically with NGINX?

I have to redirect the users to the SSL zone, if someone write http://www.example.com I Must redirect him to https://

How can I do it automatically using Nginx ?

Thank you

Upvotes: 1

Views: 5375

Answers (3)

Chirag Katudia
Chirag Katudia

Reputation: 511

The following approach is better according to

http://wiki.nginx.org/Pitfalls#Taxing_Rewrites

server {
  listen      80;
  server_name signup.mysite.com;
  rewrite     ^   https://$server_name$request_uri? permanent;
}

Upvotes: 0

For the record, it looks like this is an even better approach to it since it avoids regular expressions altogether:

server {
  listen      80;
  server_name myserver.com;
  return 301 https://$server_name$request_uri;
}

Upvotes: 1

charlie
charlie

Reputation: 89

    server {
        listen      80;
        server_name _ *;
        rewrite     ^(.*)   https://$www.example.com$1 permanent;
    }

    server {
        listen      443;
        server_name _ *;
    }

Upvotes: 0

Related Questions