无法访问资产 - dockerized Rails 应用程序,nginx 作为反向代理

Cannot access assets - dockerized Rails app, nginx as a reverse proxy

我正在尝试 运行 我的测试应用程序投入生产。我使用下面 Dockerfile:

对它进行了 dockerized
FROM ruby:3.0.2

WORKDIR /app

COPY Gemfile Gemfile.lock ./
RUN bundle install

RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update && apt-get install -y yarn

COPY . ./

RUN bin/rails assets:precompile

CMD ["rails", "server", "-b", "0.0.0.0"]

我的 sites-enabled 此站点的配置如下所示:

server {
  server_name   mywebsite.com;

  location / {
    proxy_pass  http://localhost:3000;
  }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mywebsite.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = mywebsite.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


  listen        80;
  server_name   mywebsite.com;
    return 404; # managed by Certbot
}

我 运行 手动使用此命令:

sudo docker run -p 3000:3000 -e DATABASE_NAME=dbname -e DATABASE_USER=dbuser -e DATABASE_PASSWORD=dbpassword -e RAILS_ENV=production -e DATABASE_HOST=my_ip -d 72b09817c012

它正在运行,但我在所有资产文件上都收到 404:

https://mywebsite.com/assets/application-04024382391bb910584145d8113cf35ef376b55d125bb4516cebeb14ce788597.css
https://mywebsite.com/packs/css/application-1f2a30e1.css
https://mywebsite.com/packs/js/application-98802c4d22f32af59b73.js

当我登录 运行ning 容器并检查 public/assetspublic/packs 文件夹时,文件就在那里。

知道我在这里遗漏了什么吗?

在生产中,Rails 默认不提供静态资产(假设您将它们托管在 CDN 上)。您可以使用配置设置翻转它:https://edgeguides.rubyonrails.org/configuring.html#config-public-file-server-enabled

# in: environments/production.rb

Rails.application.configure do
  # ...
  config.public_file_server.enabled = true
  # ...
end