在角色为负载均衡器的domain1.com和domain2.com之间进行Apache重定向

时间:2018-09-30 11:42:10

标签: apache amazon-ec2 url-redirection centos7 amazon-elb

我有两个相同的应用程序绑定到端口3000,使用经典的负载平衡器在2个EC2服务器之间切换以进行故障控制。对于这两个应用程序,我们使用domain1.com(与ELB DNS链接)和domain2.com(与ELB DNS链接)。现在,我必须在apache级别设置重定向,尝试使用RewriteEngine的几种组合,但是没有一种有效。有没有人曾经使用过类似的结构或其他人可以提供帮助?

<?php
$hostname = 'localhost';
$username = 'root';
$password = '';
$db_name = 'somedb';
$conn = mysqli_connect($hostname, $username, $password, $db_name);
if (isset($_POST['abc'])){
    $date_today = date('Y-m-d');
    $selectStatement = "SELECT somecount FROM sometable WHERE today = '".$date_today."'";
    $selectResult = mysqli_query($conn, $selectStatemnt);
    $finalStatement = "";
    if($selectResult){
        $finalStatement = "UPDATE sometable SET somecount = somecount + 1 WHERE today = '".$date_today."'";
    }
    else{
        $finalStatement = "INSERT INTO sometable (today,somecount) VALUES ('".$date_today."', 1)";
    }
    mysqli_query($conn, $finalStatement);
}

?>

<form method="post" action="some.php">
    <input type="submit" name="abc">
</form>

<VirtualHost *:80> ServerName olddomain.com ServerAlias www.olddomain.com RewriteEngine On RewriteCond %{HTTP_HOST}!newdomain.com$ [NC] RewriteRule ^(.)$ newdomain.com/$1 [L,R=301] </VirtualHost>

1 个答案:

答案 0 :(得分:0)

我通过以下配置实现了购买;

<VirtualHost *:80>

ProxyPreserveHost On

RewriteEngine On

RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteCond %{HTTP_USER_AGENT} !ELB-HealthChecker/1.0
RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]

RewriteCond %{HTTP_HOST} !domain1.com$ [NC]
RewriteCond %{HTTP_USER_AGENT} !ELB-HealthChecker/1.0
RewriteRule ^(.*)$ https://www.domain1.com%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_USER_AGENT} !ELB-HealthChecker/1.0
RewriteRule ^(.*)$ https://www.domain1.com%{REQUEST_URI} [R=301,L]


ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/