Passenger & Nginx => 502 Bad Gateway Errors in Pro

2019-03-21 22:19发布

问题:

I am running Nginx 1.0.6, Passenger 3.0.9, Rails 3.1.1, Ruby 1.9. In my production environment I am seeing the following intermittent warnings in my nginx_error.log file:

2011/11/22 14:44:40 [warn] 23288#0: *474 an upstream response is buffered to a temporary file /opt/nginx/proxy_temp/2/00/0000000002 while reading upstream, client: 69.172.88.178, server: www.memverse.com, request: "GET /show_all_my_verses HTTP/1.1", upstream: "passenger:unix:/passenger_helper_server:", host: "www.memverse.com", referrer: "http://www.memverse.com/quick_add/7352"

and, less frequently, the following error which leads to a 502 Bad Gateway:

2011/11/21 15:35:20 [error] 2118#0: *60762 upstream prematurely closed connection while reading response header from upstream, client: 74.125.44.84, server: www.memverse.com, request: "GET /blogs/feed/9 HTTP/1.1", upstream: "passenger:unix:/passenger_helper_server:", host: "www.memverse.com"

I have crawled through both the Passenger and the Nginx documentation and have tweaked almost every setting in my nginx.conf file but all to no avail. I'm hoping that someone can at least point me in the right direction.

Below is my nginx.conf:

user  root;
worker_processes  4;

error_log  logs/error.log  error; # debug | info | notice | warn | error | crit
pid        logs/nginx.pid;

events {
    worker_connections  1024; # max_clients = worker_processes * worker_connections
}


http {
    include       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  logs/access.log  main;

    sendfile        on;

    keepalive_timeout  65;

    # Configure Passenger
    passenger_root /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.9;
    passenger_ruby /usr/local/bin/ruby;

    passenger_log_level 1;
    passenger_debug_log_file /home/avitus/logs/passenger.log;

    rails_framework_spawner_idle_time 0;
    rails_app_spawner_idle_time 0;

    passenger_pool_idle_time 0;
    passenger_use_global_queue on;
    passenger_max_pool_size 15;

    server {
       listen 80;
       server_name www.pariday.com pariday.com;
       root /home/avitus/pariday.com/current/public;
       access_log /home/avitus/pariday.com/current/log/nginx_access.log main;
       error_log /home/avitus/pariday.com/current/log/nginx_error.log info;
       passenger_enabled on;
    }

    server {
        listen 80;
        server_name www.assetcorrelation.com assetcorrelation.com;
        root /home/avitus/assetcorrelation.com/current/public;
        access_log /home/avitus/assetcorrelation.com/current/log/nginx_access.log main;
        error_log /home/avitus/assetcorrelation.com/current/log/nginx_error.log info;
        passenger_enabled on;
    }

    server {
         listen       80;
         server_name  localhost;

         location / {
             root   html;
             index  index.html index.htm;
         }
    }
}

I've been stumped on this problem for a few days now so any help will be much appreciated. To be honest, if anyone can explain what those two warnings/errors even mean it will be helpful.

回答1:

The first problem ("upstream response is buffered to a temporary file") was caused by some pages with a lot of content and was solved by increasing the buffer size as follows:

proxy_buffers 8 16k;
proxy_buffer_size 32k;
passenger_buffers 8 16k;
passenger_buffer_size 32k;

The second problem ('upstream prematurely closed connection while reading response header from upstream') was solved by increasing the memory on the server and switching Passenger to conservative spawn mode. (It appears to have been closely related to using an old Ruby gem which was trying to run background jobs.)