将 www https 站点重定向到非 www https 导致 nginx 中的重定向过多
Redirect www https site to non-www https cause too many redirect in nginx
我在 Laravel 中有一个项目,我正在使用带有 nginx 的数字海洋,我尝试将 www 重定向到非 www url 但我不能,这是我的 nginx 配置:
example.com
server{
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
server{
listen [::]:443 ssl;
listen 443 ssl;
server_name www.example.com;
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
return 301 https://$host$request_uri;
}
server {
listen [::]:443 ssl;
listen 443 ssl;
server_name example.com;
root /var/www/website/production/public;
index index.php index.html;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
https://example.com //有效
http://example.com -> https://example.com //有效
http://www.example.com -> https://www.example.com // 重定向到 www https,重定向太多
https://www.example.com // 重定向太多。
我希望人们访问 http://www.example.com,http://example.com 和 https://www.example.com 它重定向到 https://example.com, 我能做什么做?谢谢。
第二个 server
块包含一个循环:
server{
listen [::]:443 ssl;
listen 443 ssl;
...
return 301 https://$host$request_uri;
}
$host
的值与请求的域相同。此块的目的是将请求的域从 www.example.com
更改为 example.com
.
最简单的解决方案是更改return
语句并显式指定最终域名:
return 301 https://example.com$request_uri;
我在 Laravel 中有一个项目,我正在使用带有 nginx 的数字海洋,我尝试将 www 重定向到非 www url 但我不能,这是我的 nginx 配置:
example.com
server{
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
server{
listen [::]:443 ssl;
listen 443 ssl;
server_name www.example.com;
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
return 301 https://$host$request_uri;
}
server {
listen [::]:443 ssl;
listen 443 ssl;
server_name example.com;
root /var/www/website/production/public;
index index.php index.html;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
https://example.com //有效
http://example.com -> https://example.com //有效
http://www.example.com -> https://www.example.com // 重定向到 www https,重定向太多
https://www.example.com // 重定向太多。
我希望人们访问 http://www.example.com,http://example.com 和 https://www.example.com 它重定向到 https://example.com, 我能做什么做?谢谢。
第二个 server
块包含一个循环:
server{
listen [::]:443 ssl;
listen 443 ssl;
...
return 301 https://$host$request_uri;
}
$host
的值与请求的域相同。此块的目的是将请求的域从 www.example.com
更改为 example.com
.
最简单的解决方案是更改return
语句并显式指定最终域名:
return 301 https://example.com$request_uri;