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:
- How do you serve static files from an nginx server acting as a reverse proxy for a nodejs server?
- 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 theproxy_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?