簡體   English   中英

Rails與Nginx和獨角獸初學者問題

[英]Rails with nginx and unicorn beginner issues

大家都在想,是否有人可以指出我的整體服務器組織,nginx配置或unicorn配置的明顯問題?

基本上,問題是我的請求來自Nginx,並且服務於rails靜態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配置

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

這里的問題是服務器上應用程序的結構,您應該從當前結構中刪除公共對象,並且結構應如下所示:

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

我不是專家。 但是您的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/zh-CN/docs/http/ngx_http_core_module.html#try_files它將按指定順序檢查文件是否存在。 您在最后提到了@app,它與您的上游配置不匹配。 那可能就是原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM