Blog

03/06/2019

How To Nginx Redirect All HTTP Request To HTTPS Rewrite 301 Rules



I have setup nginx as a secure reverse proxy server. How do I redirect all http://example.com/ requests (traffic) to https://example.com/ under nginx web server?


You can easily rewrite/redirect all http requests to https with Nginx web server. The syntax is as follows. You need to add the following in location or server directives:

Syntax – Nginx Redirect HTTP To HTTPS

if ($host ~* ^(example.com|www.example.com)$ ){
  rewrite  ^/(.*)$  https://example.com/$1  permanent;
}

OR better use the following rewrite:

rewrite  ^ https://$server_name$request_uri? permanent;

Or use new syntax (recommended):

return         301 https://$server_name$request_uri;

Redirect all HTTP requests to HTTPS with Nginx server

Edit your nginx.conf file, enter:
# vi nginx.conf
You need to define both http and https server as follows:

################################
## our HTTP server at port 80 ##
################################
server {
      listen      80 default;
      ## set up domain name here ##
      server_name sxi.io sxi.io
      access_log off;
      error_log off;
      ##** nginx redirect ALL http requests to https ** ##
      return      301 https://$server_name$request_uri;
}
#########################################################################
## Our HTTPS server at port 443. You need to provide ssl config below ###
#########################################################################
server {
      access_log  logs/sxi.io/ssl_access.log main;
      error_log   logs/sxi.io/ssl_error.log;
      index       index.html;
      root        /usr/local/nginx/html;
      ## start ssl config ##
      listen      443 http2 ssl;
      server_name sxi.io sxi.io
 
     ## redirect www to nowww
      if ($host = 'sxi.io' ) {
         rewrite  ^/(.*)$  https://sxi.io/$1  permanent;
      }
 
    ### ssl config - customize as per your setup ###
     ssl_certificate      ssl/sxi.io/sxi.io_combined.crt;
     ssl_certificate_key  ssl/sxi.io/sxi.io.key_without_password;
     ssl_protocols        TLSv1 TLSv1.1 TLSv1.2;
     keepalive_timeout    70;
     ssl_session_cache    shared:SSL:10m;
     ssl_session_timeout  10m;
    ### Rest of my config. It is optional. Do it only if you have Apache on backend ## 
 
    ## PROXY backend 
      location / {
        add_header           Front-End-Https    on;
        add_header  Cache-Control "public, must-revalidate";
        add_header Strict-Transport-Security "max-age=2592000; includeSubdomains";
        proxy_pass  http://cybercitiproxy;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
        proxy_set_header        Host            $host;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
      }
}

Save and close the file. Reload or restart the nginx server:
# nginx -s reload
Test it:
$ curl -I http://sxi.io
$ curl -I http://sxi.io/foo/bar/file.html

Sample outputs:

Fig.01 Rewrite 301 HTTP to HTTPS in Nginx server

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

20/08/2019

Start your Bitcoin Exchange with our Software

Start your Bitcoin Exchange with our Software Setup your white label bitcoin exchange right away. Or create your own cryptocurrency using...
14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....