如何使用用于HTTPS和Nginx的certbot将www重定向到非www

魔盒

我正在尝试将www重定向到非www,但是它不起作用。我已经尝试过类似问题的各种答案,但都没有用。

我有使用certbot的3个域的SSL证书example.comwww.example.com并且admin.example.com

这是我当前的配置,适用于非www和admin,但是www.example.com不起作用。

# HTTP - redirect all requests to HTTPS
server {
    listen 80;
    listen [::]:80;
    return 301 https://$host$request_uri;
}

# Redirect to non-www
server {
    server_name www.example.com;
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.se/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.se/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://example.com$request_uri;
}

# non-www
server {

    server_name example.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.se/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.se/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

}
# CMS
server {

    server_name admin.example.com;

    location / {
        proxy_pass http://localhost:1337;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.se/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.se/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
}

我使用DigitalOcean,其中admin和非www都指向我的Droplet,而www.example.com的CNAME记录指向example.com(非www)。

行星普拉纳夫

首先,www.example.com和example.com应该在一个服务器块中。

其次,您需要在#non-www服务器配置博客中添加它

if ($host = 'www.example.com') {
        return 301 https://example.com$request_uri;
}

第三,要将所有请求重定向到HTTPS,server_name必须在您的# HTTP - redirect all requests to HTTPS块中添加

最后,您的NGINX配置文件将如下所示

# HTTP - redirect all requests to HTTPS
server {
    server_name example.com www.example.com admin.example.com;
    listen 80;
    listen [::]:80;
    return 301 https://$host$request_uri;
}

# non-www
server {

    server_name example.com www.example.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    if ($host = 'www.example.com') {
        return 301 https://example.com$request_uri;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.se/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.se/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

}
# CMS
server {

    server_name admin.example.com;

    location / {
        proxy_pass http://localhost:1337;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.se/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.se/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
}

更新NGINX配置文件后,重新启动NGINX:

$ sudo systemctl restart nginx

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

nginx将非www重定向到www和https

NGINX:将非www https重定向到https:// www

Nginx重定向/重写:www到非www和https

Nginx:将非www重定向到www https

将Nginx HTTP / HTTPS www重定向到非www

如何使用htaccess将www和非www重定向到特定域的https

NGINX 将 http 重定向到 https 和 www。到非 www

Nginx将非www重定向到domain.com和子域的www和https

使用通配符支持将非https和非www重定向到https:// www

如何将www重定向到非www https?

将 www https 站点重定向到非 www https 导致 nginx 中的重定向过多

如何使用.htaccess将HTTPS非www重定向到HTTPS www?

Nginx重定向(非www到www)不能与Certbot一起使用

如何将非www重定向到具有多个站点的www?(Nginx和Varnish)

如何通过web.config将http重定向到https和www重定向到非www?

重定向www。到非www并使用.htaccess将所有http重定向到https

将任何域从非 www 重定向到 www 和 HTTPS,无需双重重定向

Nginx-(http / https)非www到www重定向

将www和非www都重定向到https non www

将非www和www http请求重定向到https:// www

将非www(http)和www(http)重定向到https:// www

NGINX将http重定向到https,将非www重定向到ww

Nginx重定向HTTP到HTTPS和WWW到非WWW

如何将http重定向到https和将https:// www重定向到https://

htaccess将301 https www重定向到非www

Nginx将http:// www和裸http / https重定向到https:// www

nginx 将 www 重定向到非 www

nginx将所有非WWW重定向到HTTPS WWW

Nginx 301将http(裸和www)重定向到https www,并将通配符子域重定向到https