如何使用puma/nginx在/ public中提供不属于资产管道的资产?

chr*_*isp 15 ruby-on-rails nginx amazon-web-services puma

这是一个AWS问题,我正在使用Ruby 2.2(Puma)平台.

我编译的资产(在/ public/assets中)按预期提供./ public中的其他资产未被提供(404).

我在哪里配置?这是一个nginx问题吗?还是美洲狮问题?

或者这只是一个AWS图像问题?

这是一个实例(robots.txt应该从根目录提供):http: //staging.us-west-2.elasticbeanstalk.com/public/robots.txt

值得一提的是,默认的Passenger平台图像开箱即用.

the*_*ict 6

万一它可以帮助任何人,或者有人知道如何改进它,这里的nginx配置终于让它为我工作了.在/.ebextensions/01_files.config中:

files:
    "/etc/nginx/conf.d/webapp_healthd.conf" :
        mode: "000755"
        owner: root
        group: root
        content: |
            upstream my_app {
              server unix:///var/run/puma/my_app.sock;
            }

            log_format healthd '$msec"$uri"'
                            '$status"$request_time"$upstream_response_time"'
                            '$http_x_forwarded_for';

            server {
              listen 80;
              server_name _ localhost; # need to listen to localhost for worker tier
              root /var/app/current/public;

              if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
                set $year $1;
                set $month $2;
                set $day $3;
                set $hour $4;
              }

              access_log  /var/log/nginx/access.log  main;
              access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;

              try_files $uri/index.html $uri @my_app;

              location @my_app {
                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 {
                alias /var/app/current/public/assets;
                gzip_static on;
                gzip on;
                expires max;
                add_header Cache-Control public;
              }
            }
    "/opt/elasticbeanstalk/hooks/appdeploy/post/03_restart_nginx.sh":
        mode: "000755"
        owner: root
        group: root
        content: |
            #!/usr/bin/env bash
            rm /etc/nginx/conf.d/webapp_healthd.conf.bak
            rm /etc/nginx/conf.d/custom.conf            
            service nginx restart
Run Code Online (Sandbox Code Playgroud)


Div*_*ero 5

因此,我使用的是完全相同的环境,并且找到了带有一点Google Fu的解决方案:

使用rails 4+,在文件中:

/config/environments/production.rb
Run Code Online (Sandbox Code Playgroud)

您应该在文件顶部附近找到以下几行

# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
Run Code Online (Sandbox Code Playgroud)

因为我们使用的是乘客(nginx或apache),所以这一切都很好,但Puma不会为我们处理:)

为了解决这个问题...

在您的AWS控制台中,转到有关项目的Elastic beantalk仪表板,然后单击左侧菜单上的“配置”。

现在,在标题为“软件配置”的框中单击小齿轮图标

现在,您应该在“环境属性”下看到一个表,在“属性名称”下的新字段中输入“ RAILS_SERVE_STATIC_FILES”,然后在值字段中键入“ true”(不带引号),然后单击Apply。

中提琴!现在您的项目正在提供静态文件:)

  • 应用程序服务器不应在生产中提供静态资产。请求静态文件访问Rails应用程序效率很低。http://guides.rubyonrails.org/configuring.html (7认同)