调试502 Bad Gateway错误 - Ubuntu,Nginx,Unicorn

时间:2017-02-10 18:10:49

标签: ruby-on-rails ubuntu nginx server unicorn

我有一个EC2实例,尝试使用Nginx和Unicorn运行Rails应用程序。

大约60秒后我不断收到502 Bad Gateway错误:

这是我在nginx错误日志中得到的内容:

  

2017/02/10 18:03:38 [错误] 13208#13208:* 1过早上游   从上游读取响应头时关闭连接,客户端:   174.25.146.24,服务器:_,请求:" GET / welcome HTTP / 1.1",上游:" http://unix:/home/rails/myapp/shared/sockets/unicorn.sock:/welcome",   主持人:" mydomain.com"

unicorn.stderr.log

  

E,[2017-02-13T00:58:12.456504#13149]错误 - :worker = 0 PID:16535   超时(31s> 30s),杀死E,[2017-02-13T00:58:12.459388#13149]   错误 - :收获#   worker = 0 I,[2017-02-13T00:58:12.459499#13149]信息 - :worker = 0   产卵......我,[2017-02-13T00:58:12.461390#16810]信息 - :worker = 0   产生了pid = 16810 I,[2017-02-13T00:58:12.461829#16810]信息 - :   worker = 0 ready

nginx.conf

user rails;
worker_processes auto;
pid /run/nginx.pid;

events {
        worker_connections 1024;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

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

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

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

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        #  upstream unicorn {
        #    server unix:/home/rails/myapp/shared/sockets/unicorn.sock fail_timeout=0;
        #  }
        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

unicorn.rb

# set path to application
app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#{app_dir}"
working_directory app_dir

print shared_dir

# Set unicorn options
worker_processes 2
preload_app true
timeout 30
print '1'

# Set up socket location
listen "#{shared_dir}/shared/sockets/unicorn.sock", :backlog => 64
print '2'

# Logging
stderr_path "#{shared_dir}/shared/log/unicorn.stderr.log"
stdout_path "#{shared_dir}/shared/log/unicorn.stdout.log"

print '3'
# Set master PID location
pid "#{shared_dir}/shared/pids/unicorn.pid"
print '4'

位点可用/默认

upstream app {
    # Path to Unicorn SOCK file, as defined previously
    server unix:/home/rails/myapp/shared/sockets/unicorn.sock fail_timeout=0;
}

server {
    listen 80;
    server_name _;

    root /home/rails/myapp/public;

    try_files $uri/index.html $uri @app;

    location @app {
        proxy_pass http://app;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
    }

    error_page 500 502 503 504 /500.html;
    client_max_body_size 4G;
    keepalive_timeout 10;
}

套接字上的权限:

srwxrwxrwx 1 rails rails 0 Feb 10 18:00 unicorn.sock

关于我如何追踪这一点的任何想法?

1 个答案:

答案 0 :(得分:4)

您的Rails应用程序未能在超时设置配置的时间内返回对Unicorn HTTP服务器的响应(当前为30秒,由timeout 30中的unicorn.rb行配置

要么增加unicorn.rb中的timeout设置,要么(最好)调试Rails应用程序为什么要使用> 30秒响应请求。