带有nginx和unicorn初学者问题的Rails

时间:2013-12-27 22:15:30

标签: ruby-on-rails nginx unicorn linode

嘿家伙们想知道是否有人能指出我的整体服务器组织,nginx配置或独角兽配置的明显问题?

基本上问题是我的请求来自Nginx,并提供rails static 404页面,而不是welcome #index页面。我觉得主要问题是我的nginx配置不知何故没有正确指向我的独角兽服务器。

我在nginx错误日志中遇到的唯一警告是:

duplicate MIME type "text/html" in /etc/nginx/nginx.conf:68

服务器组织:

/home
└── developer
    └── apps
        ├── absoluteDeploy
        │   └── current
        │       ├── public
        │       │   ├── app
        │       │   │   ├── assets
        │       │   │   │   ├── images
        │       │   │   │   ├── javascripts
        │       │   │   │   │   └── application.js
        │       │   │   │   └── stylesheets
        │       │   │   │       └── application.css
        │       │   │   ├── controllers
        │       │   │   │   ├── application_controller.rb
        │       │   │   │   └── concerns
        │       │   │   ├── helpers
        │       │   │   │   └── application_helper.rb
        │       │   │   ├── mailers
        │       │   │   ├── models
        │       │   │   │   └── concerns
        │       │   │   └── views
        │       │   │       └── layouts
        │       │   │           └── application.html.erb
        │       │   ├── bin
        │       │   │   ├── bundle
        │       │   │   ├── rails
        │       │   │   └── rake
        │       │   ├── config
        │       │   │   ├── application.rb
        │       │   │   ├── boot.rb
        │       │   │   ├── database.yml
        │       │   │   ├── environment.rb
        │       │   │   ├── environments
        │       │   │   │   ├── development.rb
        │       │   │   │   ├── production.rb
        │       │   │   │   └── test.rb
        │       │   │   ├── initializers
        │       │   │   │   ├── backtrace_silencers.rb
        │       │   │   │   ├── filter_parameter_logging.rb
        │       │   │   │   ├── inflections.rb
        │       │   │   │   ├── mime_types.rb
        │       │   │   │   ├── secret_token.rb
        │       │   │   │   ├── session_store.rb
        │       │   │   │   └── wrap_parameters.rb
        │       │   │   ├── locales
        │       │   │   │   └── en.yml
        │       │   │   ├── routes.rb
        │       │   │   ├── unicorn_init.sh
        │       │   │   └── unicorn.rb
        │       │   ├── config.ru
        │       │   ├── db
        │       │   │   └── seeds.rb
        │       │   ├── Gemfile
        │       │   ├── Gemfile.lock
        │       │   ├── lib
        │       │   │   ├── assets
        │       │   │   └── tasks
        │       │   ├── log
        │       │   │   ├── development.log
        │       │   │   └── production.log
        │       │   ├── public
        │       │   │   ├── 404.html
        │       │   │   ├── 422.html
        │       │   │   ├── 500.html
        │       │   │   ├── assets
        │       │   │   │   ├── application-6b9f68fc78433a350a81e54881745caa.js
        │       │   │   │   ├── application-6b9f68fc78433a350a81e54881745caa.js.gz
        │       │   │   │   ├── application-96a552b03ca0e7ebcbfc44b89ca097a6.css
        │       │   │   │   ├── application-96a552b03ca0e7ebcbfc44b89ca097a6.css.gz
        │       │   │   │   └── manifest-526e08fd0db690db92e19dab33b279e4.json
        │       │   │   ├── favicon.ico
        │       │   │   └── robots.txt
        │       │   ├── Rakefile
        │       │   ├── README.rdoc
        │       │   ├── test
        │       │   │   ├── controllers
        │       │   │   ├── fixtures
        │       │   │   ├── helpers
        │       │   │   ├── integration
        │       │   │   ├── mailers
        │       │   │   ├── models
        │       │   │   └── test_helper.rb
        │       │   ├── tmp
        │       │   │   ├── cache
        │       │   │   │   └── assets
        │       │   │   │       └── production
        │       │   │   │           └── sprockets
        │       │   │   │               ├── 0c6610ca6b397050f605de7c62a8d7b6
        │       │   │   │               ├── 0d8862530e486ce9da465a3d1b32d697
        │       │   │   │               ├── 13fe41fee1fe35b49d145bcc06610705
        │       │   │   │               ├── 2f5173deea6c795b8fdde723bb4b63af
        │       │   │   │               ├── 357970feca3ac29060c1e3861e2c0953
        │       │   │   │               ├── 837bef08e6a3cbc48a17a7310072e4c1
        │       │   │   │               ├── 91538b88c626014969abafb623c72286
        │       │   │   │               ├── a51ba41ff6777866ce33812fd794ea1c
        │       │   │   │               ├── cffd775d018f68ce5dba1ee0d951a994
        │       │   │   │               ├── d771ace226fc8215a3572e0aa35bb0d6
        │       │   │   │               ├── f25df63a5627bfbd306f407eeacec7d4
        │       │   │   │               └── f7cbd26ba1d28d48de824f0e94586655
        │       │   │   ├── pids
        │       │   │   ├── sessions
        │       │   │   └── sockets
        │       │   └── vendor
        │       │       └── assets
        │       │           ├── javascripts
        │       │           └── stylesheets
        │       └── tmp
        │           ├── cache
        │           ├── pids
        │           ├── sessions
        │           └── sockets
        └── shared
            ├── log
            │   ├── unicorn.stderr.log
            │   └── unicorn.stdout.log
            └── pids
                └── unicorn.pid

的/ etc / nginx的

nginx
├── conf.d
├── fastcgi_params
├── koi-utf
├── koi-win
├── mime.types
├── naxsi_core.rules
├── naxsi.rules
├── naxsi-ui.conf.1.4.1
├── nginx.conf
├── proxy_params
├── scgi_params
├── sites-available
├── sites-enabled
│   └── default -> /etc/nginx/sites-available/default
├── uwsgi_params
└── win-utf

nginx config

worker_processes 1;

user nobody nogroup;

pid /var/run/nginx.pid;
error_log /var/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 /var/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

  gzip on;
  gzip_http_version 1.0;
  gzip_proxied any;
  gzip_min_length 500;
  gzip_disable "MSIE [1-6]\.";
  gzip_types text/plain text/html text/xml text/css
             text/comma-separated-values
             text/javascript application/x-javascript
             application/atom+xml;

  # this can be any application server, not just Unicorn/Rainbows!
  upstream unicorn {
    # for UNIX domain socket setups:
    server unix:/tmp/unicorn.absoluteDeploy.sock fail_timeout=0;
  }

  server {
    # enable one of the following if you're on Linux or FreeBSD
      listen 80 default deferred; # for Linux
    # listen 80 default accept_filter=httpready; # for FreeBSD

    client_max_body_size 4G;
    server_name absolutedeploy www.absolutedeploy.com;

    # ~2 seconds is often enough for most folks to parse HTML/CSS and
    # retrieve needed images/icons/frames, connections are cheap in
    # nginx so increasing this is generally safe...
    keepalive_timeout 5;

    # path for static files
    root /home/developer/apps/absoluteDeploy/current/public;

    try_files $uri/index.html $uri.html $uri @app;

    location / {
      # an HTTP header important enough to have its own Wikipedia entry:
      #   http://en.wikipedia.org/wiki/X-Forwarded-For
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

      # enable this if you forward HTTPS traffic to unicorn,
      # this helps Rack set the proper URL scheme for doing redirects:
      # proxy_set_header X-Forwarded-Proto $scheme;

      # pass the Host: header from the client right along so redirects
      # can be set properly within the Rack application
      proxy_set_header Host $http_host;

      # we don't want nginx trying to do something clever with
      # redirects, we set the Host: header above already.
      proxy_redirect off;

      proxy_pass http://unicorn;
    }

    # Rails error pages
    error_page 500 502 503 504 /500.html;
    location = /500.html {
      root /home/developer/apps/absoluteDeploy/current/public;
    }
  }
}

unicorn.rb

# Use at least one worker per core if you're on a dedicated server,
# more will usually help for _short_ waits on databases/caches.
worker_processes 4

working_directory "/home/developer/apps/absoluteDeploy/current/public" # available in 0.94.0+

# listen on both a Unix domain socket and a TCP port,
# we use a shorter backlog for quicker failover when busy
listen "/tmp/unicorn.absoluteDeploy.sock", :backlog => 64
listen 8080, :tcp_nopush => true

# nuke workers after 30 seconds instead of 60 seconds (the default)
timeout 30

# feel free to point this anywhere accessible on the filesystem
pid "/home/developer/apps/shared/pids/unicorn.pid"

stderr_path "/home/developer/apps/shared/log/unicorn.stderr.log"
stdout_path "/home/developer/apps/shared/log/unicorn.stdout.log"

# combine Ruby 2.0.0dev or REE with "preload_app true" for memory savings
# http://rubyenterpriseedition.com/faq.html#adapt_apps_for_cow
preload_app true
GC.respond_to?(:copy_on_write_friendly=) and
  GC.copy_on_write_friendly = true

check_client_connection false

before_fork do |server, worker|
  # the following is highly recommended for Rails + "preload_app true"
  # as there's no need for the master process to hold a connection
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!

end

after_fork do |server, worker|
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection

end

2 个答案:

答案 0 :(得分:1)

这里的问题是你的应用程序在服务器上的结构,你应该从当前删除公共,结构应该像这样:

└── developer
└── apps
    ├── absoluteDeploy
    │   └── current
    │       ├── app
    │       │   ├── assets
    │       │   │   ├── images
    │       │   │   ├── javascripts
    │       │   │   │   │   └── application.js
    │       │   │   └── stylesheets
    │       │   │       └── application.css
    │       │   ├── controllers
    │       │   │   ├── application_controller.rb
    │       │   │   └── concerns
    │       │   ├── helpers
    │       │   │   └── application_helper.rb
    │       │   ├── mailers
    │       │   ├── models
    │       │   │   └── concerns
    │       │   └── views
    │       │       └── layouts
    │       │           └── application.html.erb
    │       ├── bin
    │       │   ├── bundle
    │       │   ├── rails
    │       │   └── rake
    │       ├── config
    │       │   ├── application.rb
    │       │   ├── boot.rb
    │       │   ├── database.yml
    │       │   ├── environment.rb
    │       │   ├── environments
    │       │   │   ├── development.rb
    │       │   │   ├── production.rb
    │       │   │   └── test.rb
    │       │   ├── initializers
    │       |   │   ├── backtrace_silencers.rb
    │       │   │   ├── filter_parameter_logging.rb
    │       │   │   ├── inflections.rb
    │       │   │   ├── mime_types.rb
    │       │   │   ├── secret_token.rb
    │       │   │   ├── session_store.rb
    │       │   │   └── wrap_parameters.rb
    │       │   ├── locales
    │       │   │   └── en.yml
    │       │   ├── routes.rb
    │       │   ├── unicorn_init.sh
    │       │   └── unicorn.rb
    │       ├── config.ru
    │       ├── db
    │       │   └── seeds.rb
    │       ├── Gemfile
    │       ├── Gemfile.lock
    │       ├── lib
    │       │   ├── assets
    │       │   └── tasks
    │       ├── log
    │       |   ├── development.log
    │       │   └── production.log
    │       ├── public
    │       │   ├── 404.html
    │       │   ├── 422.html
    │       │   ├── 500.html
    │       │   ├── assets
    │       │   │   ├── application-6b9f68fc78433a350a81e54881745caa.js
    │       │   │   ├── application-6b9f68fc78433a350a81e54881745caa.js.gz
    │       │   │   ├── application-96a552b03ca0e7ebcbfc44b89ca097a6.css
    │       │   │   ├── application-96a552b03ca0e7ebcbfc44b89ca097a6.css.gz
    │       │   │   └── manifest-526e08fd0db690db92e19dab33b279e4.json
    │       │   ├── favicon.ico
    │       │   └── robots.txt
    │       ├── Rakefile
    │       ├── README.rdoc
    │       ├── test
    │       |   ├── controllers
    │       |   ├── fixtures
    │       │   ├── helpers
    │       │   ├── integration
    │       │   ├── mailers
    │       │   ├── models
    │       │   └── test_helper.rb
    │       ├── tmp
    │       │   ├── cache
    │       │   │   └── assets
    │       │   │       └── production
    │       │   │           └── sprockets
    │       │   │               ├── 0c6610ca6b397050f605de7c62a8d7b6
    │       │   │               ├── 0d8862530e486ce9da465a3d1b32d697
    │       │   │               ├── 13fe41fee1fe35b49d145bcc06610705
    │       │   │               ├── 2f5173deea6c795b8fdde723bb4b63af
    │       │   │               ├── 357970feca3ac29060c1e3861e2c0953
    │       │   │               ├── 837bef08e6a3cbc48a17a7310072e4c1
    │       │   │               ├── 91538b88c626014969abafb623c72286
    │       │   │               ├── a51ba41ff6777866ce33812fd794ea1c
    │       │   │               ├── cffd775d018f68ce5dba1ee0d951a994
    │       │   │               ├── d771ace226fc8215a3572e0aa35bb0d6
    │       │   │               ├── f25df63a5627bfbd306f407eeacec7d4
    │       │   │               └── f7cbd26ba1d28d48de824f0e94586655
    │       │   ├── pids
    │       │   ├── sessions
    │       │   └── sockets
    │       └── vendor
    │           └── assets
    │                   ├── javascripts
    │       │           └── stylesheets
    │       └── tmp
    │           ├── cache
    │           ├── pids
    │           ├── sessions
    │           └── sockets
    └── shared
        ├── log
        │   ├── unicorn.stderr.log
        │   └── unicorn.stdout.log
        └── pids
            └── unicorn.pid

答案 1 :(得分:0)

我不是专家。但你的nginx配置似乎是错误的。我正在粘贴我用于我的nginx的配置,希望能解决你的问题。

upstream unicorn_server{
    server unix:/var/webapps/rails-server/current/tmp/sockets/unicorn.sock fail_timeout=0;
}

server{
    listen 3000;
    root /var/webapps/rails-server/current/public;

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

    try_files $uri/index.html $uri @unicorn_server;

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

    error_page 500 502 503 504 /500.html;
}

基本上我觉得你的try_files指令存在问题。 http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files它按指定的顺序检查文件是否存在。你在最后提到的@app与你的上游配置不匹配。这可能是原因。