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:
Code:
server {
server_name server.ivorde.ro;
listen 80;
rewrite ^(.*) https://$host$1 permanent;
}
server {
server_name server.ivorde.ro;
listen 443;
ssl on;
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:
Code:
GET /some-uri.html HTTP/1.1
Host: server.ivorde.ro
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Mon, 09 Feb 2015 19:13:55 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: https://server.ivorde.ro/some-uri.html
From the Nginx manual:
Quote:
last
stops processing the current set of ngx_http_rewrite_module directives and starts a search for a new location matching the changed URI;
break
stops processing the current set of ngx_http_rewrite_module directives as with the break directive;
redirect
returns a temporary redirect with the 302 code; used if a replacement string does not start with “http://” or “https://”;
permanent
returns a permanent redirect with the 301 code.
How to redirect http to https service in nginx configuration temporarily - HTTP 302 code:
Code:
server {
server_name server.ivorde.ro;
listen 80;
rewrite ^(.*) https://$host$1 redirect;
}
.
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.
[h2]How to redirect http to https service in nginx configuration permanently - HTTP 301 status code:[/h2]
[code]server {
server_name server.ivorde.ro;
listen 80;
rewrite ^(.*) https://$host$1 permanent;
}
server {
server_name server.ivorde.ro;
listen 443;
ssl on;[/code]
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 [b]HTTP[/b] URI to the same host as present one ($host) to [b]HTTPS[/b] 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:[code]
GET /some-uri.html HTTP/1.1
Host: server.ivorde.ro
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Mon, 09 Feb 2015 19:13:55 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: https://server.ivorde.ro/some-uri.html
[/code]
From the Nginx manual:
[quote]last
stops processing the current set of ngx_http_rewrite_module directives and starts a search for a new location matching the changed URI;
break
stops processing the current set of ngx_http_rewrite_module directives as with the break directive;
redirect
returns a temporary redirect with the 302 code; used if a replacement string does not start with “http://” or “https://”;
permanent
returns a permanent redirect with the 301 code.[/quote]
[h2]How to redirect http to https service in nginx configuration temporarily - HTTP 302 code:[/h2]
[code]server {
server_name server.ivorde.ro;
listen 80;
rewrite ^(.*) https://$host$1 redirect;
}[/code].