【问题标题】:nginx blocking puma from same portnginx 从同一端口阻止 puma
【发布时间】:2016-02-04 14:17:19
【问题描述】:

我有一个在 puma 和 nginx 上运行的 rails 应用程序。我用sudo service nginx restart 启动nginx,然后用rvmsudo rails server -p 80 启动puma/rails

这是我的错误:

`initialize': Address already in use - bind(2) for "localhost" port 80 (Errno::EADDRINUSE)

只有在 nginx 已经运行时才会出现此错误。

完整的错误是:

 $rvmsudo rails server -p 80

=> Booting Puma
=> Rails 4.2.4 application starting in development on http://localhost:80
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Puma 2.14.0 starting...
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://localhost:80
Exiting
/home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/puma-2.14.0/lib/puma/binder.rb:233:in `initialize': Address already in use - bind(2) for "localhost" port 80 (Errno::EADDRINUSE)
    from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/puma-2.14.0/lib/puma/binder.rb:233:in `new'
    from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/puma-2.14.0/lib/puma/binder.rb:233:in `add_tcp_listener'
    from (eval):2:in `add_tcp_listener'
    from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/puma-2.14.0/lib/rack/handler/puma.rb:33:in `run'
    from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/rack-1.6.4/lib/rack/server.rb:286:in `start'
    from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/commands/server.rb:80:in `start'
    from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:80:in `block in server'
    from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:75:in `tap'
    from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:75:in `server'
    from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

任何有关如何让 nginx 允许 puma 与之交谈的帮助将不胜感激。

编辑 1 nginx.conf

upstream puma {
  server unix:///home/my-user-name/apps/my-web-app/shared/tmp/sockets/my-web-app-puma.sock;
}

server {
  listen 80 default_server deferred;

  listen 443 ssl;

  # server_name example.com;
  ssl_certificate /etc/ssl/mysite/mysite.com.chained.crt;
  ssl_certificate_key /etc/ssl/mysite/mysite.key;

  root /home/my-user-name/apps/my-web-app/current/public;
  access_log /home/my-user-name/apps/my-web-app/current/log/nginx.access.log;
  error_log /home/my-user-name/apps/my-web-app/current/log/nginx.error.log info;

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

  try_files $uri/index.html $uri @puma;
  location @puma {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    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 10M;
  keepalive_timeout 10;
}

【问题讨论】:

  • 请显示你的 nginx 配置。但是如果 nginx 正在处理端口 80 上的传入请求,那么不,您不能在端口 80 上运行您的应用程序。

标签: ruby-on-rails nginx puma


【解决方案1】:

实际上,让我在不请求您的 nginx 配置的情况下回答这个问题。您的设置如下所示:

  • nginx 在端口 80 上侦听传入的 HTTP 请求
  • 根据配置,它将这些请求转发到正确的应用程序实例。
  • 这称为反向代理设置。

让我们看一个通用的 nginx 配置,它会告诉我们如何监听 80 端口并将所有请求转发到运行在 3000 端口上的应用程序:

# our http block to handle HTTP traffic with nginx
http {
  include mime.types;
  default_type  application/octet-stream;
  sendfile on;
  keepalive_timeout  65;

  # NGinx Server Configuration
  server {

    listen 80; #listen on port 80
    server_name my.domain.com;  # which domain we are listening for.

    # Add some basic auth
    # Remove this if not needed.
    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/.htpasswd;

    # Set up the location to map all requests to the Ruby App
    location / {
      proxy_pass http://127.0.0.1:3000;
      access_log off;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
    }
  }
}

现在你需要做两件事:

  • 重启你的 nginx
  • 在端口 3000 上启动您的 puma 应用程序

希望这能解释这两者如何协同工作,以及您需要在文件中更改哪些内容以帮助他们进行交流。

基本上:如果 nginx 声明端口 80,puma 无法声明它并需要在不同的端口上运行,让 nginx 将所有流量重新路由到该端口并返回。

【讨论】:

    【解决方案2】:

    在您的 puma.rb 文件中,您应该删除有关端口的行。由于您已经在配置中使用了 sock,因此也没有理由在 tcp 端口上运行 puma。

    当你启动 puma 时:

    puma -C config/puma.rb

    你应该只看到“Listening on unix:”,而不是 tcp:

    简而言之,您应该在 unix 套接字上运行 puma,而不是在 tcp 端口上。只需注释端口指令即可。

    一旦你这样做,它应该工作。顺便说一句,您应该允许使用 selinux,因为 selinux 将禁用套接字,您最终会看到消息“403 Forbidden”

    【讨论】:

      猜你喜欢
      • 2017-01-23
      • 2014-12-21
      • 1970-01-01
      • 1970-01-01
      • 2012-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多