How to Redirect All HTTP Requests to HTTPS in Nginx

This post will guide you how to redirect all http requests to https under your Nginx Web Server. For example, you want to redirect all HTTP traffic from your website http://mytest.com/ to HTTPS. you need to modify your virtual hosts configuration under your Nginx web server.

redirect all http request to https1

Redirect All HTTP Requests to HTTPS


To redirect all HTTP Requests to HTTPS for you web site with Nginx Server, just do the following steps:

Step1: you need to open your Nginx Config is located in the /etc/nginx/conf.d/ directory with your vi text editor.

$ sudo vi /etc/nginx/conf.d/www.mytest.com.conf

Step2: adding the following line into Server section.

return  301 https://$server_name$request_uri;

Step3: save and close the file. and the nginx config is similar to the below:

$ cat /etc/nginx/conf.d/www.mytest.com.conf

server {
listen 80;;
root /var/www/mytest.com;
index index.html;
server_name www.mytest.com;
return 301 https://$server_name$request_uri;

}

Step4: you need to restart or reload your Nginx web server with the following command:

$ /etc/init.d/nginx restart

or

$ /etc/init.d/nginx reload

 

You might also like:

Sidebar



back to top