新增 LetsEncrypt 憑證破壞了 nginx 配置

新增 LetsEncrypt 憑證破壞了 nginx 配置

我有 Nginx,在 Ubuntu 16.04 上運行,並設定了多個虛擬主機。使用 certbot 新增 LetsEncrypt 憑證後,我無法再使用 www.example.com 造訪我的網站。如果我使用任一協定造訪 www.example.com,伺服器都會傳回預設伺服器區塊。這是我的 Nginx 設定檔中的伺服器區塊:

server {

listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
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

    root /var/www/example/public;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;

    server_name example.com www.example.com;

     location / {
    try_files $uri $uri/ /index.php?$query_string;
}

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}

location ~ /\.ht {
    deny all;
}
}
server {
listen 80;

server_name example.com www.example.com;
return 302 https://example.com$request_uri;
}

我想讓伺服器回應https://www.example.com或者http://www.example.com與目前的回應方式相同https://example.com

答案1

發布答案以幫助其他可能遇到相同問題的人。

請務必檢查所有虛擬主機檔案。就我而言,Certbot 在 digitalocean 虛擬主機檔案中為我網站的 www 版本編寫了一個伺服器區塊。我刪除了該伺服器區塊,上面的程式碼按預期工作。

您可以透過執行以下操作來測試您的配置以確定是否存在相同的問題:

  • 跑步sudo nginx -t
  • 如果上面返回有關伺服器名稱衝突的信息,請查找重複的伺服器區塊

我希望這有幫助

相關內容