调试 502 Bad Gateway 错误 - Ubuntu、Nginx、Unicorn
Posted
技术标签:
【中文标题】调试 502 Bad Gateway 错误 - Ubuntu、Nginx、Unicorn【英文标题】:Debugging 502 Bad Gateway error - Ubuntu, Nginx, Unicorn 【发布时间】:2017-06-29 04:37:29 【问题描述】:我有一个 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] 错误——:收获# 工人=0 我,[2017-02-13T00:58:12.459499 #13149] 信息——:工人=0 产卵...我,[2017-02-13T00:58:12.461390 #16810] 信息 -- : worker=0 产生 pid=16810 I,[2017-02-13T00:58:12.461829 #16810] 信息——: worker=0 准备好了
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/*;
独角兽.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
关于如何追踪这个问题的任何想法?
【问题讨论】:
独角兽日志里有什么吗? 我刚刚在帖子顶部添加了这个 你能提供'unicorn.stdout.log'的日志吗? 【参考方案1】:您的 Rails 应用程序未能在您的超时设置配置的时间内(当前为 30 秒,由您的 unicorn.rb
中的 timeout 30
行配置)向 Unicorn HTTP 服务器返回响应。
要么在unicorn.rb
中增加timeout
设置,要么(最好)调试为什么你的Rails 应用程序需要> 30 秒来响应请求。
【讨论】:
嗨@wjordan,我面临着类似的问题,我如何确定哪个请求花费了超过 30 秒。我看到了动作电缆和其他动作的混合 @opensource-developer correct way 提出后续问题是提出一个新问题,链接回原始问题以获取上下文。以上是关于调试 502 Bad Gateway 错误 - Ubuntu、Nginx、Unicorn的主要内容,如果未能解决你的问题,请参考以下文章
Nginx 用 502 Bad Gateway 覆盖一般的 symfony 错误