Tarun Garg
Tarun Garg

Reputation: 717

unicorn, nginx connection refused in rails 4

While using the nginx and resolving all the errors, i am getting the error. But the application is running in development mode on production server.

Nginx configuration

pid /tmp/nginx.pid;
error_log /home/vagrant/app/sample_app/shared/log/nginx.error.log;

events {
  worker_connections 1024; # increase if you have lots of clients
  accept_mutex off; # "on" if nginx worker_processes > 1
}

http {
  include mime.types;
  default_type application/octet-stream;
  access_log  /home/vagrant/app/sample_app/shared/log/nginx.access.log combined;
  sendfile on;
  tcp_nopush on; # off may be better for *some* Comet/long-poll stuff
  tcp_nodelay off; # on may be better for some Comet/long-poll stuff

  # types_hash_max_size 2048;

  upstream app_server {
    server 192.168.33.10;
  }

  server {
    client_max_body_size 4G;
    server_name localhost;
    keepalive_timeout 600s;
    root /home/vagrant/app/sample_app/current;
    try_files $uri/index.html $uri.html $uri @app;

    location @app {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://app_server;
    }

    # Rails error pages
    error_page 500 502 503 504 /500.html;
    location = /500.html {
      root /home/vagrant/app/sample_app/current;
    }
  }
}

Unicorn Configuration

listen "/tmp/unicorn.sample_app.sock"
worker_processes 4
preload_app true
user 'vagrant'
root = "/home/vagrant/app/sample_app/current"
working_directory root
pid "#{root}/tmp/pids/unicorn.pid"
stderr_path "#{root}/log/unicorn.log"
stdout_path "#{root}/log/unicorn.log"

# listen "/tmp/unicorn.sample_app.sock"
worker_processes 2
timeout 30

# Force the bundler gemfile environment variable to
# reference the capistrano "current" symlink
before_exec do |_|
  ENV["BUNDLE_GEMFILE"] = File.join(root, 'Gemfile')
end

Getting error:-

connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.33.1, server: 192.168.33.10, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "192.168.33.10"

Upvotes: 1

Views: 3383

Answers (1)

Shivam Bajpai
Shivam Bajpai

Reputation: 1260

It seems like that you are using the wrong configuration for listening port in unicorn.rb and nginx.conf file. The upstream app_server is the connection socket to unicorn server.

In unicorn conf file use:

listen "127.0.0.1:8080"

In nginx conf file use:

upstream app_server {
    server 127.0.0.1:8080;
}

Upvotes: 1

Related Questions