apache 将非本地连接的 http 重写为 https

标签 apache .htaccess ssl nginx

我尝试将来自非本地主机连接的所有请求从 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/

相关文章:

java - Mule ESB Riak 连接器 : How to configure HTTP over SSL endpoints

apache - 如何在 htaccess 中使用 RewriteCond 写入(A 和 B)或(C 和 D)?

php - 为 Mysqli 新连接设置 SSL 证书位置

php - 无法从命令行访问 phpmyadmin

wordpress - 在根域上安装 Shopify 并将 Wordpress 作为子文件夹安装有哪些挑战?

.htaccess - AWS ELB 在没有 index.php 的情况下将 HTTP 重定向到 HTTPS

.htaccess - 我的错误导致 SE 上的错误链接如何解决?

java - IBM MQ 的深度

node.js - 对于非 PHP/非静态文件网站,letsencrypt certbot-auto 的 "webroot-path"应该是什么?

java - 为什么 Tomcat 找不到我的 Web 应用程序?