【问题标题】:apache rewrite http to https for non local connectionsapache 将非本地连接的 http 重写为 https
【发布时间】:2016-09-29 11:26:00
【问题描述】:

我尝试将来自非本地主机连接的所有请求从 http 重定向到 https。

我使用this combination https-> nginx -> varnish -> apache2 并希望将直接连接重定向到没有 ssl 的 apache

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{HTTP_HOST} !=localhost
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
    RewriteRule (.*) https://%1%{REQUEST_URI} [L,R=301]

这不行

更新 1

我把这个:

RewriteCond %{REMOTE_ADDR} !=localhost
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule (.*) https://%1%{REQUEST_URI} [L,R=301]

这是工作 - 浏览器中的 url 被重定向到 https:// 但我有无限循环,所以 apache 重定向到 https:// nginx 作为 http 的 apache 并重定向。如何解决?

更新 2

如果我换行

RewriteCond %{REMOTE_ADDR} !=localhost

RewriteCond %{REMOTE_ADDR} !=127.0.0.1

重定向停止工作

更新 3 这不起作用,因为我有

http -> 清漆 -> apache 要么 https -> nginx -> 清漆 -> apache

所以对于 apache 来说一直有 127.0.0.1 所以我需要知道这是直接来自 http 或 varnish 的清漆请求

也许在 nginx 中添加一些 var(头文件?),如果请求在 apache htacces 中没有这个头文件,则重定向到 https。最好的方法是什么?

更新 4 我设置了我的 nginx -

add_header Mea-request nginx;

在阿帕奇

RewriteCond %{HTTP:Mea-request} != ^nginx$
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule (.*) https://%1%{REQUEST_URI} [L,R=301]

但这仍然不起作用

我认为这条线不正确

RewriteCond %{HTTP:Mea-request} != ^nginx$

【问题讨论】:

    标签: apache .htaccess ssl nginx


    【解决方案1】:

    工作版本:

    在 nginx 中

    proxy_set_header X-APP_MEAREFER nginx;
    

    在清漆中

    set resp.http.X-APP_MEAREFER = req.http.X-APP_MEAREFER;
    

    在 apache2 中

    RewriteCond %{HTTP:X-APP_MEAREFER} !^nginx
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
    RewriteRule (.*) https://%1%{REQUEST_URI} [L,R=301]
    

    请以任何人知道更好的方式提供信息。

    【讨论】:

      猜你喜欢
      • 2017-12-04
      • 2018-02-28
      • 1970-01-01
      • 2014-12-16
      • 2014-03-04
      • 2015-01-10
      • 2014-01-23
      • 1970-01-01
      • 2015-06-18
      相关资源
      最近更新 更多