【问题标题】:nginx rewrite rules for only SSL仅适用于 SSL 的 nginx 重写规则
【发布时间】:2016-03-10 20:38:44
【问题描述】:

我想将域的所有流量重定向到一个目标: https://example.com 我们想把 http 改成 https,把 www 改成 nonwww。

Nginx 1.8.1 是服务器

这是虚拟主机:

server {
listen xxx.xxx.xxx.xxx:80;
listen xxx.xxx.xxx.xxx:443 ssl;

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_certificate /www/clients/client1/web2/ssl/example.com.crt;
ssl_certificate_key /www/clients/client1/web2/ssl/example.com.key;

server_name example.com www.example.com;

root   /var/www/example.com/web;
#This is a rewrite from www.example.com  -> example.com
if ($http_host = "www.example.com") {
rewrite ^ $scheme://example.com$request_uri? permanent;
} 

......
......
}                        

我们遇到的问题是,我们检查的每个重定向和重写规则都适用于这三种情况:

https://example.com    -->     is right target        works
http://www.example.com -->     https://example.com  works
http://example.com     -->     https://example.com  works

但是

https://**www**.example.com  ---> https://example.com  don't works 

在浏览器中,我们看到 https://www.example.com 而不是目标 SSL 域名https://example.com

在这种情况下,我们的 SL 证书显示“不受信任” - 消息

vhost的配置由ISPConfig预设。

有人有同样的经历吗?也许是一个解决方案。

【问题讨论】:

    标签: redirect ssl nginx url-rewriting ispconfig


    【解决方案1】:

    您的证书很可能仅针对 example.com 颁发,对 www.example.com 无效。重定向,就像您在 NGINX 配置中的重定向一样,仅在您的浏览器抱怨的 TLS/HTTPS 握手之后发生。

    您需要请求您的证书颁发者颁发对 example.com 和 www.example.com 都有效的新证书。大多数发行人一开始就应该这样做并且不收取任何费用。

    【讨论】:

      【解决方案2】:

      这是我对我的域所做的事情。

      server {
          listen 80 default_server;
          listen [::]:80 default_server ipv6only=on;
          return 301 https://$host$request_uri$is_args$args;
          root /var/www/public_html;
          index index.php index.html index.htm;
      
          server_name domain.com www.domain.com;
          add_header  Strict-Transport-Security "max-age=31536000";
      
      
          location / {
              try_files $uri $uri/ /index.php?$args;      
          }
      
          location ~ \.php$ {
                  try_files $uri =404;
                  fastcgi_pass unix:/var/run/php5-fpm.sock;
                  fastcgi_index index.php;
                  include fastcgi_params;
              }
      
          location ~*  \.(jpg|jpeg|png|gif|ico|css|js|svg)$ {
                  expires 2d;
          }   
      }
      
      server {
          listen 443;
          add_header Strict-Transport-Security "max-age=31536000";
      
          root /var/www/public_html;
          index index.php index.html index.htm;
      
          server_name domain.com www.domain.com;
          ssl on;
          ssl_certificate /etc/ssl/ssl-bundle.crt;
          ssl_certificate_key /etc/ssl/myserver.key;
          ssl_dhparam /etc/ssl/dhparams.pem;
          ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
          #Disables all weak ciphers
          ssl_ciphers "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4";
      
          ssl_prefer_server_ciphers on;
          client_max_body_size 20M;
      
          location / {
              try_files $uri $uri/ /index.php?$args;      
          }
      
          location ~ \.php$ {
                  try_files $uri =404;
                  fastcgi_pass unix:/var/run/php5-fpm.sock;
                  fastcgi_index index.php;
                  include fastcgi_params;
              }
      
          location ~*  \.(jpg|jpeg|png|gif|ico|css|js|svg)$ {
                  expires 30d;
              }
      }
      

      顺便说一句,这个设置使我的域 ssl 在 ssltestlab 中为 A+

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-28
        • 2012-06-07
        • 2010-11-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多