node.js https/www 在 nginx 上重定向到 https 非 www

node.js https/www 在 nginx 上重定向到 https 非 www

我在 node.js 应用程序中使用了 nginx。我使用了Digitalocean 教程配置 nginx 和 https 使用。目前,以下内容有效:

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

但是当我输入 https:// www.example.com 时,它不会重定向到 https:// example.com

这是我的 nginx congif 在 sites-available/default 上

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

# HTTPS - proxy requests on to local Node.js app:
server {
        listen 443;
        server_name ezplan.ir;

        ssl on;
        # Use certificate and key provided by Let's Encrypt:
        ssl_certificate /path/to.crt;
        ssl_certificate_key /path/to.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_ciphers 'SSL STUFF';

        # Pass requests for / to localhost:8080:
        location / {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-NginX-Proxy true;
                proxy_pass http://localhost:8080/;
                proxy_ssl_session_reuse off;
                proxy_set_header Host $http_host;
                proxy_cache_bypass $http_upgrade;
                proxy_redirect off;
        }
}

有人可以帮我做这个重定向吗?

答案1

您可以为 https 设置两个块 - 带有和不带有 www:

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

server {
    listen 443;
    server_name example.com;
    // ...
    // your normal config here - proxy to node and all that
}

相关内容