我在 Laravel 有一個專案,我正在使用帶有 nginx 的數字海洋,我嘗試將 www 重定向到非 www url,但我不能,這是我的 nginx 配置:
例子.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,我該怎么辦?謝謝。
uj5u.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;
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/461488.html
上一篇:AssertionError:302!=200:無法檢索重定向頁面“/api/v2/app/nextdialog”:回應代碼為302(預期為200)
