title: nginx error: (13: Permission denied) while connecting to upstream)
body: <p>I am running  rails app with puma, capistrano, and nginx on a google compute engine VM with ubuntu 14.04 LTS.</p>
  
  <p>I have the nginx running on the external IP. And when I visit it I get two nginx errors in the log:</p>
  
  <pre><code>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: "###.###.###.###"
  </code></pre>
  
  <p>Note: the last ###.###.###.### is the external IP of the google compute VM that the code is running on. I believe the 1st two IP's are my home IP.</p>
  
  <p>I have tried: <code>setsebool httpd_can_network_connect on</code> as suggested here:
  <a href="https://stackoverflow.com/questions/23948527/13-permission-denied-while-connecting-to-upstreamnginx">(13: Permission denied) while connecting to upstream:[nginx]</a>
  And it returned: <code>setsebool:  SELinux is disabled.</code> But the problem persists.</p>
  
  <p>I have looked at <a href="https://stackoverflow.com/questions/23948527/13-permission-denied-while-connecting-to-upstreamnginx">(13: Permission denied) while connecting to upstream:[nginx]</a> as well, but it seems to be particular to <code>uwsgi</code> </p>
  
  <p>Here is my nginx.conf</p>
  
  <pre><code>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;
  }
  </code></pre>
  
  <p>I run nginx with <code>sudo service nginx restart</code>
  Then I run puma with: <code>RACK_ENV=production bundle exec puma -p 3000</code> and it returns: </p>
  
  <pre><code>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
  </code></pre>
  
  <p><strong>EDIT 1</strong></p>
  
  <p>It was suggested that I run puma on unix not tcp 3000 so that it'd match nginx</p>
  
  <p>I have tried running puma on unix via the command:</p>
  
  <pre><code>RACK_ENV=production bundle exec puma -d -b unix:///tmp/my-web-app.sock --pidfile /tmp/puma.pid
  </code></pre>
  
  <p>which gave:</p>
  
  <pre><code>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...
  </code></pre>
  
  <p>It reads the above text, but it does not linger, the command prompt occurs again immediately despite the '...' at the end.</p>
  
  <p>This command seemingly does not work, so if anyone can suggest how to run puma on unix and not tcp 3000, then I could complete the suggestion. (Though I suspect there is a configuring nginx issue that may be occuring before anything that has to do with puma)</p>
  
  <p><strong>EDIT 2</strong> Attaching puma.rb</p>
  
  <pre><code>#!/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
  </code></pre>
  
  <p><strong>EDIT 3</strong></p>
  
  <p>I now tried just running the rails server on port 80 directly. I typed:
  <code>rvmsudo rails server -p 80</code> and it returned: </p>
  
  <pre><code>=&gt; Booting Puma
  =&gt; Rails 4.2.4 application starting in development on http://localhost:80
  =&gt; Run `rails server -h` for more startup options
  =&gt; 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 `&lt;top (required)&gt;'
          from bin/rails:4:in `require'
          from bin/rails:4:in `&lt;main&gt;'
  </code></pre>
  
  <p><strong>EDIT 4</strong></p>
  
  <p>If I run sudo service nginx stop then run <code>rvmsudo rails server -p 80</code> again it returns:</p>
  
  <pre><code>=&gt; Booting Puma
  =&gt; Rails 4.2.4 application starting in development on http://localhost:80
  =&gt; Run `rails server -h` for more startup options
  =&gt; Ctrl-C to shutdown server
  Puma 2.14.0 starting...
  * Min threads: 0, max threads: 16
  * Environment: development
  * Listening on tcp://localhost:80
  </code></pre>
  
  <p>That means that approach was incorrect since without nginx when I visit the external IP nothing its now returns <code>The server refused the connection.</code> as opposed to the original:</p>
  
  <pre><code>We're sorry, but something went wrong.
  
  If you are the application owner check the logs for more information.
  </code></pre>
  
  <p>If anyone knows how to prevent the original error, any suggestions would be much appreciated.</p>
  
  <p><strong>EDIT 5</strong> 
  The original question remains, but can anyone tell me if this is an https problem or an ssl problem?</p>
  
  <p><strong>EDIT 6</strong></p>
  
  <p>I have tried running puma directly on 80 and am getting a permission error on 80.</p>
  
  <p>I try: <code>RACK_ENV=production bundle exec puma -p 80</code> and get:</p>
  
  <pre><code>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 `&lt;top (required)&gt;'
          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 `&lt;main&gt;'
  </code></pre>
  
  <p>I believe that this is caused because port 80 has higher permissions than others. So, I ran <code>sudo RACK_ENV=production bundle exec puma -p 80</code> but that just returned:  <code>Your Ruby version is 1.9.3, but your Gemfile specified 2.1.7</code></p>