Redirect HTTP To HTTPS in Nginx - Linuxize
Redirect HTTP To HTTPS in Nginx - Linuxize
Redirect HTTP To HTTPS in Nginx - Linuxize
In this guide, we will explain how to redirect the HTTP tra c to HTTPS in Nginx.
If you are a developer or system administrator, chances are that you’re dealing with Nginx on
a regular basis. One of the most common tasks you’ll likely perform is redirecting the HTTP
tra c to the secured (HTTPS) version of your website.
Unlike HTTP, where requests and responses are sent and returned in plaintext, HTTPS uses
TLS/SSL to encrypt the communication between the client and the server.
There are many bene ts of using HTTPS over HTTP, such as:
All the data is encrypted in both directions. As a result, sensitive information cannot be
read if intercepted.
Google Chrome and all other popular browsers will mark your website as safe.
HTTPS allows you to use the HTTP/2 protocol, which signi cantly improves the site
performance.
Google favors HTTPS websites. Your site will rank better if served via HTTPS.
The preferred method to redirect HTTP to HTTPS in Nginx is to con gure a separate server
block for each version of the site. You should avoid redirecting the tra c using the if
directive , as it may cause unpredictable behavior of the server.
To redirect a single website to HTTPS open the domain con guration le and make the
following changes:
server {
listen 80;
server_name linuxize.com www.linuxize.com;
return 301 https://linuxize.com$request_uri;
}
listen 80 - The server block will listen for incoming connections on port 80 for the
speci ed domain.
server_name linuxize.com www.linuxize.com - Speci es the server block’s domain
names. Make sure you replace it with your domain name.
return 301 https://linuxize.com$request_uri - Redirect the tra c to the HTTPS
version of the site. The $request_uri variable is the full original request URI, including
the arguments.
Usually, you will also want to redirect the HTTPS www version of the site to the non-www or
vice versa. The recommended way to do the redirect is to create a separate server block for
both www and non-www versions.
For example, to redirect the HTTPS www requests to non-www, you would use the following
con guration:
server {
listen 80;
server_name linuxize.com www.linuxize.com;
return 301 https://linuxize.com$request_uri;
}
server {
listen 443 ssl http2;
server_name www.linuxize.com;
# . . . other code
server {
listen 443 ssl http2;
server_name linuxize.com;
# . . . other code
}
Whenever you make changes to the con guration les you need to restart or reload the
Nginx service for changes to take effect:
To create a single catch-all HTTP block which will redirect the visitors to the HTTPS version
of the site, open the Nginx con guration le and make the following changes:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 301 https://$host$request_uri;
}
listen 80 default_server - Sets this server block as the default (catch-all) block for
all unmatched domains.
server_name _ - _ is an invalid domain name that never matches any real domain
name.
return 301 https://$host$request_uri - Redirect the tra c to the corresponding
HTTPS server block with status code 301 (Moved Permanently). The $host variable
holds the domain name of the request.
For example, if the visitor opens http://example.com/page2 in the browser, Nginx will
redirect the request to https://example.com/page2 .
Conclusion
In Nginx, the preferred way to redirect HTTP to HTTPS is to create a separate server blocks
and perform 301 redirect.
nginx
BUY ME A COFFEE
Sign up to our newsletter and get our latest tutorials and news
straight to your mailbox.
Related Articles
DEC 13, 2019
DEC 4, 2019
© 2021 Linuxize.com
Privacy Policy Terms Contact Advertise on Linuxize