2016-02-03 128 views
4

我在ubuntu 14.04 LTS的谷歌計算引擎虛擬機上運行Puma,capistrano和nginx rails應用程序。nginx錯誤:(13:權限被拒絕),同時連接到上游)

我有運行在外部IP上的nginx。當我參觀我在日誌中獲得兩個nginx的錯誤:

2016/02/03 11:58:07 [info] 19754#0: *73 client closed connection while waiting for request, client: ###.##.##.###, server: 0.0.0.0:443 

2016/02/03 11:58:07 [crit] 19754#0: *74 connect() to unix:///home/my-user-name/apps/my-web-app/shared/tmp/sockets/my-web-app-puma.sock failed (13: Permission denied) while connecting to upstream, client: ###.##.##.###, server: , 
request: "GET/HTTP/1.1", upstream: "http://unix:///home/my-user-name/apps/my-web-app/shared/tmp/sockets/my-web-app-puma.sock:/", host: "###.###.###.###" 

注:。在過去### ### ### ###是谷歌的計算虛擬機的外部IP是代碼正在運行。我相信前兩個IP是我的家庭IP。

我曾嘗試:setsebool httpd_can_network_connect on如下建議: (13: Permission denied) while connecting to upstream:[nginx] 它返回:setsebool: SELinux is disabled.但問題仍然存在。

我在(13: Permission denied) while connecting to upstream:[nginx]看上去很好,但它似乎是特別uwsgi

這裏是我的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/my-web-app/my-web-app.com.chained.crt; 
    ssl_certificate_key /etc/ssl/my-web-app/my-web-app.key; 
    root /home/my-web-app/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的運行與sudo service nginx restart 後來我跑PUMA有:RACK_ENV=production bundle exec puma -p 3000並返回:

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

E DIT 1

有人建議我在UNIX上運行彪馬不是TCP 3000,這樣它會匹配nginx的

我已經通過命令試圖在UNIX上運行彪馬:

RACK_ENV=production bundle exec puma -d -b unix:///tmp/my-web-app.sock --pidfile /tmp/puma.pid 

這給了:

Puma starting in single mode... 
* Version 2.14.0 (ruby 2.1.7-p400), codename: Fuchsia Friday 
* Min threads: 0, max threads: 16 
* Environment: production 
* Daemonizing... 

它讀取上面的文字,但它不會逗留,在命令提示符下再次立即儘管「......」在最後出現。

該命令看起來不起作用,所以如果任何人都可以建議如何在unix上運行puma而不是tcp 3000,那麼我可以完成這個建議。 (雖然我懷疑有可能任​​何有與PUMA做之前存在的一個配置nginx的問題)

EDIT 2附加puma.rb

#!/usr/bin/env puma 
directory '/home/my-user-name/apps/my-web-app/current' 
rackup "/home/my-user-name/apps/my-web-app/current/config.ru" 
environment 'production' 
pidfile "/home/my-user-name/apps/my-web-app/shared/tmp/pids/puma.pid" 
state_path "/home/my-user-name/apps/my-web-app/shared/tmp/pids/puma.state" 
stdout_redirect '/home/my-user-name/apps/my-web-app/current/log/puma.error.log', '/home/my-user-name/apps/my-web-app/current/log/puma.access.log', true 
threads 2,8 
bind 'unix:///home/my-user-name/apps/my-web-app/shared/tmp/sockets/my-web-app-puma.sock' 
workers 1 
preload_app! 
on_restart do 
    puts 'Refreshing Gemfile' 
    ENV["BUNDLE_GEMFILE"] = "/home/my-user-name/apps/my-web-app/current/Gemfile" 
end 
on_worker_boot do 
    ActiveSupport.on_load(:active_record) do 
    ActiveRecord::Base.establish_connection 
    end 
end 

編輯3

我現在試着直接在端口80上運行rails服務器。我打字: 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>' 

EDIT 4

如果我執行sudo服務的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 

這意味着,方法是不正確,因爲沒有nginx當我訪問外部IP沒有它現在返回The server refused the connection.而不是原來的:

We're sorry, but something went wrong. 

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

如果有人知道如何防止原始錯誤,任何建議將不勝感激。

編輯5 原來的問題仍然存在,但任何人都可以告訴我,如果這是一個https問題或ssl問題?

編輯6

我曾嘗試直接運行在80彪馬上午80

得到一個權限錯誤我嘗試:RACK_ENV=production bundle exec puma -p 80並獲得:

Puma starting in single mode... 
* Version 2.14.0 (ruby 2.1.7-p400), codename: Fuchsia Friday 
* Min threads: 0, max threads: 16 
* Environment: production 
* Listening on tcp://0.0.0.0:80 
/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': Permission denied - bind(2) for "0.0.0.0" port 80 (Errno::EACCES) 
     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 /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/puma-2.14.0/lib/puma/binder.rb:98:in `block in parse' 
     from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/puma-2.14.0/lib/puma/binder.rb:84:in `each' 
     from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/puma-2.14.0/lib/puma/binder.rb:84:in `parse' 
     from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/puma-2.14.0/lib/puma/runner.rb:119:in `load_and_bind' 
     from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/puma-2.14.0/lib/puma/single.rb:79:in `run' 
     from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/puma-2.14.0/lib/puma/cli.rb:215:in `run' 
     from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/gems/puma-2.14.0/bin/puma:10:in `<top (required)>' 
     from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/bin/puma:23:in `load' 
     from /home/my-user-name/apps/my-web-app/shared/bundle/ruby/2.1.0/bin/puma:23:in `<main>' 

我相信,這是由於端口80比其他端口具有更高的權限所致。所以,我跑sudo RACK_ENV=production bundle exec puma -p 80,但剛剛回來:Your Ruby version is 1.9.3, but your Gemfile specified 2.1.7

+0

您的上游模塊顯示unix套接字,但您在TCP/IP套接字的端口3000上啓動puma。其中一個需要改變。 –

+0

謝謝@RichardSmith我曾嘗試編輯您的建議1 – Rorschach

+3

@Rorschach您有沒有機會解決這個問題?如果是這樣,你可以發佈答案,以便其他人可以從中受益。 – George

回答

4

我得到了同樣的錯誤與你,我有一個解決方案,但不知道它是否應該是正確的。 更改文件/etc/nginx/nginx.conf

user www-data;
的第一線,
user root;

使用,然後重啓nginx的:

service nginx restart OR systemctl restart nginx

效果很好!

+2

這有效地運行Nginx進程作爲根可能是危險的。 – Nicholas

相關問題