Linux, FreeBSD, Juniper, Cisco / Network security articles and troubleshooting guides

FAQ
It is currently Wed Dec 06, 2023 8:46 am


Username:
Subject:
Message body:
Enter your message here, it may contain no more than 60000 characters. 

Smilies
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :arrow: :| :mrgreen: :geek: :ugeek:
Font size:
 
Font colour
Options:
BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON
Disable BBCode
Disable smilies
Do not automatically parse URLs
Confirmation code
Confirmation code:
In an effort to prevent automatic submissions, we require that you enter both of the words displayed into the text field underneath.
     

Topic review - NGINX: How to redirect http to https website
Author Message
Post subject: NGINX: How to redirect http to https website  |  Post Posted: Mon Feb 09, 2015 3:27 pm
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;
}
.
Jump to:  
News News Site map Site map SitemapIndex SitemapIndex RSS Feed RSS Feed Channel list Channel list


Delete all board cookies | The team | All times are UTC - 5 hours [ DST ]



phpBB SEO