我在Docker环境中使用Postgres,Puma,Nginx的rails应用程序。一旦我运行docker-compose build和docker-compose,我就可以访问该页面,除了找不到资产(js,images,css)。我已登录功能但是当我成功登录时,我被重定向到localhost而不是localhost / admin / settings。我觉得这是一个Nginx配置问题,但到目前为止还没有取得任何进展。
我的nginx.conf是:
server {
listen 80;
# Allow nginx to serve assets from docker
location ^~ /my-app/assets/ {
rewrite /my-app(/assets/.*) $1;
root /public/;
gzip_static on;
expires max;
add_header Cache-Control public;
add_header Strict-Transport-Security "";
}
# Allow nginx to serve assets from ALB
location ^~ /assets/ {
root /public/;
gzip_static on;
expires max;
add_header Cache-Control public;
add_header Strict-Transport-Security "";
}
location / {
proxy_pass http://my-app:3000;
add_header Strict-Transport-Security "";
}
}
我的docker-compose.yml是:
version: "3.4"
services:
my-app:
build:
context: .
target: my-app
restart: "no"
env_file:
./.my-app.env
links:
- postgres
command: >
/bin/bash -c "
while ! nc -z postgres 5432;
do
echo waiting for postgres;
sleep 1;
done;
echo Connected!;
bundle exec rake db:create db:migrate;
rm -f /app/tmp/pids/server.pid
bundle exec rake assets:clobber
bundle exec rake assets:precompile RAILS_ENV=production
bundle exec rake assets:precompile RAILS_ENV=staging
bundle exec rails server;
"
postgres:
image: postgres:9.6
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- '5432:5432'
my-app-nginx:
build:
context: .
target: nginx
links:
- skymap
restart: "no"
ports:
- '3000:80'
在我的Dockerfile中,我正在运行下面的Nginx指令:
# Start NGINX container config
FROM nginx as nginx
WORKDIR /public
COPY docker/nginx.conf /etc/nginx/conf.d/default.conf
COPY public /public
我不确定我是不是正确设置路径还是资产没有编译。
如果不是问题,你可以配置rails来提供静态文件而不是nginx,它会起作用。
config.serve_static_assets = true