0

I'm redirecting incoming HTTP traffic on port 80 to HTTPS on port 443. There, I'm redirecting non-www to www:

http://example.com -> https://example.com --> https://www.example.com

It seems google pagespeed insights counts this as "multiple redirects". It also does seem introduce 300-400ms latency between each redirect.

Is there any way to do a redirect in one step for all scenarios?

  1. HTTP non-www -> HTTPS www
  2. HTTP www -> HTTPS www
  3. HTTPS non-www -> HTTPS www
  4. HTTPS www -> HTTPS www

My config file below:

server {
        listen 80;
        server_name example.com;
        rewrite ^/(.*) https://example.com/$1 permanent;
}

server {
        root /var/www/html;

        index index.php index.js index.html index.htm index.nginx-debian.html;

        server_name example.com;
        return 301 https://www.example.com$request_uri;
        ssl_certificate /ssl/example.com.chained.crt;
        ssl_certificate_key /ssl/example.com.key;

        location / {
        proxy_pass http://localhost:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        }

        location /wordpress {
        proxy_pass http://localhost:8090;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        }
}

server {
        listen 443 ssl;
        listen [::]:443 ssl;

        root /var/www/html;

        index index.php index.js index.html index.htm index.nginx-debian.html;

        server_name www.example.com;
        ssl_certificate /ssl/example.com.chained.crt;
        ssl_certificate_key /ssl/example.com.key;

        location / {
        proxy_pass http://localhost:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        }

        location /wordpress {
        proxy_pass http://localhost:8090;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        }
}
1

In this part of your configuration , you can :

  • you can catch multiple name example.com and www.example.com
  • you can redirect directly to www.example.com

Example of all redirect configuration :

server {
        listen 80;
        server_name example.com  www.example.com;
        rewrite ^/(.*) https://www.example.com/$1 permanent;
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;

    server_name example.com;
    ssl_certificate /ssl/example.com.chained.crt;
    ssl_certificate_key /ssl/example.com.key;
    rewrite ^/(.*) https://www.example.com/$1 permanent;
}


server {
    listen 443 ssl;
    listen [::]:443 ssl;

    server_name www.example.com ;

    # HERE you push all you configuration for HTTPS://WWW.EXAMPLE.COM

    #.../.../

}
  • thanks. I realized I asked a pretty dumb question lol. All I needed to do was to redirect in my port 80 server block directly to https://www. – geochanto Jul 3 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.