我已经检查了很多类似问题的其他答案以及DigitalOcean等的教程,但没有成功。 对不起,可能是一个非常明显的问题 – 我是新手。
本质上,我需要设置redirect,以便http://, https:// www 。和http:// www 。 全部redirect到https://。
目前,https://正常工作,http://redirect到https://。 我需要redirectwwwstream量,因为SSL证书只configuration为非www。 但是,当我尝试将另一个服务器块添加到configuration中以将www(http和https)redirect到https://时,服务器将closures所有4个变体的连接。
这是我的configuration:
# HTTP - redirect to HTTPS server { listen 80; server_name www.example.com example.com; return 301 https://example.com$request_uri; } # HTTPS www - redirect to non-www RESETS CONNECTION #server { # listen 443; # server_name www.example.com; # return 301 https://example.com$request_uri; #} # HTTPS — proxy all requests to the Node app server { # Enable HTTP/2 listen 443 ssl http2; server_name example.com; # Use the Let's Encrypt certificates ssl_certificate /etc/not/my/path/live/example.com/fullchain.pem; ssl_certificate_key /etc/not/my/path/live/example.com/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://0.0.0.0:2368/; proxy_ssl_session_reuse off; proxy_set_header Host $http_host; proxy_cache_bypass $http_upgrade; proxy_redirect off; } }
谁能告诉我我做错了什么? 我再次知道这可能是显而易见的,但我不能得到这个工作。 我应该用NGINXredirect以外的其他方法来做这个吗?
除非您拥有www.example.com的证书,否则您不能将www.example.com httpsstream量redirect到example.com 。 SSL协商使用域名,redirect只有在SSL协商完成后才会发生。
server { server_name www.example.com example.com listen 80; location / { rewrite ^/(.*)$ https://example.com/$1 permanent; } } server { server_name www.example.com; listen 443 ssl; [... ssl configuration ...] location / { rewrite ^/(.*)$ https://example.com/$1 permanent; } } server { server_name example.com; listen 443 ssl; [ ... main block ... ] }