我将端口 80 上的传入 HTTP 流量重定向到端口 443 上的 HTTPS。在那里,我将非 www 重定向到 www:
http://example.com->https://example.com-->https://www.example.com
google pagespeed insights 似乎将此算作“多次重定向”。它似乎也确实在每次重定向之间引入了 300-400 毫秒的延迟。
有没有办法可以在所有情况下一次性完成重定向?
- HTTP 非 www -> HTTPS www
- HTTP www -> HTTPS www
- HTTPS 非 www -> HTTPS www
- HTTPS www -> HTTPS www
我的配置文件如下:
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
在此配置部分,您可以:
- 你可以捕获多个名字示例.com和www.example.com
- 您可以直接重定向至www.example.com
所有重定向配置的示例:
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
#.../.../
}