2016-02-04 35 views
0

我有一個在美洲獅和nginx上運行的rails應用程序。我啓動與sudo service nginx restart然後PUMA/nginx的滑軌與rvmsudo rails server -p 80nginx阻止來自同一端口的美洲獅

這是我的錯誤:

`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的,讓彪馬去跟那將是非常讚賞。

編輯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; 
} 
+0

請顯示您的nginx配置。 但是,如果nginx正在處理端口80上的傳入請求,那麼不,您不能在端口80上運行您的應用程序。 –

回答

4

其實,讓我回答這個問題,而無需請求您的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應用

希望這解釋了2如何一起工作,以及您需要在文件中更改以幫助他們進行通信。

基本上:如果nginx聲稱端口80,puma不能聲稱它並需要在不同的端口上運行,讓nginx重新路由所有流量到該端口並返回。

0

在您的puma.rb文件中,您應該刪除有關端口的行。由於您已經在配置中使用sock,因此沒有理由在tcp端口上運行puma。

當你開始與PUMA:

PUMA -C配置/ puma.rb

你應該只看到 「聽在UNIX上:」,而不是TCP:

enter image description here

簡而言之,您應該在unix套接字上運行puma,而不是在tcp端口上運行。只需評論港口指令。

一旦你這樣做,它應該工作。順便說一句,你應該是寬容的selinux,因爲selinux將禁用該插座,你最終會看到消息「403 Forbidden」