[英]AWS Application Load Balancer unable to process login form request via Apache/MySQL DB
我已經部署了一個 AWS EC2 實例,其中部署了 Apache,該實例托管了一個簡單的注冊/登錄表單。 當用戶注冊時,他們的憑證存儲在部署在輔助 AWS EC2 實例上的 MySQL 數據庫中。 因此,當用戶點擊登錄頁面時,他們輸入了他們的憑據,PHP 代碼使用 MySQL DB 對這些用戶進行身份驗證,隨后允許用戶進入。
此設置部署在兩個 AZ 中,並且 MySQL DB 是 Master-Master 復制的。
我想在兩個 AWS EC2 (Apache) web 服務器前面放置一個應用程序負載均衡器,以解決其中一個 AZ 中的故障。 但是,當我部署 AWS Application Load Balancer 並使用 DNS 名稱登錄時,應用程序似乎沒有驗證用戶憑證並從表單中刪除用戶名/密碼(幾乎就像刷新頁面一樣)。
該應用程序已在 PHP 中構建,並且非常簡單,但我無法確定為什么在使用負載均衡器時我的應用程序不起作用?
有什么建議么??
我的身份驗證方法:
<?php
session_start();
// Change this to your connection info.
$DATABASE_HOST = 'XXX.XX.XX.XX';
$DATABASE_USER = 'XXX';
$DATABASE_PASS = 'XXX';
$DATABASE_NAME = 'XXX';
// Try and connect using the info above.
$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
if ( mysqli_connect_errno() ) {
// If there is an error with the connection, stop the script and display the error.
exit('Failed to connect to MySQL: ' . mysqli_connect_error());
}
// Now we check if the data from the login form was submitted, isset() will check if the data exists.
if ( !isset($_POST['username'], $_POST['password']) ) {
// Could not get the data that should have been sent.
exit('Please fill both the username and password fields!');
}
// Prepare our SQL
if ($stmt = $con->prepare('SELECT id, password FROM accounts WHERE username = ?')) {
// Bind parameters (s = string, i = int, b = blob, etc), in our case the username is a string so we use "s"
$stmt->bind_param('s', $_POST['username']);
$stmt->execute();
// Store the result so we can check if the account exists in the database.
$stmt->store_result();
if ($stmt->num_rows > 0) {
$stmt->bind_result($id, $password);
$stmt->fetch();
// Account exists, now we verify the password.
if ($_POST['password'] === $password) {
// Verification success! User has logged-in!
// Create sessions, so we know the user is logged in, they basically act like cookies but remember the data on the server.
session_regenerate_id();
$_SESSION['loggedin'] = TRUE;
$_SESSION['name'] = $_POST['username'];
$_SESSION['id'] = $id;
// echo 'Welcome ' . $_SESSION['name'] . '!';
header('Location: home.php');
} else {
// Incorrect password
echo 'Incorrect username and/or password!';
}
} else {
// Incorrect username
echo 'Incorrect username and/or password!';
}
$stmt->close();
}
?>
在沒有分布式 session 存儲或啟用粘性會話的情況下,您不能擁有多個要求用戶登錄的 web 服務器。 現在您的用戶 session 在多台服務器之間跳來跳去,但您只登錄到其中一台服務器。
解決此問題的最簡單方法是將 go 簡單地添加到負載均衡器設置中並暫時啟用粘性會話,因此所有用戶的請求都將“粘”到單個服務器上。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.