【问题标题】:NGINX Rewrite - HTTPS/wwwNGINX 重写 - HTTPS/www
【发布时间】:2014-10-21 16:32:02
【问题描述】:

我正在尝试获取我的网站,以便 domain.com 的每个变体都将重定向到 https://www.domain.com。我正在使用多种工具来执行此操作.. 一个是 NGINX 配置文件,其开头为:

server {
            listen  80;
            server_name domain.com;
            return       301 https://www.domain.com$request_uri;
    }
server {
            listen          80;
            server_name     localhost www.domain.com;
            root            /usr/local/nginx/html;

我还在我的 CDN 中设置了一条规则,通过 HTTPS 代理所有内容:

if ( $scheme = http ) { 
  REWRITE RULE ^ https://$http_host$request_uri permanent 
} 

几乎所有变体都有效,除了https://domain.com .. 它应该重定向到https://www.domain.com,但它不会加载。

【问题讨论】:

    标签: .htaccess redirect nginx https


    【解决方案1】:

    这样的事情可能会有所帮助:

    server {  # redirect _all_ http requests to https server
        listen 80;
        return  301 https://www.domain.com$request_uri;
    }
    
    server {
        listen 443 ssl;
        server_name localhost .domain.com ;
        root    /usr/local/nginx/html;
    
        if ($host = domain.com) {
            return  301 https://www.domain.com$request_uri;
        }
    }
    

    【讨论】:

      【解决方案2】:

      使用另一个端口而不是标准 80 的 HTTP 连接应该侦听端口 443。来自wikipedia

      HTTPS URL 以“https://”开头并默认使用端口 443,而 HTTP URL 以“http://”开头,默认使用端口 80。

      另请参阅:nginx HTTPS serving with same config as HTTP。从这个页面:

      server {
          listen 80;
          listen 443 default_server ssl;
      
          # other directives
      }
      

      另外这个页面有一个http/https解决方案:Nginx no-www to www and www to no-www

      【讨论】:

        猜你喜欢
        • 2018-04-22
        • 1970-01-01
        • 2020-03-21
        • 2016-03-10
        • 1970-01-01
        • 2019-06-04
        • 2018-06-23
        • 2015-10-23
        相关资源
        最近更新 更多