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?
- HTTP non-www -> HTTPS www
- HTTP www -> HTTPS www
- HTTPS non-www -> HTTPS www
- 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;
}
}