从上游Ruby项目读取响应头时,上游过早关闭连接

时间:2018-03-27 08:44:30

标签: ruby-on-rails nginx google-cloud-platform

我正在尝试使用NGINX在GCP上反转代理ruby项目,我的/etc/nginx/sites-available/default文件看起来像这样

 server {
     large_client_header_buffers 4 16k;
     listen 80 default_server;
     #server_name my-devops-staging.com  
     listen [::]:80 default_server;
     #return 301 https://$host$request_uri;
     # SSL configuration
     #
     listen 443 ssl default_server;
     listen [::]:443 ssl default_server;
     ssl_certificate           /etc/nginx/certificate.crt;
     ssl_certificate_key       /etc/nginx/key.key;
     ssl off;
     ssl_session_cache  builtin:1000  shared:SSL:10m;
     ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
     ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
     ssl_prefer_server_ciphers on;
     # Note: You should disable gzip for SSL traffic.
     # See: https://bugs.debian.org/773332
     #
     # Read up on ssl_ciphers to ensure a secure configuration.
     # See: https://bugs.debian.org/765782
     #
     # Self signed certs generated by the ssl-cert package
     # Don't use them in a production server!
     #
     # include snippets/snakeoil.conf;
     root /var/www/html;
     # Add index.php to the list if you are using PHP
     index index.html index.htm index.nginx-debian.html;
     location / {
         proxy_set_header        Host $server_name;
         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;
         # Fix the “It appears that your reverse proxy set up is broken" error.
         proxy_pass http://localhost:8080;
         # proxy_read_timeout 300;
         proxy_read_timeout  9000;
         proxy_request_buffering off;
         proxy_buffering off;
         proxy_redirect off;
     }
}

每当我跑

时,我会做错什么
$ sudo service nginx restart

我在error.log

中收到了这些错误
2018/03/27 08:32:50 [error] 2959#2959: *64 upstream prematurely closed connection while reading response header from upstream, client: 130.211.2.175, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "my-devops-staging.com" 
2018/03/27 08:32:53 [error] 2959#2959: *66 upstream prematurely closed connection while reading response header from upstream, client 130.211.2.87, server: , request: "GET / HTTP/1.1", upstream: http://127.0.0.1:8080/", host: "my-devops-staging.com"

2 个答案:

答案 0 :(得分:1)

您实际上已经超时连接到Rails应用程序,您可以将proxy_read_timeout降低到300而将proxy_connect_timeout降低到与之匹配,如果它继续发生则更高。只需在location \ {...}块中添加这些行:

 location / {
     proxy_set_header        Host $server_name;
     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;
     proxy_pass http://localhost:8080;
     proxy_read_timeout 300; # Reducing this
     proxy_connect_timeout 300;  # Adding this
     proxy_request_buffering off;
     proxy_buffering off;
     proxy_redirect off;
 }

答案 1 :(得分:1)

通过将以下参数添加到代理Nginx配置文件中,使代理与后端之间的通信更加忠诚:

location / {

    proxy_http_version 1.1; // you need to set this in order to use params below.

    proxy_temp_file_write_size 64k;
    proxy_connect_timeout 10080s;
    proxy_send_timeout 10080;
    proxy_read_timeout 10080;
    proxy_buffer_size 64k;
    proxy_buffers 16 32k;
    proxy_busy_buffers_size 64k;
    proxy_redirect off;
    proxy_request_buffering off;
    proxy_buffering off;
    proxy_pass <whatever_here>;
}

我从对我们有用的生产环境中获得了神奇的数字。您可能需要考虑更改这些数字以适合您的环境和连接数等。

我希望这会有所帮助。