Nginx is running on my server (not a docker image) to proxy the subdomain requests to my docker containers.
Having an additional nginx container for each image works well as follows:
docker-compose.yml
version: '2' services: nginx: image: nginx:latest ports: - "8080:80" volumes: - .:/app - ./site.conf:/etc/nginx/conf.d/default.conf networks: - code-network php: image: php:fpm volumes: - .:/app networks: - code-network networks: code-network: driver: bridge
site.conf
server { listen 80; index index.php index.html; server_name localhost; error_log /var/log/nginx/error.log; access_log /var/log/nginx/access.log; root /app; location ~ .php$ { try_files $uri =404; fastcgi_split_path_info ^(.+.php)(/.+)$; fastcgi_pass php:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; } }
/etc/nginx/sites-available/dev.domain.co.uk
server { listen 80; listen [::]:80; server_name dev.domain.co.uk; location / { proxy_pass http://localhost:8080; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } }
But seems wasteful to have an additional instance of nginx for each php site.
How can I route the server instance of nginx directly to each php:fpm container?
note: the docker ‘code-network’ gets renamed appname-code-network upon docker-compose up -d
Advertisement
Answer
Figured this out.
In the nginx server conf, the root is the location to the code on the server host i.e. /home/user/CODE/site
the fastcgi_param is the location of the code on the Docker container, (/app) as defined in docker-compose.yml
fastcgi_param SCRIPT_FILENAME /app$fastcgi_script_name;
You also need to pass to the docker container host ip:
fastcgi_pass 172.21.0.2:9000;
As discovered by hostname -I, when using bash on the container.
I’m yet to discover how to use the container hostname instead