使用Nginx和Puma Rails提供静态文件

问题描述 投票:0回答:1

我试图用unix套接字配置Nginx + Rails + Puma,但我无法为我的服务器提供服务。我有以下配置,如果有人可以指出缺少什么,我将不胜感激。我的Nginx配置;

upstream my_app {
  server unix:///var/run/my_app.sock;
}

server {
  listen 8080;
  server_name _; 
  root /var/www/plantmonitorweb/public; # app location

  location / {
    proxy_pass http://my_app;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }

  # this one is from Rails guides
  location ~ ^/assets/ {
    expires 1y;
    add_header Cache-Control public;

    add_header ETag "";
  }
}

在production.rb

  config.public_file_server.enabled = false
  config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' 

puma.rb没有改变。我用以下命令运行puma服务器;

puma -e production -b unix:///var/run/my_app.sock

我在远程机器上运行那些(Raspberry Pi是特定的)Puma运行良好似乎nginx正在击中我的服务器但是当我打开chrome控制台时我看到以下

 GET http://192.168.1.35:8080/pack/app.js

我在public / pack下检查这个文件,它就在那里。

我的Nginx配置在/etc/nginx/conf.d/中,它包含在Nginx.conf中(包括/etc/nginx/conf.d/*.conf)

ruby-on-rails nginx puma
1个回答
0
投票

在rails 5中,react js文件在public/pack/下提供。因此,我们需要添加静态服务的位置以及资源文件夹。所以在我的nginx配置中添加了以下位置。

 location ~ ^/(assets|packs)/ {
    gzip_static on;
    expires 1y;

    add_header Cache-Control public;
    add_header Last-Modified "";
    add_header ETag "";
  }
© www.soinside.com 2019 - 2024. All rights reserved.