在Ubunutu重新启动后,NginX不再将非wwwredirect到www

好的,所以我最近设置了Ubuntu(node.js)w。 Nginx和SSL(letsencrypt)在我的服务器上,让一切运行完美。

我有3个服务器块。

一切都按照上面的方式工作,但是,重新启动Ubuntu后,只有第三个(服务SSL)工作,现在完全忽略了第一和第二,尽pipeNginx运行没有错误(因此为什么第三块工程)。 所以它不再redirect非www到www和http到https,但是,当我直接从https访问它的工作完美。 不知道发生了什么事情,为什么这发生后重新启动。

以下是位于/ etc / nginx / sites-enabled / default中的代码:

# Redirect non-www to www server { server_name mydomain.com; return 301 https://$host$request_uri; } # HTTP - redirect all traffic to HTTPS server { listen 80; listen [::]:80 default_server ipv6only=on; return 301 https://$host$request_uri; } # HTTPS - proxy all requests to the Node app server { # Enable HTTP/2 listen 443 ssl http2; listen [::]:433 ssl http2; server_name www.mydomain.com; # Use the Let's Encrypt certificates ssl_certificate /etc/letsencrypt/live/www.mydomain.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/www.mydomain.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://localhost:3000/; proxy_ssl_session_reuse off; proxy_set_header Host @http_host; proxy_cache_bypass $http_upgrade; proxy_redirect off; } } 

 # Redirect non-www to www server { server_name mydomain.com; return 301 https://$host$request_uri; } 

您正在丢失来自此块的listen和SSL证书指令。 应该是这样的:

 # Redirect non-www to www server { listen 443 ssl http2; server_name mydomain.com; # Use the Let's Encrypt certificates ssl_certificate /etc/letsencrypt/live/www.mydomain.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/www.mydomain.com/privkey.pem; # Include the SSL configuration from cipherli.st include snippets/ssl-params.conf; return 301 https://$host$request_uri; } 

而且,在你的主服务器块中,你有:

 listen 443 ssl http2; listen [::]:433 ssl http2; 

你应该有这些:

 listen 443 ssl http2; listen [::]:443 ssl http2 ipv6only=yes;