简体   繁体   中英

502 bad gateway error (nginx + unicorn + digital ocean)

I struggled for few hours to fix this issue but still it doesn't work. The error I see in my browser is:

POST /users 502 (Bad Gateway)

I know that it's the problem of setting nginx and unicorn, but I can't solve it. By the way, I deployed my code using digital ocean. Here is my config file


Unicorn config ( nginx.conf ):

 user              nginx;
 worker_processes  1;
 error_log  /var/log/nginx/error.log;
 pid        /var/run/nginx.pid;
 events {
  worker_connections  1024;
 }
 http {
   server_names_hash_bucket_size 64;
   include       /etc/nginx/mime.types;
   default_type  application/octet-stream;

   log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

   access_log  /var/log/nginx/access.log  main;

   sendfile        on;
   #tcp_nopush     on;

   #keepalive_timeout  0;
   keepalive_timeout  65;

   #gzip  on;

   # Load config files from the /etc/nginx/conf.d directory
   # The default server is in conf.d/default.conf
   include /etc/nginx/conf.d/*.conf;
   fastcgi_buffers 8 16k;
   fastcgi_buffer_size 32k;
   fastcgi_connect_timeout 300;
   fastcgi_send_timeout 300;
   fastcgi_read_timeout 300;
}

Unicorn config file ( /var/nginx/unicorn.conf ):

upstream unicorn {
  server unix:/tmp/unicorn.sock fail_timeout=0;
}

server {

  listen              80;
  listen              443 ssl;
  ssl_certificate     /root/certs/server.crt;
  ssl_certificate_key /root/certs/server.key;

  client_max_body_size 4G;
  keepalive_timeout 15;
  root /var/www/quoine/current/public;
  try_files $uri @unicorn;

  location ~ ^/assets|app/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  location = /app/ {
    rewrite $uri $uri/index.html;
  }

  location = /app/index.html {
    add_header Pragma "no-cache";
    add_header Cache-Control "no-cache, no-store, max-age=0, must-revalidate";
    add_header Expires "Fri, 01 Jan 1990 00:00:00 GMT";
  }

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

  if (-f $document_root/system/maintenance.html) {
    return 503;
  }

  error_page 500 502 504 /500.html;
  location = /500.html {
    root /var/www/quoine/current/public;
  }

  error_page 503 @maintenance;
  location @maintenance {
    rewrite  ^(.*)$  /system/maintenance.html break;
  }
}

I'm using Rails 3. If any one got any idea about this problem, please tell me. It takes me 3 hours without any progress. Thanks

Can you provide the config you've got for unicorn too (the files you've provided are both for nginx). An example of what it should look like is in the first part of the "Configuring Servers" entry here: https://www.digitalocean.com/community/tutorials/how-to-deploy-rails-apps-using-unicorn-and-nginx-on-centos-6-5

I ran across this trying to figure out why I was getting 502 errors after using the 1-click install for Digital Ocean - and using a different version of Ruby.

I found my answer by looking at this guide: https://www.digitalocean.com/community/tutorials/how-to-use-the-1-click-ruby-on-rails-on-ubuntu-14-04-image

My issue was the following from the guide:

Once you have the location of Ruby that you are using by default, change /etc/default/unicorn pathnames to include /usr/local/rvm/rubies subfolder and /usr/local/rvm/gems subfolders for the newly installed version as well as location of unicorn

Hope this helps helps someone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM