rails-nginx + puma-通过提供的教程链接,nginx不提供静态资产 [英] rails - nginx + puma - static assets not being served by nginx from the tutorial link provided

查看:102
本文介绍了rails-nginx + puma-通过提供的教程链接,nginx不提供静态资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ubuntu. 这是教程

我正在使用的Nginx配置:

upstream my_app {
server unix:///home/uname/railsproject/my_app.sock;
}

server {
listen 88; #(I used exact 88 when I am testing now)
server_name localhost; # I used exact localhost when I am testing this
root /home/uname/railsproject/public; # I assume your app is located at that location

location / {
 proxy_pass http://my_app; # match the name of upstream directive which is defined above
 proxy_set_header Host $host;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

location ~* ^/assets/ {
 # Per RFC2616 - 1 year maximum expiry
 expires 1y;
 add_header Cache-Control public;

 # Some browsers still send conditional-GET requests if there's a
 # Last-Modified header or an ETag header even if they haven't
 # reached the expiry date sent in the Expires header.
 add_header Last-Modified "";
 add_header ETag "";
 break;
 }

}

彪马命令

RAILS_ENV=production puma -e production  -b unix:///home/uname/railsproject/my_app.sock -p 8000

在地址栏中,我输入

http://localhost/ 

,然后打开网站,但静态资产不起作用.当然,我跑了

RAILS_ENV=production rake assets:precompile

和资产在public/assets文件夹中可用
我还尝试将m.txt文件放置在资产目录中并访问

http://localhost/assets/m.txt    

但是没有用.我也尝试了以下命令:

sudo chown -R www-data:www-data public/

但这没有帮助.

解决方案

我正在为将来的读者发布信息. 更改托管服务提供商时遇到此错误.我用于资产的标准nginx conf停止工作,我必须将其更改为:

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

在/assets成功之前删除/,不知道为什么. ps:位置在服务器块中

I am using Ubuntu. Here is the tutorial

Nginx config I am using:

upstream my_app {
server unix:///home/uname/railsproject/my_app.sock;
}

server {
listen 88; #(I used exact 88 when I am testing now)
server_name localhost; # I used exact localhost when I am testing this
root /home/uname/railsproject/public; # I assume your app is located at that location

location / {
 proxy_pass http://my_app; # match the name of upstream directive which is defined above
 proxy_set_header Host $host;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

location ~* ^/assets/ {
 # Per RFC2616 - 1 year maximum expiry
 expires 1y;
 add_header Cache-Control public;

 # Some browsers still send conditional-GET requests if there's a
 # Last-Modified header or an ETag header even if they haven't
 # reached the expiry date sent in the Expires header.
 add_header Last-Modified "";
 add_header ETag "";
 break;
 }

}

Puma command

RAILS_ENV=production puma -e production  -b unix:///home/uname/railsproject/my_app.sock -p 8000

In the address bar, I am typing

http://localhost/ 

and then website opening but static assets not working. Of course, I ran

RAILS_ENV=production rake assets:precompile

and assets are available in public/assets folder
I also tried placing m.txt file in assets directory and accessing

http://localhost/assets/m.txt    

but didn't work. I also tried this command:

sudo chown -R www-data:www-data public/

but this didn't help.

解决方案

I'm posting for future readers. I encounter this error when I change my hosting provider. My standard nginx conf for assets stop working, I have to change it for:

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

Removing the / before /assets did the trick, don't know why. ps: location is in the server block

这篇关于rails-nginx + puma-通过提供的教程链接,nginx不提供静态资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆