≡ Menu

HowTo: Nginx Redirect All HTTP Request To HTTPS Rewrite 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?

The syntax is as follows. You need to add the following in location or server directives:

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;

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

## our http server at port 80
server {
      listen default;
      server_name example.com www.example.com;
      ## redirect http to https ##
      rewrite        ^ https://$server_name$request_uri? permanent;
## Our https server at port 443. You need to provide ssl config here###
server {
      access_log  logs/example.com/ssl_access.log main;
      error_log   logs/example.com/ssl_error.log;
      index       index.html;
      root        /usr/local/nginx/html;
      ## start ssl config ##
      listen ssl;
      server_name example.com www.example.com;
     ## redirect www to nowww
      if ($host = 'www.example.com' ) {
         rewrite  ^/(.*)$  https://example.com/$1  permanent;
    ### ssl config - customize as per your setup ###
     ssl_certificate      ssl/example.com/example.com_combined.crt;
     ssl_certificate_key  ssl/example.com/example.com.key_without_password;
     ssl_protocols        SSLv3 TLSv1 TLSv1.1 TLSv1.2;
     ssl_ciphers RC4:HIGH:!aNULL:!MD5;
     ssl_prefer_server_ciphers on;
     keepalive_timeout    70;
     ssl_session_cache    shared:SSL:10m;
     ssl_session_timeout  10m;
    ## 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://exampleproxy;
        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:
# nginx -s reload
Test it:
$ curl -I http://example.com
$ curl -I http://example.com/foo/bar/file.html

Sample outputs:

HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Sat, 01 Dec 2012 23:49:52 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: https://example.com/
Share this tutorial on:

Like this? Follow us on Twitter OR support us by using Patreon

{ 9 comments… add one }
  • Jared December 2, 2012, 1:14 am

    An even better way to redirect would be with this:

    return 301 http://exampledomain.com$request_uri;

    • Ruben Decrop November 15, 2015, 11:59 am

      POST requests are converted to GET with a 301 redirect, which is not always what is desired

  • David Gómez (@emsLinux) December 2, 2012, 8:00 pm

    Excelent tip!

    Please write more stuff about NGINX, I love it.

  • Sukoon February 12, 2013, 3:53 pm

    This is the best redirect tutorial for nginx on the web. Great work!!

  • karnd01 August 19, 2014, 7:16 pm

    need to change $server_name to $host as $server_name will default to the first and not the particular name the user typed in.

    may seem trivial but when getting into public vs internal dns entries and routing of traffic you may need to user the hostname that got you here.

  • Jon September 30, 2014, 11:02 pm

    Doesn’t work on Ubuntu 14.04.

  • Komu February 4, 2015, 1:40 pm

    To redirect on the same port:
    Lets assume your IP address is and your app is running on port 3000 and you want nginx to listen on port 7000.

        server {
            listen 7000 ssl;
            ssl_certificate /path/to/ssl_certificate.cer;
            ssl_certificate_key /path/to/ssl_certificate_key.key;
            ssl_client_certificate /path/to/ssl_client_certificate.cer;
            # this line is important, for u to handle http and https on same port
            # see: http://bit.ly/1C0kgny
            error_page 497 301 =307$request_uri;
            # in order to preserve the http VERB that the request came with,
            # we use error response redirection
            # see: http://bit.ly/1uX5jNO
            location / {
                proxy_pass_header Server;
                proxy_set_header Host $http_host;
                proxy_redirect off;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-Protocol $scheme;
  • Dawid September 8, 2015, 12:44 pm

    Using if statements is strongly discouraged in nginx. See here: http://wiki.nginx.org/IfIsEvil and here: http://wiki.nginx.org/Pitfalls#Taxing_Rewrites

  • shairalibaig December 7, 2015, 6:00 am

    hii Thanks a lot for ur post..
    but am trieng to redirect https subadomain to another https subdomain
    but the post requests getting converted into get..
    how can redirect 301 for https to https using post to post
    any help would be greatly appreciated..

Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">

   Tagged with: , , , , , , , , ,