两个 rails 个应用程序使用 puma 和 nginx

Two rails apps using puma and nginx

我在 rails 应用程序上开发了两个 ruby。我拥有域 potato.domain.com 和 orange.domain.com,它们都指向我的 linux 服务器的相同 IP 地址。

我想为每个域部署一个 rails 应用程序。 为此,我使用了 Puma 和 Nginx,并遵循 this tutorial 并成功部署了其中一个站点(我们称之为 potato.domain.com)。

现在,当我尝试部署另一个域时,我遵循与 potato.domain.com 完全相同的步骤(用 de orange 应用程序替换对马铃薯应用程序的引用)但是一旦我尝试部署完成访问 orange.domain.com 我总是在 potato.domain.com.

这是我用于这两个应用程序的 puma.rb 文件:

# Change to match your CPU core count
workers 1

# 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"
# 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

下面是 nginx/sites-available 下的文件(并且都自动映射到已启用的站点):

默认文件(potato.domain.com):

upstream app {
    # Path to Puma SOCK file, as defined previously
    server unix:/home/username/potato/site/shared/sockets/puma.sock fail_timeout=0;
}

server {
    listen 80;
    server_name potato.domain.com;

    root /home/username/potato/site/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;
}

和橙色文件(orange.domain.com):

upstream events {
    # Path to Puma SOCK file, as defined previously
    server unix:/home/username/orange/site/shared/sockets/puma.sock fail_timeout=0;
}

server {
    listen 80;
    server_name orange.domain.com;

    root /home/username/potato/site/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;
}

有没有人注意到任何文件有问题?或者知道任何好的教程吗?

抱歉这么久 post 谢谢!

更新:

我在 nginx/sites-enabled 中合并了两个文件,通过查看日志似乎路由正常。

现在的问题是没有为其中一个应用程序创建 puma.sock 文件,记录错误:

17:45:05 [crit] 2444#0: *16 connect() to unix:/home/username/orange/site/shared/sockets/orange-puma.sock failed (2: No such file or directory) while connecting to upstream, client: IP, server: orange.domain.com, request: "GET / HTTP/1.1", upstream: "http://unix:/home/username/orange/site/shared/sockets/orange-puma.sock:/", host: "orange.domain.com", referrer: "http://orange.domain.com/"

橙色的 puma.rb 现在看起来像这样:

# Change to match your CPU core count
workers 1

# 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 = "production"
environment rails_env

# Set up socket location
bind "unix://#{shared_dir}/sockets/events-puma.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/events-puma.pid"
state_path "#{shared_dir}/pids/events-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

我似乎在某处与 puma 配置错误,有什么帮助吗? 谢谢

我认为主要原因是您为两个虚拟服务器提供了相同的根指令。

将橙色更改为,我猜:

root /home/username/potato/site/public;

但这可能只是示例的错字。

其次,使用上游事件,然后调用 @app。你真的应该打电话给 @events .... 像这样:

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

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