rails nginx unicorn听多个端口

时间:2014-09-24 06:40:38

标签: ruby-on-rails nginx unicorn

首先,抱歉我的英语(T ^ T)

我想要运行三个应用程序 (实际上,一个应用程序。唯一的区别是环境(开发,升级,生产))

所以我修改了一些conf代码 但只能在80端口工作!
如果我将工作端口(监听80)更改为81,则不能在端口81 T ^ T上工作。

我不知道为什么它只能在80端口上工作

这是我的my_app_nginx.conf,deploy.rb,unicorn.rb,unicorn_init.sh,' netstat -lnp'

/ etc / nginx / sites-enabled / my_app_nginx (包含在nginx.conf中)
三个上司。 监听端口80(生产),3000(开发),3001(分期)上的三个服务器

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

upstream unicorn_development {
  server unix:/tmp/unicorn.chimiseng_development.sock fail_timeout=0;
}
upstream unicorn_staging {
  server unix:/tmp/unicorn.chimiseng_staging.sock fail_timeout=0;
}
upstream unicorn_production {
  server unix:/tmp/unicorn.chimiseng_production.sock fail_timeout=0;
}

server {
  listen 80; 
  root /bps_data/apps/chimiseng_production/current/public;

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

  try_files $uri/index.html $uri @unicorn_production;
  location @unicorn_production {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn_production;
  }

  error_page 500 502 503 504 /500.html;
  error_log /bps_data/apps/chimiseng_production/shared/log/nginx_error.log warn;
  access_log /bps_data/apps/chimiseng_production/shared/log/nginx_access.log compression;
  client_max_body_size 4G; 
  keepalive_timeout 10; 
}

server {
  listen 3000;
  root /bps_data/apps/chimiseng_development/current/public;

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

  try_files $uri/index.html $uri @unicorn_development;
  location @unicorn_development {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn_development;
  }
  error_page 500 502 503 504 /500.html;
  error_log /bps_data/apps/chimiseng_development/shared/log/nginx_error.log warn;
  access_log /bps_data/apps/chimiseng_development/shared/log/nginx_access.log compression;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

server {
  listen 3001;
  root /bps_data/apps/chimiseng_staging/current/public;

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

  try_files $uri/index.html $uri @unicorn_staging;
  location @unicorn_staging {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn_staging;
  }

  error_page 500 502 503 504 /500.html;
  error_log /bps_data/apps/chimiseng_staging/shared/log/nginx_error.log warn;
  access_log /bps_data/apps/chimiseng_staging/shared/log/nginx_access.log compression;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

供参考.. http://my_domain.com:80记录在nginx_access_log中 但是http://my_domain.com:3000或:3001永远不会记录在nginx_access_log中。只留下0个字节。

unicorn.rb

environment = ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'production'
root = "/bps_data/apps/chimiseng_#{environment}/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.chimiseng_#{environment}.sock"
worker_processes 2
timeout 30

deploy.rb

namespace :deploy do
  %w[start stop restart].each do |command|
    desc "#{command} unicorn server"
    task command, roles: :app, except: {no_release: true} do
      run "/etc/init.d/unicorn_#{application} #{command} #{rails_app}"
    end 
  end 

  task :setup_config, roles: :app do
    sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/chimiseng"
    sudo "ln -nfs #{current_path}/config/unicorn_init.sh /etc/init.d/unicorn_#{application}"
  end 
...
...

unicorn_init.sh

TIMEOUT=${TIMEOUT-60}
APP_ROOT=/bps_data/apps/chimiseng_$2/current
PID=$APP_ROOT/tmp/pids/unicorn.pid
CMD="cd $APP_ROOT; bundle exec unicorn -D -c $APP_ROOT/config/unicorn.rb -E $2"
... start|stop|force-stop|restart... codes...

netstat -lnp

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
...
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      -               
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -               
tcp        0      0 0.0.0.0:3000            0.0.0.0:*               LISTEN      -               
tcp        0      0 0.0.0.0:3001            0.0.0.0:*               LISTEN      -
...
Active UNIX domain sockets (only servers)
...
unix  2      [ ACC ]     STREAM     LISTENING     9525914  8564/unicorn.rb -E  /tmp/unicorn.chimiseng_development.sock
unix  2      [ ACC ]     STREAM     LISTENING     9509620  13448/unicorn.rb -E /tmp/unicorn.chimiseng_staging.sock
unix  2      [ ACC ]     STREAM     LISTENING     9519355  3020/unicorn.rb -E  /tmp/unicorn.chimiseng_production.sock
...

我试图" nginx重启"和sh -c" /etc/init.d/unicorn_chimiseng_ENVIRONMENT重启环境"不工作
但是你可以看到netstat -lnp ..很好地监听端口并且袜子是活动的。
并且nginx没有默认的延期选项' listen'
为什么不能使用除80以外的端口

请帮帮我T ^ T

1 个答案:

答案 0 :(得分:0)

抱歉T ^ T
我解决了这个问题。

我正在使用云服务器 突然之间,想到云服务器网站上的Fort转发让我感到震惊。
在网站上允许3000,3001端口..它运行良好......!

每个人都点击不清楚的问题..抱歉阅读这个问题。 X(

度过美好的一天!