将所有HTTP/HTTPS请求重定向到特定网站,然后返回

将所有HTTP/HTTPS请求重定向到特定网站,然后返回

问题描述:

我想要做什么(所有这些仅在一个服务器上执行);将所有HTTP/HTTPS请求重定向到特定网站,然后返回

(我正在与example.com合作不做任何广告)。

将所有传入HTTP/HTTPS请求(端口80和443)重定向到特定网站,例如filter.example.com。在那里我创建了自己的机制来过滤恶意请求。之后,请求应该返回到请求的网站。

我的问题是,每个请求都被重定向回过滤器,所以有一个无限循环。

你知道任何解决方案或可能替代(Nginx的)?

这是数据包流所显示的问题;

“User-Request = https://example.com” - >“Apache将其重定向到= https://filter.example.com” - >“获得过滤= https://example.com” - >“Apache将其重定向回来。

我真的很希望你能理解我的问题。

谢谢。

编辑:

这是我对filter.example.com服务器名称设置;

<VirtualHost *:80> 
    ServerName filter.example.com 
    RewriteEngine On 
    RewriteCond %{HTTPS} off 
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 
</VirtualHost> 

<VirtualHost *:443> 
    ServerName filter.example.com 
    RewriteEngine On 
    DocumentRoot /var/www/filter/ 
    SSLEngine On 
    SSLCertificateFile /etc/letsencrypt/live/filter.example.com/cert.pem 
    SSLCertificateChainFile /etc/letsencrypt/live/filter.example.com/chain.pem 
    SSLCertificateKeyFile /etc/letsencrypt/live/filter.example.com/privkey.pem 
    ErrorDocument 404 /error404.html 
    AddOutputFilterByType DEFLATE text/plain 
    AddOutputFilterByType DEFLATE text/html 
    AddOutputFilterByType DEFLATE text/xml 
    AddOutputFilterByType DEFLATE text/css 
    AddOutputFilterByType DEFLATE application/xml 
    AddOutputFilterByType DEFLATE application/xhtml+xml 
    AddOutputFilterByType DEFLATE application/rss+xml 
    AddOutputFilterByType DEFLATE application/javascript 
    AddOutputFilterByType DEFLATE application/x-javascript 
</VirtualHost> 

这里为我的 “真实” 的网站;

<VirtualHost *:80> 
    ServerName example.com 
    RewriteEngine On 
    RewriteCond %{HTTPS} off 
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 
</VirtualHost> 

<VirtualHost *:443> 
    ServerName example.com 
    RewriteEngine On 
    DocumentRoot /var/www/html/ 
    SSLEngine On 
    SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem 
    SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem 
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem 
    ErrorDocument 404 /error404.html 
    AddOutputFilterByType DEFLATE text/plain 
    AddOutputFilterByType DEFLATE text/html 
    AddOutputFilterByType DEFLATE text/xml 
    AddOutputFilterByType DEFLATE text/css 
    AddOutputFilterByType DEFLATE application/xml 
    AddOutputFilterByType DEFLATE application/xhtml+xml 
    AddOutputFilterByType DEFLATE application/rss+xml 
    AddOutputFilterByType DEFLATE application/javascript 
    AddOutputFilterByType DEFLATE application/x-javascript 
</VirtualHost> 

大卫写道:

您真的会在重定向循环中结束,因为www.example.com的请求1将重定向到filter.example.com,并无休止地再次返回到www.example.com。 为了避免这种情况,在filter.example.com中添加一个cookie/header到来自www.example.com的传入请求(当然,在完成过滤器过程之后),例如Filter:true,所以您知道这已经是过滤的请求并且不需要去filter.example.com。

server { 
    server_name filter.example.com; 
    //logic to filter 
    add_header 'passed_filter' 'true'; 
} 

如果您重定向逻辑增加一个检查核实,如果头过滤器:真实存在,如果是的如果不是重定向到filter.example.com, - 跳过重定向,并按照正常的执行过程。

//If the header is not set, then we understand that this request should be redirected to filter.example.com 
if($sent_passed_filter ~= 'true') { 
    //logic to redirect to filter 
} 

是Nginx,因为我使用的是Apache。是否也有类似的解决方案,但对于Apache?

您真的会在重定向循环中结束,因为www.example.com的请求1将重定向到filter.example.com,并无休止地再次返回到www.example.com。

为了避免这种情况,请在filter.example中为来自www.example.com的传入请求添加cookie /标头。com(当然,在完成过滤过程之后)类似Filter:true,所以你知道这已经是一个过滤的请求,并且不需要去filter.example.com。

server { 
    server_name filter.example.com; 
    //logic to filter 
    add_header 'passed_filter' 'true'; 
} 

在您重定向逻辑添加一个检查核实,如果头过滤器:真实存在,如果是的如果不是重定向到filter.example.com, - 跳过重定向并按照正常的执行过程。

//If the header is not set, then we understand that this request should 
be redirected to filter.example.com 
if($sent_passed_filter ~= 'true') { 
    //logic to redirect to filter 
} 
+0

我会试试看。谢谢。几分钟后,我会给你反馈。但标题是可伪造的,或者不是吗?问候。 – BERNARDO

+0

我编辑了我的帖子,也许你可以帮忙。感谢您的时间。问候。 – BERNARDO

+1

问题不是由于nginx或apache,它的设置方式。根据您所解释的内容,我认为您希望将请求发送到过滤器,并在过滤器上发回请求。如果是这种情况,那么你需要标记你已经处理过的每一个请求,所以它不会以循环结束。 下面是你需要做的,以使其发挥作用,对于你在filter.example.com中处理的每一个请求,添加一个头来标识这个请求已经被处理,并且在filter.example.com中添加一个检查器来查看是否头是存在的,如果是的话,你不需要再次处理请求 – David