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

时间:2015-07-13 07:09:11

标签: ruby-on-rails nginx capistrano puma

我已将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)

2 个答案:

答案 0 :(得分:0)

我想您可能不会删除已启用网站的默认网页。

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


接下来,您的nginx访问日志打开失败问题。

尝试更改日志文件路径。

`"http://localhost/immediateHelp.aspx" != "https://localhost/immediateHelp.aspx"

也许我认为当nginx首先启动时,没有当前文件夹,因为在部署应用程序时会生成当前文件夹。
并且在部署应用程序时不会重新启动nginx。 因此,尝试重新启动nginx或将nginx日志路径修改为静态路径而不是symlink路径。 (不要忘记在修改conf文件后重新启动nginx)


css,js没有加载问题。

您是否执行了资产:部署时预编译?
检查是否有需要' capistrano / rails'在Capfile中。
然后在deploy.rb中设置:rails_env。

再次部署!

答案 1 :(得分:0)

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

upstream puma {
   server 0.0.0.0:8080;
}

在我config/deploy.rb我需要写这个:

set :puma_bind, "0.0.0.0:8080"