Puma 和 Nginx 502 Bad Gateway 错误(Ubuntu Server 14.04)

Posted

技术标签:

【中文标题】Puma 和 Nginx 502 Bad Gateway 错误(Ubuntu Server 14.04)【英文标题】:Puma and Nginx 502 Bad Gateway error (Ubuntu Server 14.04) 【发布时间】:2016-09-14 20:55:08 【问题描述】:

我需要部署我的 rails 应用程序,所以我已经按照这里的所有步骤进行操作,https://www.digitalocean.com/community/tutorials/how-to-deploy-a-rails-app-with-puma-and-nginx-on-ubuntu-14-04

但是教程结束,我得到了这个错误 --> "502 Bad Gateway"

编辑 现在的错误消息 --> “我们很抱歉,但出了点问题。” 但是 Nginx 错误输出是一样的,我检查 puma 错误消息,但它们只是在启动和正常停止时记录。

app_directory/log 下的 Rails 日志不会产生任何输出。 puma-manager --> 我检查它工作正常 路径--->我检查了三遍

Nginx error.log 输出信息:

2016/05/18 14:22:21 [crit] 1099#0: *7 connect() to unix:/home/deploy   /hotel-automata/shared/sockets/puma.sock failed (2: No such file or directory) while connecting to upstream, client: 192.168.2.105, server: localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "http://unix:/home/deploy/hotel-automata/shared/sockets/puma.sock:/500.html", host: "192.168.2.170"

操作系统 -> Vmware Player,桥接网络 Ubuntu Server 14.0.4 红宝石版本:2.3.1 Rails 版本:4.2.5.2

这是我的 /etc/nginx/sites-available/default 的 nginx 配置内容

upstream app 
# Path to Puma SOCK file, as defined previously
server unix:/home/deploy/hotel-automata/shared/sockets/puma.sock fail_timeout=0;


server 
listen 80;
server_name localhost;

root /home/deploy/hotel-automata/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;

【问题讨论】:

我对彪马一无所知。但是您是否尝试过更改puma.sock 文件的权限?当我将uwsginginx 一起使用时,我曾经遇到过这种错误 发布你的 nginx 配置 @dan-klasson 我已经添加了 nginx 配置 @manojprashantk 目录中没有 puma.sock 文件。 我可能错了,但你能不能把unix:/home/deploy/hotel-automata/shared/sockets/puma.sock改成unix:///home/deploy/hotel-automata/shared/sockets/puma.sock试试 【参考方案1】:

编辑:

    让用户知道套接字存在。否则它在这一点上失败了:

config/puma.rb 中,您需要有指向您的套接字的行:

bind "unix://<path or variable for the path where the socket will be>/sockets/puma.sock"

变量示例:

application_path = '/home/deploy/hotel-automata/shared'

bind "unix://#application_path/sockets/puma.socket"
    检查套接字的权限

您需要确保 Nginx 能够访问您的套接字(具有所需的权限,即 RW)

检查整个路径的权限试试这个:

namei -m /home/deploy/hotel-automata/shared/sockets/puma.sock

或者试试这个:

sudo -u <user> test <-r / -w > <path> && echo True

sudo -u nginx test -w /home/deploy/hotel-automata/shared/sockets/puma.sock && echo True

Nginx 需要对该套接字进行 RW 访问。

如果它不返回 true,则表示用户没有该权限,即 -w -> write

【讨论】:

我的套接字路径没有间隙,你的意思是不同的吗? 在您提供新数据时必须编辑我的答案,检查权限并告诉我。 目录下没有puma.sock文件。它应该自动生成吗? @MichaIT 当 puma 启动它应该创建一个套接字。这是你配置它的地方:vi config/puma.rb# Set up socket location bind "unix://#shared_dir/sockets/puma.sock" 只要确保在config/puma.rb 中你有bind "unix://&lt;path to your socket&gt;/sockets/puma.sock" 然后重启你的puma。很难让某人通过评论框。我会再次编辑答案【参考方案2】:

您的puma.rb 文件应如下所示。

# /config/puma.rb

  app = "manabalss" # App-specific
  root = "/home/deployer/apps/#app"

  workers  5
  threads  1, 1 # relying on many workers for thread-unsafe apps

  rackup      DefaultRackup
  port        11592
  environment ENV['RACK_ENV'] || 'production'
  daemonize   true

  pidfile "#root/puma/puma.pid"
  stdout_redirect "#root/puma/puma.log", "#root/puma/puma_error.log"
  bind "unix:/tmp/puma.socket

而你的 nginx.conf 应该是这样的。

# config/deploy/nginx.conf

upstream puma 
  server unix:/tmp/puma.socket fail_timeout=1;


# This block redirects http requests to https version 
server 
  listen 37.139.0.211:80 default deferred;
  server_name www.manabalss.lv, manabalss.lv;
  return 307  https://manabalss.lv$request_uri;


server 
  listen                37.139.0.211:443 ssl;
  server_name           manabalss.lv;
  ssl_certificate       /etc/ssl/server.crt;
  ssl_certificate_key   /etc/ssl/server.key;
  ssl_protocols         SSLv3 TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers           RC4:HIGH:!aNULL:!MD5;
  ssl_session_cache     shared:SSL:15m;
  ssl_session_timeout   15m;

  root /home/deployer/apps/manabalss/current/public;

  location ^~ /assets/ 
    gzip_static on;
    gzip_vary on;
    expires max;
    add_header Cache-Control public;
  

  try_files $uri/index.html $uri @puma;
  location @puma 
    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 https;
    proxy_set_header  Host $http_host;
    proxy_redirect    off;
    proxy_pass        http://puma;
  

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

如果这没有帮助,你想看看这个。

Rails + Puma + Nginx Every Bad Gateway 502

【讨论】:

谢谢subhash,我现在开始尝试

以上是关于Puma 和 Nginx 502 Bad Gateway 错误(Ubuntu Server 14.04)的主要内容,如果未能解决你的问题,请参考以下文章

运行 Puma 和 Nginx 的带有 AWS Elastic Beanstalk 的 Rails 应用程序 502

使用 -d(守护进程)运行 Rails(Puma)时的 Nginx 502

Nginx 渲染 502 错误页面

使用 flask_socketio + flask + gunicorn + nginx 获得 502 bad gateway 和 400 bad request

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

尝试在 Docker 上使用 NGINX + Gunicorn 时 NGINX 给出 502 Bad Gateway