Rails 项目位于 8080 端口,而 nginx 位于 80 端口

Rails project located at port 8080 while nginx at port 80

我已经将我的 Rails 应用程序部署到 VPS (DigitalOcean)。我已经安装了 NGINX,它将处理我所有的静态 cssjshtml 文件。

我已经通过 capistrano 上传了我的项目。

当我在 example.com 打开我的页面时,它显示页面 欢迎使用 NGINX。我只能通过输入 example.com:8080/admin 来访问我的页面,它不会加载 cssjshtml 文件。

NGINX 不检测由 Rails.

生成的静态文件

我错过了什么?为什么我的 rails 应用程序在 8080 端口上?

我的 nginx.conf 文件是:

upstream puma {
  server unix:///var/www/newsapp/shared/tmp/sockets/newsapp-puma.sock;
}

server {
  listen 80 default_server deferred;
  # server_name example.com;

  root /var/www/newsapp/current/public;
  access_log /var/www/newsapp/current/log/nginx.access.log;
  error_log /var/www/newsapp/current/log/nginx.error.log info;

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

  try_files $uri/index.html $uri @puma;
     location @puma {
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_set_header Host $http_host;
     proxy_redirect off;

     proxy_pass http://puma;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 10M;
  keepalive_timeout 10;
}

我正在使用 Puma。我的 deploy.rb 文件:

set :application, 'newsapp'
set :repo_url, 'https://example@bitbucket.org/example.git'
set :linked_dirs, %w(
   bin log vendor/bundle public/system
   tmp/pids tmp/cache tmp/sockets
)
set :puma_bind, "unix:///var/www/newsapp/shared/tmp/sockets/newsapp-puma.sock"
set :puma_state,      "/var/www/newsapp/shared/tmp/pids/puma.state"
set :puma_pid,        "/var/www/newsapp/shared/tmp/pids/puma.pid"
set :puma_access_log, "/var/www/newsapp/shared/log/puma.error.log"
set :puma_error_log,  "/var/www/newsapp/shared/log/puma.access.log"

namespace :deploy do

   after :restart, :clear_cache do
     on roles(:web), in: :groups, limit: 3, wait: 10 do
     # Here we can do anything such as:
     # within release_path do
     #   execute :rake, 'cache:clear'
     # end
     end
   end

end

我的config/deploy/production.rb:

server "my.server.ip.here",
   :user => "deployer",
   :roles => %w(web app db)

我的 nginx.conf 文件位于 VPS /etc/nginx:

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}
http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    gzip on;
    gzip_disable "msie6";

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

评论了很多行。我刚刚跳过了它们。

我的var/log/nginx/error.log有这样的台词:

 2015/07/12 13:25:08 [emerg] 12215#0: open() "/var/www/newsapp/newsapp/current/log/nginx.access.log" failed (2: No such file or directory)

我想你可能没有删除启用站点的默认页面。

rm -f /etc/nginx/sites-enabled/default


接下来是你的nginx access log open failed问题

尝试更改您的日志文件路径。

access_log /var/www/newsapp/current/log/nginx.access.log;
  # => /var/www/newsapp/shared/log/ACCESS_LOG_FILENAME.log
error_log /var/www/newsapp/current/log/nginx.error.log info;
  # => /var/www/newsapp/shared/log/ERROR_LOG_FILENAME.log

可能我觉得nginx刚启动的时候没有当前文件夹,因为当前文件夹是部署app的时候生成的。
并且 nginx 不会在部署您的应用程序时重新启动。 所以尝试重新启动 nginx 或将 nginx 日志路径修改为静态路径而不是符号链接路径。 (不要忘记修改conf文件后重启nginx)


css,js加载不出来的问题。

部署时是否执行了assets:precompile?
查看Capfile中是否有require 'capistrano/rails'.
然后在 deploy.rb.

中设置你的 :rails_env

再次部署!

在我的 nginx.conf 中,我已将 upstream puma 更改为:

upstream puma {
   server 0.0.0.0:8080;
}

在我的 config/deploy.rb 中,我需要这样写:

set :puma_bind, "0.0.0.0:8080"