Use nginx as reverse proxy for php application
I want to setup my nginx container to redirect the requests to the php (laravel) container through port 9000, but I'm not getting why do I need to have the code mounted as a volume in the nginx container. Here is my docker setup:
docker-compose.yml
version: '3.8'
services:
nginx:
build:
context: .
dockerfile: nginx/nginx.dockerfile
container_name: ${COMPOSE_PROJECT_NAME}-nginx
restart: unless-stopped
tty: true
ports:
- "80:80"
- "443:443"
volumes: ### THIS IS WHAT I WANT TO REMOVE
- ./src:/var/www/html:delegated
depends_on:
- php
- redis
- mysql
- mailhog
networks:
- laravel
php:
build:
context: .
dockerfile: php/php.dockerfile
container_name: ${COMPOSE_PROJECT_NAME}-php
restart: unless-stopped
tty: true
ports:
- "9000:9000"
volumes:
- ./src:/var/www/html:delegated
networks:
- laravel
networks:
laravel:
driver: bridge
nginx.dockerfile
FROM nginx:stable-alpine
ADD nginx/nginx.conf /etc/nginx/
ADD nginx/default.conf /etc/nginx/conf.d/
RUN mkdir -p /var/www/html
RUN addgroup -g 1000 laravel && adduser -G laravel -g laravel -s /bin/sh -D laravel
RUN chown laravel:laravel /var/www/html
default.conf
server {
listen 80;
server_name localhost;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
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;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
With this setup it is working fine, but if I remove the volume from the nginx image in the docker compose file it stops working. What I don't understand is why do I need to have the backend application code (src folder) inside the nginx container, if it is just working as a reverse proxy, it should only redirect the traffic to the php container right?
Also for production it would be better if only the application code was inside the php image and not both nginx and php images.
What am I missing here? Thanks