在 puma、rails、nginx 上设置 rails 应用程序一切都在运行,但 nginx 发送错误

Posted

技术标签:

【中文标题】在 puma、rails、nginx 上设置 rails 应用程序一切都在运行,但 nginx 发送错误【英文标题】:Setting up a rails app on puma, rails, nginx everything is running, but nginx sends error 【发布时间】:2016-05-11 04:38:10 【问题描述】:

我正在尝试运行一个 Rails 应用程序。我按照以下说明操作:http://luugiathuy.com/2014/11/setup-nginx-puma-on-ubuntu/ 设置服务器。

但是,当我走到最后,特别是:puma -e production -d -b unix:///tmp/app_name.sock --pidfile /tmp/puma.pid 运行它后,我得到了响应:

Puma starting in single mode...
* Version 2.16.0 (ruby 2.1.7-p400), codename: Midwinter Nights Trance
* Min threads: 0, max threads: 16
* Environment: production
* Daemonizing...

外部IP一直返回nginx错误:

We're sorry, but something went wrong.

If you are the application owner check the logs for more information.

所以,在其他地方,我尝试了: RACK_ENV=production bundle exec puma -p 3000

而且,这似乎做得更多,我得到了:

Puma starting in single mode...
* Version 2.15.3 (ruby 2.1.7-p400), codename: Autumn Arbor Airbrush
* Min threads: 0, max threads: 16
* Environment: production
Rails Error: Unable to access log file. Please ensure that /home/myusername/myappname/log/production.log exists and is writable (ie, make it writable for user and group: chmod 0664 /home/myusername/myappname/log/production.log). The log level has bee
n raised to WARN and the output directed to STDERR until the problem is fixed.
** [Bugsnag] Bugsnag exception handler 3.0.0 ready, api_key=#####################
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop

但是外部网页返回同样的nginx错误。

我应该如何进行?似乎事情正在运行,但 puma 和 nginx 只是不说话?

编辑 1

I did:  `sudo chmod -R 0777 /home/myunsername/appname/log/`

Then:

$RACK_ENV=production bundle exec puma -p 3000

而且,现在输出如下,外部 IP 上的 nginx 响应保持不变。

Puma starting in single mode...
* Version 2.15.3 (ruby 2.1.7-p400), codename: Autumn Arbor Airbrush
* Min threads: 0, max threads: 16
* Environment: production
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop

现在,两种方法:RACK_ENV=production bundle exec puma -p 3000puma -e production -d -b unix:///tmp/web-app.sock --pidfile /tmp/puma.pid 创建 puma 进程似乎不与 nginx 通信但不返回任何错误。

EDIT 2检查日志

我跑了:

cat log/production.log

它返回:

I, [2016-02-02T##:##:##.###### #28802] INFO -- : ** [Bugsnag] Bugsnag exception handler 3.0.0 ready, api_key=####################

EDIT 2.5 检查更多日志 我跑了:

tail -f /var/log/nginx/error.log

它返回:

2016/02/02 11:09:52 [emerg] 28220#0: unknown directive "tream" in /etc/nginx/sites-enabled/web-appname.com:1 2016/02/02 11:10:26 [emerg] 28273#0: unknown directive "tream" in /etc/nginx/sites-enabled/web-appname.com:1

这些错误是由于我的文件缺少前 3 个字符而导致的,该错误已得到修复。总之:我上面列出的日志中似乎没有任何错误给我任何提示。

编辑 3

我目前的猜测是我错误地设置了我的 /etc/nginx/sites-available/myapp.com 文件。这是我使用的所有命名。

目前命名为:web-app.com,url就是ip地址,比如说https://104.199.155.166/。我的应用程序位于名为 web-app

的文件夹中
upstream web-app 
  server unix:///tmp/web-app.sock;


server 
  listen 80;
  server_name web-app; # change to match your URL
  root /home/myusername/web-app/public; # change to match your rails app public folder

  location / 
    proxy_pass http://web-app; # match the name of upstream directive which is defined in line 1
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
  

  location ~* ^/assets/ 
    # Per RFC2616 - 1 year maximum expiry
    expires 1y;
    add_header Cache-Control public;

    # Some browsers still send conditional-GET requests if there's a
    # Last-Modified header or an ETag header even if they haven't
    # reached the expiry date sent in the Expires header.
    add_header Last-Modified "";
    add_header ETag "";
    break;
  

编辑 4

我对上述配置文件做了很多修改,但我所做的一切都没有改变。我目前拥有我认为正确的配置。我很茫然,如果有任何建议,我将不胜感激。

小问题:如果有其他用户在同一个虚拟机上,他们可能安装和配置不同的 nginx,这会导致问题吗?

【问题讨论】:

首先修复日志的权限错误。 您是否检查了production.log的访问权限为mentioend in error 您能check the NGiNX logs 并在问题中添加任何相关行吗?在这种情况下,错误可能发生在 NGiNX 中,甚至在您怀疑的请求被传递给 Puma 之前。 @max 感谢您提供有关在何处查找日志的信息,我已在编辑 2.5 中添加了该信息 【参考方案1】:

您必须手动创建 config/puma.rb。我推荐阅读 DigitalOcean 的文章: "How To Deploy a Rails App with Puma and Nginx on Ubuntu 14.04"

我将粘贴文章中的 puma.rb:

# config/puma.rb
# Change to match your CPU core count
workers 2

# Min and Max threads per worker
threads 1, 6

app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#app_dir/shared"

# Default to production
rails_env = ENV['RAILS_ENV'] || "production"
environment rails_env

# Set up socket location
# bind "unix://#shared_dir/sockets/puma.sock"
# in your case
bind "unix:///tmp/web-app.sock"

# Logging
stdout_redirect "#shared_dir/log/puma.stdout.log", "#shared_dir/log/puma.stderr.log", true

# Set master PID and state locations
pidfile "#shared_dir/pids/puma.pid"
state_path "#shared_dir/pids/puma.state"
activate_control_app

on_worker_boot do
  require "active_record"
  ActiveRecord::Base.connection.disconnect! rescue ActiveRecord::ConnectionNotEstablished
  ActiveRecord::Base.establish_connection(YAML.load_file("#app_dir/config/database.yml")[rails_env])
end

细节:设置绑定后[...]你可以执行

$ bundle exec puma RAILS_ENV=production

确保共享目录存在,其中包含文件夹 log 和 pids, 并且nginx 正在使用的用户有权读取它们。

其他一些提示:

如果您在共享(日志和 pid)和 git 中的空文件夹有问题,请将 .gitkeep(空文件)放入这两个文件夹中。 如果您没有域,您可以将 server_name(nginx 指令)设置为 _(是的,下划线)或 localhost。

另外不要忘记检查您的虚拟机是否启用了 http 和 https 流量。

【讨论】:

以上是关于在 puma、rails、nginx 上设置 rails 应用程序一切都在运行,但 nginx 发送错误的主要内容,如果未能解决你的问题,请参考以下文章

ruby 在Heroku上设置Puma和Rails

使用 Nginx、Puma 和 Redis 部署 Rails 5 Action Cable

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

Rails + (Phusion Passenger|Puma|Unicorn|Thin) + (Nginx|Apache) 如何协同工作?

text Dockerize Rails(Puma + Postgres + Nginx)

Websocket rails (Faye-websocket) 握手错误代码 200 - AWS ElasticBeanstalk、nginx、puma