【问题标题】:Upstream prematurely closed connection while reading response header from upstream Ruby project从上游 Ruby 项目读取响应标头时,上游过早关闭连接
【发布时间】:2018-03-27 08:44:30
【问题描述】:

我正在尝试使用 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"

【问题讨论】:

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


    【解决方案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>;
    }
    

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

    我希望这会有所帮助。

    【讨论】:

    • 你的环境好吗?
    【解决方案2】:

    您实际上在连接到您的 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;
     }
    

    【讨论】:

      猜你喜欢
      • 2018-09-21
      • 1970-01-01
      • 2019-01-17
      • 2018-03-12
      • 1970-01-01
      • 2012-08-15
      • 1970-01-01
      • 2014-04-02
      • 2018-06-11
      相关资源
      最近更新 更多