【问题标题】:Rewrite single page https to http Nginx将单页 https 重写为 http Nginx
【发布时间】:2011-06-30 17:12:35
【问题描述】:

我想将“https://www.example.com”和“https://example.com”重写为“http://www.example.com”或“http://example.com”分别'。我只想写网站的特定部分,而不是任何子文件夹或 php 相关页面。我该怎么做?

【问题讨论】:

    标签: php ssl joomla nginx rewrite


    【解决方案1】:

    我在另一个方向使用它(没有在这个方向测试,但应该可以工作)。

    if ( $scheme = https )
    {
      rewrite ^ http://$host$uri;
    }
    

    编辑:限制到一个位置并结束不要尝试重写更多:

    location / {
      if ( $scheme = https )
      {
        rewrite ^ http://$host$uri last;
      }
    }
    

    【讨论】:

    • 我需要它只在人们在 url 而不是任何其他页面时重写。该指令重写整个网站的网址。
    • 这种做法太糟糕了...wiki.nginx.org/Pitfallswiki.nginx.org/IfIsEvil
    • @Ownatik:来自您引用的页面:“在location 上下文中,可以在if 内完成的唯一100% 安全的事情是:return ...;rewrite ... last;”。这里是第二种情况,你有更好的解决方案吗?
    【解决方案2】:

    我使用了类似于以下内容的东西,它避免了 IfIsEvil 并使用 return 而不是 rewrite 。但是它失败了DRY,这让我很困扰.. 欢迎提出建议。

    server {
        listen 80;
        server_name .example.com;
        index index.html index.htm index.php;
    
        location / {
            try_files $uri $uri/ /index.php?$args;
        }
    
        location ~ \.php$ {
            fastcgi_pass   unix:/var/run/php5-fpm.sock ;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
    
    server {
        listen              443 ssl;
        server_name         .example.com;
        ssl_certificate     /etc/ssl/certs/example.com.cert;
        ssl_certificate_key /etc/ssl/private/example.com.key;
    
        index index.html index.htm index.php;
    
        # Rewrite only / to http
        location = / {
            return 301 http://$server_name$request_uri;
        }
    
        location / {
            try_files $uri $uri/ /index.php?$args;
        }
    
        location ~ \.php$ {
            fastcgi_pass   unix:/var/run/php5-fpm.sock ;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
    

    另见

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-18
      • 2015-01-08
      • 2012-02-28
      • 2014-03-04
      • 1970-01-01
      • 2014-09-20
      • 2011-06-02
      • 2011-09-15
      相关资源
      最近更新 更多