Nginx从非www重定向到www(以及从http重定向到https)

时间:2019-06-28 07:00:10

标签: ubuntu nginx ssh devops

这是我的nginx conf:

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

# HTTPS — proxy all requests to the Node app
server {
    # Enable HTTP/2
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name www.example.co.il;

    # Use the Let’s Encrypt certificates
    ssl_certificate /etc/letsencrypt/live/www.example.co.il/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/www.example.co.il/privkey.pem;

    # Include the SSL configuration from cipherli.st
    include snippets/ssl-params.conf;

    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:3000/;
        proxy_ssl_session_reuse off;
        proxy_set_header Host $http_host;
        proxy_cache_bypass $http_upgrade;
        proxy_redirect off;
    }
}

如您所见,对端口80(http)的每个请求都首先滑至https并保存主机名(域)并请求uri。

然后,我处理同一域的端口443(http)-在这种情况下,我将使用在端口3000(http://localhost:3000)上运行的节点服务器传输要处理的请求。

当我仅访问example.com时,我实际上移到了https://www.example.com,这很好。但是,当我使用https://example.com时,我将停留在相同的地址,而没有转到www地址。

该如何解决?

0 个答案:

没有答案