2017-02-20 51 views
0

應用程序位於aws(elasticbeanstack)上的負載平衡器後面。在負載平衡之前它已經工作了。應用程序響應400錯誤請求。關於這個話題有很多問題。我嘗試了很多。粘滯的會話,更改上游代理http://localhost,連接升級......但我無法弄清楚。下面是nginx的error.log中:Sails.js套接字連接400錯誤請求

* 1217連接()失敗(111:連接 拒絕),同時連接到上游,客戶端:172.31.31.90,服務器:, 請求:「GET /socket.io /?__sails_io_sdk_version=0.13.8 & __sails_io_sdk_platform =瀏覽器& __sails_io_sdk_language = JavaScript的& EIO = 3 &運輸=輪詢&噸= LfS5yzc & SID = i1QLZeltxMS0D5oBAAAE HTTP/1.1" ,上游: 「http://127.0.0.1:8081/socket.io/?__sails_io_sdk_version=0.13.8&__sails_io_sdk_platform=browser&__sails_io_sdk_language=javascript&EIO=3&transport=polling&t=LfS5yzc&sid=i1QLZeltxMS0D5oBAAAE

Nginx confifarions;

user nginx; 
worker_processes auto; 

error_log /var/log/nginx/error.log; 

pid  /var/run/nginx.pid; 

events { 
    use epoll; 
    worker_connections 1024; 
    multi_accept on; 
} 

http { 

    port_in_redirect off; 
    include  /etc/nginx/mime.types; 
    default_type application/octet-stream; 

    log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 
         '$status $body_bytes_sent "$http_referer" ' 
         '"$http_user_agent" "$http_x_forwarded_for"'; 

    access_log /var/log/nginx/access.log main; 

    sendfile on; 
    tcp_nopush on; 
    tcp_nodelay on; 

    # Cache modifikasyonları 
    open_file_cache   max=10000 inactive=10m; 
    open_file_cache_valid 20m; 
    open_file_cache_min_uses 1; 
    open_file_cache_errors on; 

    # Transfers 
    client_body_buffer_size 10K;   # client POST buffer size 
    client_header_buffer_size 1k;   # client header size 
    client_max_body_size 20M;    # allowed size for a client request 
    large_client_header_buffers 2 1k;  # number and size of buffers for large client headers 

    # Timeouts 
    client_body_timeout 30; 
    client_header_timeout 30; 
    keepalive_timeout 65; 
    send_timeout 10; 

    # Elastic Beanstalk Modification(EB_INCLUDE) 

    log_format healthd '$msec"$uri"' 
         '$status"$request_time"$upstream_response_time"' 
         '$http_x_forwarded_for'; 

    include /etc/nginx/conf.d/*.conf; 

    # End Modification 
} 

服務器:

upstream nodejs { 
    server 127.0.0.1:8081; 
    keepalive 256; 
} 

server { 
    listen 8080; 

    location/{ 
     proxy_pass http://nodejs; 
     #proxy_set_header Connection ""; 
     proxy_http_version 1.1; 
     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 Upgrade $http_upgrade; 
     proxy_set_header Connection "upgrade"; 

     proxy_buffers 8 24k; 
     proxy_buffer_size 2k; 
    } 

    gzip on; 
    gzip_comp_level 4; 
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; 
} 

回答

1

走進了elasticbeanstack儀表板和點擊爲您的應用環境。

左邊是一個垂直列表,其中一個是你應該點擊的配置。向下滾動並找到「負載平衡」框,單擊該框右上角的編輯工具以配置負載平衡器。

在這種形式下,第二個字段應該標記爲Protocol,並有兩個選項:HTTP(應該選擇)和TCP。選擇TCP並向下滾動並保存。

還確保在您的ec2 AWS Admin> EC2>安全組中打開您正在使用的端口(8081)。

可能會幫助的最後一件事是在您的應用的根目錄中創建一個名稱爲.ebextensions的目錄。然後在其中創建 file.config一個文件,這個內容添加到文件:

files: 
    "/etc/nginx/conf.d/websocketupgrade.conf" : 
     mode: "000755" 
     owner: root 
     group: root 
     content: | 
      proxy_set_header  Upgrade   $http_upgrade; 
      proxy_set_header  Connection  "upgrade"; 

如果所有的不工作,你不想打擾自己,只是禁止nginx的,大多數時候這將解決(400)問題。

希望它有幫助

+0

我做了,但它不起作用。 – user305015

+0

此外tcp監聽器給出「粘滯性選項不適用於TCP協議」警告。我不確定,但它意味着負載均衡器後套接字連接會丟失。編輯過的 – user305015

+0

,看上面。 – Sam