Nginx重定向在将所有案例都重定向到https和非www时不起作用

问题描述 投票:0回答:1

我在这里找到了答案,但仍然可以使它起作用,我想要和他一样的东西:

https://example.com
https://www.example.com
http://example.com
http://www.example.com
example.com
www.example.com

to all redirect to `https://example.com`

这是我的配置文件

server {
    server_name example.com www.example.com;
    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl;
    server_name example.com;

    # 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
}

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

    server_name example.com;
    listen 80;
    listen 443 ssl;
    return 404; # managed by Certbot
}

除“ https://www.example.com”外,所有重定向都是正确的,它根本不重定向。

我在哪里做错了?请帮助!

nginx redirect certbot
1个回答
0
投票

您的cerbot生成的配置看起来很奇怪。如评论中所述。检查sudo nginx -t以查看是否有任何警告。

要重定向发送到https://www.example.com的客户端请求,您需要一个与www.example.com liste 443 ssl相匹配的服务器博客。请确保您拥有包括www.example.com在内的证书作为CN!

在此服务器博客中,您可以使用return 301 https://example.com$reuest_uri创建重定向”>

Certbot应该!如果端口80(非ssl)的配置正确,则为ssl生成正确的配置。

server {
server_name example.com;
listen 80;

...
}

server {
server_name www.example.com;
listen 80;

return 301 $scheme://example.com$request_uri;

...
}

这应该使用nginx certbot插件生成正确的ssl配置。

© www.soinside.com 2019 - 2024. All rights reserved.