|Linux, FreeBSD, Juniper, Cisco / Network security articles and troubleshooting guides
|NGINX: How to redirect http to https website
|Page 1 of 1|
|Author:||mandrei99 [ Mon Feb 09, 2015 3:27 pm ]|
|Post subject:||NGINX: How to redirect http to https website|
It is a good practice to run a website purely on https and not http for obvious reason. If both http and https services are configured to run properly, first step to take is to redirect http to https within nginx configuration. For this, there are two "server" section, one for each http(s) service.
How to redirect http to https service in nginx configuration permanently - HTTP 301 status code:
rewrite ^(.*) https://$host$1 permanent;
IN above configuration for a virtual host, first "server" section defines the http service (listen 80) and the second the https service (listen 443).
The first section rewrite rule can be explained as following: rewrite (redirect) any HTTP URI to the same host as present one ($host) to HTTPS followed by the original requested URI. The "permanent" keyword makes use of "HTTP 301" status - Moved permanently.
This can be confirmed with a telnet to http port:
GET /some-uri.html HTTP/1.1
HTTP/1.1 301 Moved Permanently
Date: Mon, 09 Feb 2015 19:13:55 GMT
From the Nginx manual:
stops processing the current set of ngx_http_rewrite_module directives and starts a search for a new location matching the changed URI;
stops processing the current set of ngx_http_rewrite_module directives as with the break directive;
returns a temporary redirect with the 302 code; used if a replacement string does not start with “http://” or “https://”;
returns a permanent redirect with the 301 code.
How to redirect http to https service in nginx configuration temporarily - HTTP 302 code:
rewrite ^(.*) https://$host$1 redirect;
|Page 1 of 1||All times are UTC - 5 hours [ DST ]|
|Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group