Serve static content using docker + nginx + php-fpm
Solution 1:
I managed to solve the problem by mounting the webapp
volume in the nginx
container.
This is what the run-nginx
job looks like now:
run-nginx:
docker run --rm \
--name=nginx \
--net=internal-net \
--volume=$(PWD)$(CONFIG)/webapp.conf:/etc/nginx/conf.d/webapp.domain.com.conf:ro \
--volume=$(PWD)$(WEBAPP)/web:/var/www/webapp/web:ro \
-p 80:80 \
nginx:1.11.0-alpine
And this is the webapp.conf
file, that will try to load the static files from the container and, if that's not possible will proxy the request to the fpm
worker:
server {
listen 80;
server_name webapp.domain.com;
root /var/www/webapp/web;
location ~ \.(js|css|png) {
try_files $uri $uri/;
}
location / {
rewrite ^(.*)$ /index.php$1 last;
}
location ~ ^/index\.php(/|$) {
include fastcgi_params;
fastcgi_pass webapp:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
}
However, I'd like to know if there's a better way to do so instead of sharing the same volume twice. Thanks a lot!