使用 Certbot 从 Nginx 中删除 WWW

Remove WWW from Nginx with Certbot

我很困惑为什么我不能让 www 删除这个域名。在这个 Nginx 配置的 底部Remove WWW 部分。我不确定 Certbot 是否对此有任何问题,有人看到错误了吗?

该站点已上线,wwwnon www 均可用。我正在尝试获取 only www.好多年没问这个问题了,我可能生疏了--给铁皮人加油!

upstream jessered {
    server 127.0.0.1:3000;
}

server {

    server_name jesse.red www.jesse.red;
    root /var/www/jesse.red/;

    # ---------------------------------------------------------------
    # Location
    # ---------------------------------------------------------------
    location / {
        proxy_pass          http://jessered;
        #proxy_http_version  1.1;                                                       
        proxy_set_header    Upgrade $http_upgrade;
        proxy_set_header    Connection 'upgrade';
        proxy_set_header    Host $host;
        proxy_set_header    X-Real-IP $remote_addr;
        proxy_set_header    X-forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header    X-Forwarded-Proto $scheme;
        proxy_cache_bypass  $http_upgrade;
        proxy_read_timeout  90;
    }

    # ---------------------------------------------------------------
    # SSL
    # ---------------------------------------------------------------
    listen 443 ssl http2; # managed by Certbot
    gzip off;

    ssl_certificate /etc/letsencrypt/live/www.jesse.red/fullchain.pem; # managed by Certbot  
    ssl_certificate_key /etc/letsencrypt/live/www.jesse.red/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

    error_log /var/www/errors/jesse.red.error.log;
}

# ---------------------------------------------------------------
# Force SSL
# ---------------------------------------------------------------
server {
    if ($host = www.jesse.red) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = jesse.red) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
}


# ---------------------------------------------------------------
# Remove Non-www
# ---------------------------------------------------------------
server {
  listen 443 ssl http2;
  server_name www.jesse.red;
  return 301 $scheme://jesse.red$request_uri;
}

如果您只想使用 jesse.red 而不是 www.jesse.red,则不要在 server_name 定义中包含 www.jesse.red

您的 if 块不是必需的且不正确,因为如果 $host = www.jesse.redreturn 301 https://$host$request_uri 仍将是 return 301 https://www.jesse.red/$request_uri

您的整体配置可以稍微整理一下:

# Force SSL
server {
    listen 80;
    server_name jesse.red;
    root /var/www/jesse.red/;
    return 301 https://$host$request_uri;
}

# SSL configuration
server {
    listen 443 ssl http2; # managed by Certbot
    gzip off;
    ssl on;

    ssl_certificate /etc/letsencrypt/live/www.jesse.red/fullchain.pem; # managed by Certbot  
    ssl_certificate_key /etc/letsencrypt/live/www.jesse.red/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

    error_log /var/www/errors/jesse.red.error.log;

    location / {
        proxy_pass          127.0.0.1:3000;
        #proxy_http_version  1.1;                                                       
        proxy_set_header    Upgrade $http_upgrade;
        proxy_set_header    Connection 'upgrade';
        proxy_set_header    Host $host;
        proxy_set_header    X-Real-IP $remote_addr;
        proxy_set_header    X-forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header    X-Forwarded-Proto $scheme;
        proxy_cache_bypass  $http_upgrade;
        proxy_read_timeout  90;
    }
}