簡體   English   中英

Nginx重定向到www域無法正常工作

[英]Nginx redirect to www domain not working

我有以下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流量。 你當前的配置搞砸了一些東西。 以下代碼使用永久重定向將所有請求從http://example.com重寫為https://example.com

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

第二個代碼塊將處理來自端口443的請求(此處的示例將在ssllabs.com上為您提供A評級):

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
   }


 }

最后在我們的配置中使用第三個塊,我們將https://www.example.com重寫為https://example.com

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

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM