【问题标题】:Nginx Redirect HTTP to HTTPS and WWW to Non-WWWNginx 将 HTTP 重定向到 HTTPS,将 WWW 重定向到非 WWW
【发布时间】:2016-01-07 23:26:43
【问题描述】:

我遇到了这个配置的问题:

#=========================#
# domain settings #
#=========================#

# Catch http://domain, and http://www.domain
server {
        listen 80;
        server_name www.domain domain;

        # Redirect to https://domain
        return 301 https://domain$request_uri;
}

# Catch https://www.domain
server {
        listen 443;
        server_name www.domain;

        # Redirect to https://domain
        return 301 https://domain$request_uri;
}

# Catch https://domain
server {
        listen 443;
        server_name domain;

        root /usr/share/nginx/domain;
        index index.html index.htm;

        ssl on;
        ssl_certificate /etc/nginx/ssl/server.crt;
        ssl_certificate_key /etc/nginx/ssl/server.key;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
        ssl_prefer_server_ciphers on;

        location / {
                try_files $uri $uri/ =404;
        }
}

第三个服务器指令有问题。我收到 SSL 连接错误。但是当我评论我们的那个部分时,一切正常。但我也希望 www 也通过 https 重定向到非 www

谁能发现问题?

【问题讨论】:

    标签: redirect web nginx https


    【解决方案1】:

    添加

    ssl on;
    ssl_certificate /etc/nginx/ssl/server.crt;
    ssl_certificate_key /etc/nginx/ssl/server.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
    ssl_prefer_server_ciphers on;
    

    在第三个服务器指令中修复了这个问题。

    【讨论】:

      【解决方案2】:

      下面的 Nginx 配置 sn-p 将使您能够有效地将所有 http 流量重定向到 https,同时剥离任何最终的 www 前缀。

      因此,您的网站将严格通过 https 访问,并且没有 www 前缀。

      server {
          listen 80 default_server;
          listen [::]:80 default_server;
      
          server_name www.example.com example.com;
      
          return 301 https://example.com$request_uri;
      }
      
      server {
          listen 443 ssl http2;
          listen [::]:443 ssl http2;
      
          if ($host = www.example.com) {
              return 301 https://example.com$request_uri;
          }
      
          server_name www.example.com example.com;
      
          # SSL configuration
          # Other configurations
      }
      

      关于if is evil,请注意使用if 指令是安全的,因为它不在location 上下文中使用。

      【讨论】:

        猜你喜欢
        • 2018-02-02
        • 2017-09-03
        • 1970-01-01
        • 2015-10-23
        • 2017-11-24
        • 2017-03-24
        • 1970-01-01
        • 2018-04-22
        • 1970-01-01
        相关资源
        最近更新 更多