Nginx重定向到www域不工作

Nginx重定向到www域不工作,nginx,Nginx,我有以下nginx配置 server { listen 80; listen [::]:80; listen 443 ssl; server_name example.com; return 301 https://www.example.com$request_uri; } 它重定向http://example.com至https://www.example.com 但不重定向https://example.com至https://www.examp

我有以下nginx配置

server {
    listen 80;
    listen [::]:80;
    listen 443 ssl;
    server_name example.com;
    return 301 https://www.example.com$request_uri;
}
  • 它重定向
    http://example.com
    https://www.example.com
  • 但不重定向
    https://example.com
    https://www.example.com

如何重定向
https://example.com
https://www.example.com

请将http和https流量分开。您当前的配置有点混乱。以下代码使用永久重定向重写从到的所有请求:

server {
   listen 80;
   server_name example.com;
   return 301 https://$server_name$request_uri;
}
第二个代码块将处理来自端口443的请求(这里的示例将在ssllabs.com上为您提供评级):

最后,在配置中使用第三个块,我们重写回:


希望这有帮助。

您是否在这台服务器上设置了有效的证书(不是主
www.-
一个?不是。证书仅用于www子域。这是一个问题吗?@VasfedIt可能是,因为ssl没有以这种方式完全配置。证书通常是为
www.
@
颁发的,所以请尝试复制其配置,即使它无效-您至少应该得到一个警告,而不是警告。)静默不工作我已使用指向证书的路径更新配置,但仍不工作。证书对www.and有效@
server {
   listen 443 ssl;
   server_name example.com;

   ssl_certificate /path_to/ssl.crt;
   ssl_certificate_key /path_to/ssl.key;
   ssl_session_timeout 1d;
   ssl_session_cache shared:SSL:10m;
   # ssl_session_tickets off;

   # openssl dhparam -out dhparam.pem 2048
   # ssl_dhparam /etc/nginx/SSL/dhparams.pem;

   ssl_protocols TLSv1.1 TLSv1.2;
   ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGC$
   ssl_prefer_server_ciphers on;

   add_header Strict-Transport-Security "max-age=15768000;includeSubdomains; preload";

   root /srv/wwwroot/;
   index index.html index.htm index.php;

   client_max_body_size 20M;

   location / {
       # your special config if needed
   }


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