Rails资产未在Docker上加载

时间:2018-09-10 19:27:52

标签: ruby-on-rails docker nginx

我有一个在Docker环境中使用Postgres,Puma,Nginx的Rails应用程序。一旦我运行了docker-compose build和docker-compose up,就可以访问该页面,但找不到资产(js,图像,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

我不确定是否设置路径不正确或资产是否尚未编译。

1 个答案:

答案 0 :(得分:1)

如果这不是问题,则可以将rails配置为提供静态文件,而不是nginx,它将起作用。

config.serve_static_assets = true