Reputation:
I'm struggling to setup nginx inside a docker container. I basically have two containers:
a php:7-apache container that serves a dynamic website, including its static contents.
a nginx container, with a volume mounted inside it as a /home/www-data/static-content folder (I do this in my docker-compose.yml), to try to serve a static website (unrelated to the one served by the apache container).
I want to use the domain dynamic.localhost to serve my dynamic website, and static.localhost to serve my static website only made up of static files.
I have the following Dockerfile for my nginx container:
########## BASE #########
FROM nginx:stable
########## CONFIGURATION ##########
ARG DEBIAN_FRONTEND=noninteractive
ENV user www-data
COPY ./nginx.conf /etc/nginx/nginx.conf
COPY ./site.conf /etc/nginx/conf.d/default.conf
RUN touch /var/run/nginx.pid && \
chown -R ${user}:${user} /var/run/nginx.pid && \
chown -R www-data:www-data /var/cache/nginx
RUN chown -R ${user} /home/${user}/ && \
chgrp -R ${user} /home/${user}/
USER ${user}
As you see I'm using two configuration files for nginx: nginx.conf and site.conf. Here is nginx.conf (it's not important because there is nothing special in it but if I'm doing something wrong just let me know):
worker_processes auto;
error_log /var/log/nginx/error.log debug;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}
And here is the file site.conf that I have been failing miserably at writing correctly for days now:
server {
listen 8080;
server_name static.localhost;
root /home/www-data/static-content;
location / {
try_files $uri =404;
}
}
server {
listen 8080;
server_name dynamic.localhost;
location / {
proxy_pass http://dynamic;
proxy_redirect off;
proxy_set_header Host $host:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Port 8080;
proxy_set_header X-Forwarded-Host $host:8080;
}
}
(http://dynamic passes the request to the apache container that I name "dynamic").
So basically I keep getting 404 for whatever file I try to access in my static-content directory. E.g.:
I tried various things, like writing try_files /home/www-data/static-content/$uri, but I didn't get any result. I read some parts of nginx documentation and searched on Stack Overflow but nothing that I found helped me. If I made a stupid mistake I apologize, but the only thing that I care about now is to get this to work, and to understand what I'm doing wrong.
Thanks
Upvotes: 3
Views: 2492
Reputation: 97
I had a same issue and I had to change permissions to mounted volume files for user group.
On dockerfile
RUN useradd -G root,www-data -u 1000 ${user}
and changed permissions on host files
chmod 775 /home/www-data/static-content -R
Upvotes: 1
Reputation:
I solved my problems by simply not using a volume for the static files but copying them in the container. I suspect it's a problem of permissions with the way the volume is mounted by docker-compose and the nginx process running as non-root. It's not a perfect solution since I have to give up on using a volume but it'll do.
Upvotes: 1