gpt4 book ai didi

ruby-on-rails - 无法访问 Assets - dockerized Rails 应用程序,nginx 作为反向代理

转载 作者:行者123 更新时间:2023-12-05 05:55:34 26 4
gpt4 key购买 nike

我正在尝试在生产环境中运行我的测试应用程序。我使用下面的 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

它工作正常,但我在所有 Assets 文件上收到 404:

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

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

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

最佳答案

在生产中,Rails 默认不提供静态 Assets (假设您将在 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

关于ruby-on-rails - 无法访问 Assets - dockerized Rails 应用程序,nginx 作为反向代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69453611/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com