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

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

如302 loop, 找不到網頁

 

1. 302 loop解決方法

參考網址: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

 

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

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

 

基礎nginx 設定

參考網址:https://blog.dtask.idv.tw/Nginx/2018-07-31/

https://dotblogs.com.tw/eric_obay_talk/2018/08/03/112217

# 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;
#}
}