title: 502 bad gateway with manual (recommended) install of huginn with nginx
body: <p>I am having to open yet another ticket for bad gateway. I tried to use the manual install steps on Ubuntu (on an AWS EC2 server): <a href="https://github.com/cantino/huginn/blob/master/doc/manual/installation.md" rel="nofollow">https://github.com/cantino/huginn/blob/master/doc/manual/installation.md</a></p>
  
  <p>I was receiving 502 bad gateway with the following in nginx log:
  *4 connect() to unix:/home/huginn/huginn/tmp/sockets/unicorn.socket failed (2: No such file or directory) while connecting to upstream</p>
  
  <p>I changed the Procfile and nginx config to listen to try and create a socket file in another location with the following result:
  connect() to unix:/var/sockets/unicorn.huginn.socket failed (2: No such file or directory) while connecting to upstream</p>
  
  <p>Then, I changed the Procfile and nginx config to listen to 127.0.0.1:3000 and get this:
  *1 connect() failed (111: Connection refused) while connecting to upstream</p>
  
  <p>I believe I have unicorn running, but for some reason nginx is not listening on the same socket:</p>
  
  <pre><code>ubuntu@ip-172-31-62-157:/home/huginn/huginn/config$ ps aux | grep unicorn
  huginn   21407 46.0  2.1  93944 21880 ?        Rl   11:54   0:02 /home/huginn/huginn/vendor/bundle/ruby/2.3.0/bin/unicorn -c config/unicorn.rb
  ubuntu   21417  0.0  0.0  10460   940 pts/0    S+   11:54   0:00 grep --color=auto unicorn
  </code></pre>
  
  <p>What else do I need to troubleshoot? I am at my wits' end about this - Any pointers are appreciated!</p>
  
  <p>My Unicorn.rb is below:</p>
  
  <pre><code>wd = File.expand_path(File.join(File.dirname(__FILE__), '..'))
  
  app_path = wd
  
  worker_processes 2
  preload_app true
  timeout 180
  # listen "#{wd}/tmp/sockets/unicorn.socket"
  listen "127.0.0.1:3000"
  
  working_directory app_path
  
  rails_env = ENV['RAILS_ENV'] || 'production'
  
  # Log everything to one file
  stderr_path "log/unicorn.log"
  stdout_path "log/unicorn.log"
  
  # Set master PID location
  pid "#{wd}/tmp/pids/unicorn.pid"
  
  before_fork do |server, worker|
    ActiveRecord::Base.connection.disconnect!
    old_pid = "#{server.config[:pid]}.oldbin"
    if File.exist?(old_pid) &amp;&amp; server.pid != old_pid
      begin
        Process.kill("QUIT", File.read(old_pid).to_i)
      rescue Errno::ENOENT, Errno::ESRCH
        # someone else did our job for us
      end
    end
  end
  
  after_fork do |server, worker|
    ActiveRecord::Base.establish_connection
  end
  </code></pre>
  
  <p>nginx config:</p>
  
  <pre><code>## Huginn
  ##
  ## Lines starting with two hashes (##) are comments with information.
  ## Lines starting with one hash (#) are configuration parameters that can be uncommented.
  ##
  ###################################
  ##         configuration         ##
  ###################################
  ##
  ## See installation.md#using-https for additional HTTPS configuration details.
  
  # upstream huginn {
  #  server unix:/home/huginn/huginn/tmp/sockets/unicorn.socket fail_timeout=0;
  # }
  
  upstream huginn {
    server 127.0.0.1:3000 fail_timeout=0;
  }
  ## Normal HTTP host
  server {
    listen 0.0.0.0:80 default_server;
    listen [::]:80 ipv6only=on default_server;
    server_name ec2-54-209-37-232.compute-1.amazonaws.com; ## Replace this with something like huginn.example.com
    server_tokens off; ## Don't show the nginx version number, a security best practice
    root /home/huginn/huginn/public;
  
    ## Increase this if you want to upload large attachments
    client_max_body_size 20m;
  
    ## Individual nginx logs for this Huginn vhost
    access_log  /var/log/nginx/huginn_access.log;
    error_log   /var/log/nginx/huginn_error.log;
  
    location / {
      ## Serve static files from defined root folder.
      ## @huginn is a named location for the upstream fallback, see below.
      try_files $uri $uri/index.html $uri.html @huginn;
    }
  
    ## If a file, which is not found in the root folder is requested,
    ## then the proxy passes the request to the upsteam (huginn unicorn).
    location @huginn {
      ## If you use HTTPS make sure you disable gzip compression
      ## to be safe against BREACH attack.
      # gzip off;
  
      proxy_read_timeout      300;
      proxy_connect_timeout   300;
      proxy_redirect          off;
  
      proxy_set_header    Host                $http_host;
      proxy_set_header    X-Real-IP           $remote_addr;
      proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
      proxy_set_header    X-Forwarded-Proto   $scheme;
      proxy_set_header    X-Frame-Options     SAMEORIGIN;
  
      proxy_pass http://huginn;
    }
  
    ## Enable gzip compression as per rails guide:
    ## http://guides.rubyonrails.org/asset_pipeline.html#gzip-compression
    ## WARNING: If you are using relative urls remove the block below
    ## See config/application.rb under "Relative url support" for the list of
    ## other files that need to be changed for relative url support
    location ~ ^/(assets)/ {
      root /home/huginn/huginn/public;
      gzip_static on; # to serve pre-gzipped version
      expires max;
      add_header Cache-Control public;
    }
  
    error_page 502 /502.html;
  </code></pre>
  
  <p>Procfile:</p>
  
  <pre><code>###############################
  #         DEVELOPMENT         #
  ###############################
  
  # Procfile for development using the new threaded worker (scheduler, twitter stream and delayed job)
  # web: bundle exec rails server -p ${PORT-3000} -b ${IP-0.0.0.0}
  # jobs: bundle exec rails runner bin/threaded.rb
  
  # Old version with separate processes (use this if you have issues with the threaded version)
  # web: bundle exec rails server
  # schedule: bundle exec rails runner bin/schedule.rb
  # twitter: bundle exec rails runner bin/twitter_stream.rb
  # dj: bundle exec script/delayed_job run
  
  ###############################
  #         PRODUCTION          #
  ###############################
  
  # You need to copy or link config/unicorn.rb.example to config/unicorn.rb for both production versions.
  # Have a look at the deployment guides, if you want to set up huginn on your server:
  # https://github.com/cantino/huginn/doc
  
  # Using the threaded worker (consumes less RAM but can run slower)
  web: bundle exec unicorn -c config/unicorn.rb
  jobs: bundle exec rails runner bin/threaded.rb
  
  # Old version with separate processes (use this if you have issues with the threaded version)
  # web: bundle exec unicorn -c config/unicorn.rb
  # schedule: bundle exec rails runner bin/schedule.rb
  # twitter: bundle exec rails runner bin/twitter_stream.rb
  # dj: bundle exec script/delayed_job run
  
  ###############################
  # Multiple DelayedJob workers #
  ###############################
  # Per default Huginn can just run one agent at a time. Using a lot of agents or calling slow
  # external services frequently might require more DelayedJob workers (an indicator for this is
  # a backlog in your 'Job Management' page).
  # Every uncommented line starts an additional DelayedJob worker. This works for development, production
  # and for the threaded and separate worker processes. Keep in mind one worker needs about 300MB of RAM.
  #
  #dj2: bundle exec script/delayed_job -i 2 run
  #dj3: bundle exec script/delayed_job -i 3 run
  #dj4: bundle exec script/delayed_job -i 4 run
  #dj5: bundle exec script/delayed_job -i 5 run
  #dj6: bundle exec script/delayed_job -i 6 run
  #dj7: bundle exec script/delayed_job -i 7 run
  #dj8: bundle exec script/delayed_job -i 8 run
  #dj9: bundle exec script/delayed_job -i 9 run
  #dj10: bundle exec script/delayed_job -i 10 run
  </code></pre>