Redirect HTTP to HTTPS in VestaCP with NGINX

Very simple trick here, but has a few people at a road block. If you’re running NGINX on VestaCP then the following will redirect all HTTP traffic to HTTPS traffic. Just make sure you’ve got a SSL certificate set-up and working.

Redirect HTTP to HTTPS

Create a new file (if you already have it, place the next bit of code inside it) at /home/USER/conf/web/ (replace USER with your VestaCP Username) and add the following code:

if ($scheme = http) { return 301$request_uri; }

Replace with your own domain name.


Redirect HTTPS to HTTP

Similarly if you want to redirect all secure traffic to non secure then use this code instead:

if ($scheme = https) { return 301$request_uri; }

Replace with your own domain name.

4 thoughts...

    1. SSULLIVAN88 says:

      If it was bad they would have removed it 😉

      1. JꙨ dцӍoηҬ says:

        if 🙂 I understood properly

        at this level in the nginx config the if is evil because it will be evaluate at every queries

        1. Thanks for sharing! Really appreciate it. 😀

          I imagine then that for very high traffic websites (we’re talking hundreds of millions of hits) it’s not ideal however, it’s just like .htaccess. The .htaccess file is evaluated at every query too and Apache and .htaccess are still a dominant, even if used as a proxy.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>