0

I have an Nginx web server and I need to:

  1. Redirect all HTTP (both www and non-www) to HTTPS
  2. Redirect all HTTPS non-www to www

I know that I can use different server{} blocks but I want to achieve this with one server block using "if" statements, so here is my actual Nginx vhost configuration:

server {
        listen 80;
        listen 443 ssl http2;
        server_name website.com www.website.com;

        if ($scheme = "http") {
            return 301 https://www.website.com$request_uri;
        }

        if ($host = "website.com") {
            return 301 https://www.website.com$request_uri;
        }

        [...]
}

It works fine without problems:

http://website.com     => https://www.website.com
http://www.website.com => https://www.website.com
https://website.com    => https://www.website.com

My questions is if my configuration is correct.

Thanks!

user2972081
  • 101
  • 1

0 Answers0