重定向www https站点到非www https在nginx中引起太多重定向

发布于 2025-01-22 11:54:36 字数 2705 浏览 1 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梨涡少年 2025-01-29 11:54:36

第二个服务器块包含一个循环:

server{
    listen [::]:443 ssl;
    listen 443 ssl;
    ...
    return 301 https://$host$request_uri;
}

$ host的值与请求的域相同。该块的目的是将请求的域从www.example.com更改为example.com

最简单的解决方案是更改返回语句,并明确指定最终域名:

return 301 https://example.com$request_uri;

The second server block contains a loop:

server{
    listen [::]:443 ssl;
    listen 443 ssl;
    ...
    return 301 https://$host$request_uri;
}

The value of $host is the same as the requested domain. The purpose of this block is to change the requested domain from www.example.com to example.com.

The simplest solution is to change the return statement and explicitly specify the final domain name:

return 301 https://example.com$request_uri;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文