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

FAQ
It is currently Tue Oct 03, 2023 3:46 pm


This forum is dedicated to Apache, Nginx, Lighttpd, Squid and other HTTP transport protocol related software

Author Message
mandrei99
Post  Post subject: NGINX: How to redirect http to https website  |  Posted: Mon Feb 09, 2015 3:27 pm

Joined: Tue Aug 04, 2009 9:16 am
Posts: 250

Offline
 

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:


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;
}
.





Top
Display posts from previous:  Sort by  
E-mail friendPrint view

Topics related to - "NGINX: How to redirect http to https website"
 Topics   Author   Replies   Views   Last post 
There are no new unread posts for this topic. How to test deflate/gzip compression on a HTTPS (HTTP over SSL) Apache server

debuser

0

5491

Tue Aug 03, 2010 3:30 am

debuser View the latest post

There are no new unread posts for this topic. NGINX: how to set the default virtual host

mandrei99

0

3953

Thu Dec 12, 2013 6:01 am

mandrei99 View the latest post

There are no new unread posts for this topic. Nginx: How to list virtual hosts

mandrei99

0

13294

Mon Feb 23, 2015 5:05 pm

mandrei99 View the latest post

There are no new unread posts for this topic. NGINX: 413 Request Entity Too Large

admin

0

1958

Thu Sep 14, 2017 4:48 am

admin View the latest post

There are no new unread posts for this topic. Attachment(s) Nginx + apache (for PHP) with real IP addresses in logs

designeru

0

3058

Tue May 18, 2010 9:49 am

designeru View the latest post

There are no new unread posts for this topic. Nginx + php-fpm increase upload_max_filesize and other php values per vhost

mandrei99

0

8189

Mon Jan 02, 2012 9:04 am

mandrei99 View the latest post

There are no new unread posts for this topic. Converting PhpBB-SEO Apache RewriteRule to Nginx rewrite

debuser

0

10535

Tue Feb 23, 2010 5:00 am

debuser View the latest post

There are no new unread posts for this topic. Apache to Nginx Server parameters translation with php function

debuser

0

104653

Thu Aug 26, 2010 7:47 am

debuser View the latest post

There are no new unread posts for this topic. Starting nginx: [emerg]: directive "rewrite" is not terminated by ";"

debuser

0

8362

Tue Feb 23, 2010 6:05 am

debuser View the latest post

There are no new unread posts for this topic. Apache - Restricting "Server" information in HTTP response header with ServerTokens

debuser

0

2308

Thu Nov 10, 2011 6:39 am

debuser View the latest post

 

Who is online
Users browsing this forum: No registered users and 1 guest
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum
Jump to:  
cronNews 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