重定向www https站点到非www https在nginx中引起太多重定向
我在Laravel有一个项目,我正在使用nginx使用数字海洋,我尝试将www重定向到非www urls,但我不能,这是我的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 // works
http://example.com - > https://example.com // works
http://www.example.com - > https://www.example.com // redirects to www https,带有太多redirects
https://www.example.com //太多重定向。
我希望当人们访问 http://www.example.com , http:// example.com 和 https://www.example.com 它将其重定向到 https://example.com ,我能做什么?谢谢。
I have a project in Laravel, I am using digital ocean with nginx, I try to redirect www to non-www urls but I can't, this is my nginx configuration:
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 //works
http://example.com -> https://example.com //works
http://www.example.com -> https://www.example.com // redirects to www https, with too many redirects
https://www.example.com // too many redirects.
I want that when people visits http://www.example.com,http://example.com and https://www.example.com it redirects to https://example.com, What can I do? thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第二个
服务器
块包含一个循环:$ host
的值与请求的域相同。该块的目的是将请求的域从www.example.com
更改为example.com
。最简单的解决方案是更改
返回
语句,并明确指定最终域名:The second
server
block contains a loop:The value of
$host
is the same as the requested domain. The purpose of this block is to change the requested domain fromwww.example.com
toexample.com
.The simplest solution is to change the
return
statement and explicitly specify the final domain name: