2016-11-14 39 views
1

我在AWS ELB後面有一個帶有NGINX反向代理的Rails應用程序。我在ELB上終止SSL,並且我已經配置NGINX來強制任何嘗試將HTTP重寫爲HTTPS。這種設置工作正常,但我也通過ECS提供站點服務,並且由於ELB運行狀況檢查位於HTTP端口80上,所以當它獲得重定向並返回301時,ELB運行狀況檢查失敗,實例被註銷。NGINX強制所有人都使用SSL,但健康檢查文件?

如何設置NGINX通過HTTPS發送除健康檢查文件以外的所有健康檢查文件?

這裏是nginx.conf我的服務器塊:

server { 
     listen 80; 

     server_name localhost; 

     root /var/www/html; 

     location ~ ^elbcheck\.html$ { 
      proxy_set_header X-Real-IP $remote_addr; 
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
      proxy_set_header Host $http_host; 
      proxy_redirect off; 
      proxy_pass http://rails_app; 
      break; 
     } 

     location/{ 
      proxy_redirect off; 
      proxy_next_upstream error; 

      if ($http_x_forwarded_proto != "https") { 
      rewrite^https://$host$request_uri? permanent; 
      } 

      try_files $uri $uri/ @proxy; 
     } 

     location ~* \.(jpg|jpeg|svg|png|gif|ico|css|js|eot|woff|woff2|map)$ { 
      proxy_cache APP; 
      proxy_cache_valid 200 1d; 
      proxy_cache_valid 404 5m; 
      proxy_ignore_headers "Cache-Control"; 
      expires 1d; 

      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 https; 
      add_header X-Cache-Status $upstream_cache_status; 

      proxy_pass http://rails_app; 
     } 

     location @proxy { 
      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 https; 

      proxy_pass http://rails_app; 
     } 
    } 

回答

0

找到一個簡單的答案,在this post偉大的工作。這裏是@ceejayoz建議有:

server { 
    location /elb-status { 
    access_log off; 
    return 200; 
    } 
} 

似乎是工作 - ECS尚未終止我的服務因未能健康檢查了。

+0

這種方法的問題是,nginx的將響應運行狀況檢查,而不是rails應用程序,所以它繞過健康檢查的目標之一:去除Rails服務器沒有響應的不健康實例。你可能會遇到nginx響應的情況,但是rails應用程序沒有響應,你的用戶仍然會被重定向到這個錯誤的實例。 – Augusto

0

我有同樣的問題,並發現某處這個答案在互聯網上(沒有來源了,這是前一段時間)

server { 
    listen 80; 

    set $redirect_to_https 0; 
    if ($http_x_forwarded_proto != 'https') { 
     set $redirect_to_https 1; 
    } 

    if ($request_uri = '/status') { 
     set $redirect_to_https 0; 
    } 

    if ($redirect_to_https = 1) { 
     return 301 https://$host$request_uri; 
    } 

    ... 
}