0

I'm having problems with static files when served with nginx in my dockerized app.

Stack: Ruby + Hanami + Postgres + Puma + nginx

Static files: images, fonts, js files and css.

I wanted nginx to serve static files instead of Hanami. I have all static files ready in the hanami_project/public folder with this structure:

hanami_project/public
-general asset
-general asset 1

- hanami_app
  - hanami_app_assets

- hanami_app_2
  - hanami_app_2 assets

The hanami_project/public folder is a volume for the nginx service.

This is my docker_compose.yml:

version: '3'
services:
  postgres:
    image: postgres
    environment:
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_DB=${POSTGRES_DB}
    ports:
      - 5432:5432
    volumes: 
      - postgres:/var/lib/postgresql/data

  web:
    build: .
    command: >
      bash -c "bundle exec hanami db migrate
      && bundle exec rake initial_settings:add_default_language
      && bundle exec rake initial_settings:add_session_validity
      && bundle exec rake import_user:create
      && bundle exec rake super_admin:create
      && bundle exec hanami assets precompile
      && bundle exec hanami server"
    expose:
      - 2300
    volumes:
      - ./hanami_log/hanami_app.log:/usr/src/app/hanami_log/hanami_app.log
    links:
      - postgres
    depends_on:
      - postgres

  nginx:
    image: nginx:alpine
    restart: unless-stopped
    tty: true
    ports:
      - "1337:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./nginx_log:/var/log/nginx
      - ./public/assets:/www/static/assets
    depends_on:
      - web

volumes:
  postgres:
  web:
  nginx:

And here is my nginx.conf file:

user nginx;

events {}

http {

    upstream tsr {
        server web:2300;
    }

    server {
        listen 80;
        access_log /var/log/nginx/tsr.access.log;
        error_log  /var/log/nginx/tsr.error.log;

        location / {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;

            proxy_pass http://tsr;
            proxy_redirect off;

            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }

        location /assets {
            root /www/static;
        }

        location /assets/hanami_app_2 {
            root /www/static;
        }
    }
}

EDIT1---

I've changed the root to alias like so:

location /assets/ {
            alias /www/static/assets/;
        }

        location /assets/hanami_app_2/ {
            alias /www/static/assets/hanami_app_2/;
        }

And the nginx logs return the file with 200:

"GET /assets/hanami_app_2/dataTables.fixedHeader.min-dfe62a6df19664362cdead540196ed66.js HTTP/1.1" 200 8148 "http://localhost:8091/hanami_app_2/login" 

However, the page is still stripped.

EDIT1---

As far as I can tell from nginx access and error logs this setup serves the files, but nothing gets loaded apart from images. By not being loaded I mean the entire page seems completely stripped with the exception of images.

Is there any way to pinpoint the issue?

Best, Sebastjan

Sebastjan Hribar
  • 396
  • 3
  • 13
  • Can we see more from your error and access logs? – palindromeotter33 Feb 03 '22 at 18:09
  • Your `proxy_path` doesn't seem to point anywhere. It also might be helpful to try setting up without the container to begin. – palindromeotter33 Feb 03 '22 at 18:10
  • `location /assets/ { alias /www/static/assets/; } location /assets/hanami_app_2/ { alias /www/static/assets/hanami_app_2/; }` can be replaced with `location /assets/ { root /www/static/; }` – palindromeotter33 Feb 03 '22 at 18:11
  • @palindromeotter33 I have changed the location paths as suggested, but nothing changed. Images still get served, but no .js or .css files. The setup is such that the .js invokes all the css classes, if this matters. What more of logs should I append here? I don't understand your comment about `poxy_path`. – Sebastjan Hribar Feb 11 '22 at 07:20

0 Answers0