简体   繁体   中英

Rails with nginx and unicorn beginner issues

Hey guys was wondering if anyone could point out the glaring issue with my overall server organization, nginx config, or unicorn config?

Basically the issue is my requests are going from Nginx, and serving the rails static 404 page, as opposed to the welcome#index page. I feel like the main issue is with my nginx config somehow not properly pointing to my unicorn server.

The only warning I am getting in nginx error log is:

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

Server organization:

/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

The problem here is the structure of your application on server, where you should remove the public from within current and the structure should be like this :

home

└── 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

I am no expert. But your nginx config appears to be wrong. I am pasting the config I use for my nginx and hope that solves your problem.

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;
}

Basically I feel the problem lies with your try_files directive. http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files It checks the existence of files in specified order. You have mentioned @app at the end which doesn't match your upstream config. That might be the reason.

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