1

I have a node.js project, working with nginx acting as a reverse proxy. I'm trying the configure nginx to serve static content. Both nginx and node.js are docker containers. I've tried to use the following solutions:

  1. How do you serve static files from an nginx server acting as a reverse proxy for a nodejs server?
  2. Serving static files with NGINX

No matter what i do, I always get the following error:

directory index of "/usr/src/app/public/" is forbidden

Here is my nginx configuration:

server {
    listen 80;
    server_name ~^(.+)$;
    root /usr/src/app/public;

    location / {
        try_files $uri @nodejs;
    }

    location @nodejs {
        default_type text/css;
        proxy_set_header Accept-Encoding  "";
        proxy_set_header Host $host;
        set $key $http_host$request_uri;
        set_escape_uri $escaped_key $key;
        srcache_fetch GET /redis $key;
        srcache_store PUT /redis2 key=$escaped_key&exptime=120;
        add_header X-Cached $srcache_fetch_status;
        proxy_pass http://node;
    }

# Try fetching from Redis
    location = /redis {
       ...
    }
# Storing to Redis
    location = /redis2 {
       ...
    }

    include /etc/nginx/default.d/*.conf;
}

Here is my docker-compose:

services:
  web:
    ports:
      - "127.0.0.1:80:80"
    build:
      context: .
      dockerfile: docker/nginx/Dockerfile
    volumes:
      - ./docker/nginx/server.conf:/etc/nginx/conf.d/server.conf
      - ./docker/nginx/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf
    links:
      - node:node
  node:
    ports:
      - "81:80"
    environment:
      DEVELOP: "true"
    build:
      context: .
      dockerfile: ./docker/node/Dockerfile

As mentioned above, I get error of directory index of "/usr/src/app/public/" is forbidden.

Some more info:

  • Nginx has the relevant permissions (Rnning with root)
  • I'm able to get specific static files from the public directory. For example: /public/image.png
  • When i create some index.html file in the public directory, I'm able to load it. However, The expected behaviour is that nginx will pass me to my backend server using proxy_pass http://node configuration, So it seems that the proxy_pass doesn't occurring.

What could be the issue? Please advise.

Update:

I've updated nginx configuration as suggested to:

try_files $uri @nodejs;

Initially, The issue seemed to be solved and i could load my backend server. However, I order to verify that now the static content is being served from the nginx container, I've deleted the public directory from nodejs container, and now i get the following error:

Failed to lookup view "index" in views directory "/usr/src/app/public"

Does anyone know what could be the issue?

Omri
  • 1,436
  • 7
  • 31
  • 61
  • Use: `try_files $uri @nodejs;` – Richard Smith Dec 10 '19 at 14:31
  • Thanks. I tried this and my backend server was loaded properly. However, In order to verify that the static content is indeed being loaded from the nginx server and not from the nodejs, I deleted the public directory from the nodejs container, And now i get an error from the nodejs container: `Failed to lookup view "index" in views directory "/usr/src/app/public"`. – Omri Dec 10 '19 at 14:48
  • I've updated the question according to the new error. – Omri Dec 12 '19 at 15:27

1 Answers1

1

Since your node container is set to expose via port 81 in the docker-compose file, your proxy_pass has to be updated like this.

proxy_pass http://node:81;

Refer to this article to setup nginx with a nodejs reverse proxy step by step https://medium.com/@francoisromain/setup-node-js-apache-nginx-reverse-proxy-with-docker-1f5a5cb3e71e

DinushaNT
  • 1,137
  • 6
  • 17