【问题标题】:Rails project located at port 8080 while nginx at port 80Rails 项目位于 8080 端口,而 nginx 位于 80 端口
【发布时间】:2015-07-14 17:04:16
【问题描述】:

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

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

当我在example.com 打开我的页面时,它会显示页面Welcome to NGINX。我只能通过输入example.com:8080/admin 来访问我的页面,它不会加载css、js 和html 文件。

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

我正在使用 彪马。我的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)

【问题讨论】:

  • 你也能展示一下你的 puma 配置吗?
  • 您通过套接字而不是端口访问 Puma。似乎您有多个 Nginx 服务器块,显示它侦听的端口超过 80。请显示整个 Nginx 配置并确保 error_log 为空
  • @mikhailov 这是我在 Rails 项目中的nginx.conf 文件。您的意思是位于我的 VPS 中的配置?
  • @QQQ 是的,我们说服务器
  • @mikhailov 更新了我的问题

标签: ruby-on-rails nginx capistrano puma


【解决方案1】:

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

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


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

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

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首先启动时,没有当前文件夹,因为当前文件夹是在部署应用程序时生成的。
每当部署您的应用程序时,nginx 都不会重新启动。 所以尝试重新启动 nginx 或将 nginx 日志路径修改为静态路径而不是符号链接路径。 (别忘了修改conf文件后重启nginx)


css, js没有加载问题。

您是否在部署时执行了 assets:precompile ?
检查 Capfile 中是否需要“capistrano/rails”。
然后在 deploy.rb 中设置你的 :rails_env。

然后再次部署!

【讨论】:

    【解决方案2】:

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

    upstream puma {
       server 0.0.0.0:8080;
    }
    

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

    set :puma_bind, "0.0.0.0:8080"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-10
      • 2011-03-23
      • 2017-09-21
      • 2015-10-16
      • 1970-01-01
      • 2015-03-05
      • 2014-09-11
      • 1970-01-01
      相关资源
      最近更新 更多