我尝试将来自非本地主机连接的所有请求从 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]
这不行
更新一
我把这个:
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 的 varnish 请求
也许在 nginx 中添加一些 var( header ?),如果请求在 apache htacces 中没有此 header ,则重定向到 https。执行此操作的最佳方法是什么?
更新 4 我设置我的 nginx -
add_header Mea-request nginx;
和
在 Apache
RewriteCond %{HTTP:Mea-request} != ^nginx$
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule (.*) https://%1%{REQUEST_URI} [L,R=301]
但是还是不行
我觉得这条线不对
RewriteCond %{HTTP:Mea-request} != ^nginx$
最佳答案
工作版本:
在 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]
任何人都知道更好的方法,请提供信息。
关于apache 将非本地连接的 http 重写为 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37545873/