Nginx

Nginx www 不工作

  • April 3, 2022

我真的不知道為什麼我的 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 伺服器 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;

問題出在域託管服務提供商上。因為我忘記將 www 定址到我的伺服器 IP。我只設置了沒有 www 的 DNS。我只是愚蠢。

引用自:https://serverfault.com/questions/1097676