我真的不知道为什么我的 nginx 配置不适用于 www。
我的配置是:
server {
listen 80;
server_name postimg.cz www.postimg.cz;
return 301 https://$server_name$request_uri;
}
server {
listen 443;
server_name postimg.cz;
# SSL Configuration
ssl_certificate /etc/letsencrypt/live/postimg.cz/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/postimg.cz/privkey.pem;
ssl_session_cache shared:SSL:10m;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GC>
ssl_prefer_server_ciphers on;
# See https://hstspreload.org/ before uncommenting the line below.
# add_header Strict-Transport-Security "max-age=15768000; preload;";
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
add_header X-Robots-Tag none;
add_header Content-Security-Policy "frame-ancestors 'self'";
add_header X-Frame-Options DENY;
add_header Referrer-Policy same-origin;
root /var/www/postimg.cz;
# Disable access to sensitive application files
location ~* (app|content|lib)/.*\.(po|php|lock|sql)$ {
return 404;
}
location ~* composer\.json|composer\.lock|.gitignore$ {
return 404;
}
location ~* /\.ht {
return 404;
}
# Image not found replacement
location ~* \.(jpe?g|png|gif|webp)$ {
log_not_found off;
error_page 404 /content/images/system/default/404.gif;
}
# CORS header (avoids font rendering issues)
location ~* \.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$ {
add_header Access-Control-Allow-Origin "*";
}
# PHP front controller
location / {
index index.php;
try_files $uri $uri/ /index.php$is_args$query_string;
}
# Single PHP-entrypoint (disables direct access to .php files)
location ~* \.php$ {
internal;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
但当我去http://www.postimg.cz它不会重定向到https://postimg.cz这是为什么?你能帮我吗?
服务器:Ubuntu Server 20.04
编辑//也尝试了这个,但还是不起作用:
server {
listen 80;
server_name www.postimg.cz postimg.cz;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name www.postimg.cz;
ssl_certificate /etc/letsencrypt/live/www.postimg.cz/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.postimg.cz/privkey.pem;
return 301 https://postimg.cz$request_uri;
}
server {
listen 443 ssl;
server_name postimg.cz;
答案1
问题出在域名托管提供商。因为我忘记将 www 添加到我的服务器 IP。我只设置了 DNS,没有设置 www。我太笨了。