简体   繁体   English

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

[英]serving static files with Nginx and Puma Rails

I am trying to configure Nginx + Rails + Puma with unix sockets but I was not able to serve my asests. 我正在尝试使用unix套接字配置Nginx + Rails + Puma,但是我无法满足自己的需求。 I have following configurations I would appreciate if someone could point what is missing. 我有以下配置,如果有人可以指出缺少的内容,我将不胜感激。 My Nginx configuration; 我的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 "";
  }
}

In production.rb 在production.rb中

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

puma.rb is not changed. puma.rb未被更改。 I am running puma server with following command; 我正在使用以下命令运行puma服务器;

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

I run those in remote machine (Rasberry Pi to be specific) Puma runs fine it seems nginx is hitting my server but when I open chorome console I see following 我在远程计算机(具体来说是Rasberry Pi)中运行这些文件,Puma运行正常,似乎Nginx正在命中我的服务器,但是当我打开chorome控制台时,我看到以下内容

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

I check this file under public/pack and it is there. 我在public / pack下检查此文件,它在那里。

My Nginx configuration is in /etc/nginx/conf.d/ which is included inside of Nginx.conf (include /etc/nginx/conf.d/*.conf) 我的Nginx配置位于/etc/nginx/conf.d/中,该文件包含在Nginx.conf中(包括/etc/nginx/conf.d/*.conf)

In rails 5, react js files are served under public/pack/ . 在Rails 5中,react js文件位于public/pack/ So we need to add those locations to be served statically as well as assets folder. 因此,我们需要添加那些要静态投放的位置以及资产文件夹。 So added following location to my nginx configuration. 因此,将以下位置添加到我的nginx配置中。

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

    add_header Cache-Control public;
    add_header Last-Modified "";
    add_header ETag "";
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM