0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Magento]Load Balancer(負載平衡) for Magento

Posted at

#目的
這是一次安裝經驗紀錄,當magento 使用nginx 做]Load Balancer(附載平衡)且將ssl安裝在Load Balancer機器上,所碰上的幾個問題

302 loop, 找不到網頁

#安裝nginx

1 . 安裝EPEL

yum install epel-release

2 . 安裝 nginx

yum install nginx

3 . 在 /etc/nginx/conf.d/ 中新增一個 lb.conf file

4 . 加入以下設定

# upstream 用於定義load balance
# 預設行為為round robin 這邊用權重來當範例
# ej為自行定義的名
upstream ej {
  server 192.168.10.9:9000 weight=1;
  server 192.168.10.8:9000 weight=3;
 
}
#定義nginx port 443
#使用者連線 yourdomain:443會自動導入到後端兩台web server
#proxy_pass 負載平衡接收到需求時會導向哪台機器
server {
  listen 443 ssl;
server_name yourdomain;

    ssl_certificate /etc/ssl/certs/yourdomain_com_tw.crt;
    ssl_certificate_key /etc/ssl/certs/ssl.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    location / {
        proxy_pass http://ej;
        proxy_set_header   Host    $host;
        proxy_set_header   X-Real-IP   $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }

#location / {
#  proxy_pass http://ej;
#}
}

#magento 端設定問題

當我只設定上面的Load Balancer後發現會卡在無限的302 redirect loop,後來翻查了文章發現是因為magento
在前面加入一層load balacer後並不知道前面已經使用了ssl所以需要加一段告訴magento讓他知道不需重新導向443的連結
在index.php Mage::run($mageRunCode, $mageRunType);前加入以下程式碼

if( isset($_SERVER['HTTP_X_FORWARDED_PROTO']) ) {
    $_SERVER['HTTPS'] = 'on';
    $_SERVER['SERVER_PORT'] = 443;
}

Mage::run($mageRunCode, $mageRunType);

參考文章(reference):
https://blog.dtask.idv.tw/Nginx/2018-07-31/
https://dotblogs.com.tw/eric_obay_talk/2018/08/03/112217
https://tech.webestimate.net/magento-has-a-redirect-loop-2/
https://naruzo.typepad.com/blog/2011/01/ssl-with-magento-behind-a-load-balancer.html
https://www.sonassi.com/blog/magento-kb/magento-https-redirect-loop-2

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?