rails - nginx + puma - 静态资产不是由提供的教程链接中的nginx提供的

 手机用户2502936521 发布于 2023-02-08 10:03

我正在使用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

公共/资产文件夹中有资产和资产
我也尝试将m.txt文件放在资产目录中并进行访问

http://localhost/assets/m.txt    

但没有奏效.我也试过这个命令:

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

但这没有帮助.

2 个回答
  • 我是为未来的读者发帖的.我更改托管服务提供商时遇到此错误.资产的标准nginx配置停止工作,我必须更改它:

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

    删除/之前/资产做了伎俩,不知道为什么.ps:location位于服务器块中

    2023-02-08 10:04 回答
  • 我遇到了类似的问题.我从irc上非常有用的#nginx频道得到了答案.他们说这是"惯用的nginx",而另一种形式,尽管更受欢迎,却不鼓励:

    server {
      server_name server.com;
    
      # path for static files
      root /home/production/server.com/current/public;
    
      location / {
          try_files $uri @proxy;
          # if url path access by directory name is wanted:
          #try_files $uri $uri/ @proxy;
      }
    
      location @proxy {
          proxy_pass  http://localhost:9292;
          proxy_set_header Host      $host;
          proxy_set_header X-Real-IP $remote_addr;
      }
    }
    

    / hattip:vandemar

    2023-02-08 10:05 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有