Docker-compose + Nginx + Multi Site + Set Server-Name (localhost)
ตัวอย่างการเขียน Docker Compose ใช้งาน Nginx และ PHP โดย การเชื่อมต่อ PHP Container แบบ Multi Site และ Set Server_name…
Docker-compose + Nginx + Multi Site + Set Server-Name (localhost)
ตัวอย่างการเขียน Docker Compose ใช้งาน Nginx และ PHP โดย การเชื่อมต่อ PHP Container แบบ Multi Site และ Set Server_name เพื่อให้ใช้งานได้เหมือนใช้งานเว็ปไซค์จริง

ตัวอย่าง Docker-compose.yml
version: '3'
services:
nginx:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./site1:/usr/share/nginx/html/site1
- ./site2:/usr/share/nginx/html/site2
depends_on:
- php1
- php2
php1:
image: php:7.4-fpm
volumes:
- ./site1:/usr/share/nginx/html/site1
php2:
image: php:7.4-fpm
volumes:
- ./site2:/usr/share/nginx/html/site2
volumes:
site1:
site2:
หลังจากที่สร้าง file docker-compose ภานใน Folder Project แล้ว สร้าง file ชื่อ nginx.conf เพื่อ config nginx
events { }
http {
server {
listen 80;
server_name site1.local;
root /usr/share/nginx/html/site1; #document_root
index index.php index.html; #fastcgi_script_name
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass php1:9000; # ส่งคำร้องขอ containner_nane php1
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
server {
listen 80;
server_name site2.local;
root /usr/share/nginx/html/site2; #document_root
index index.php index.html; #fastcgi_script_name
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass php2:9000; # ส่งคำร้องขอ PHP ไปยัง php2
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
}
สร้าง folder site1 และ site2 ภายใน จะมีไฟล index.php เพื่อทดสอบหน้าเว็ปของเรา
หลังจากนั้นแก้ไขไฟล C:\Windows\System32\drivers\etc\host เพิ่ม server name ที่เราเพิ่งสร้างลงไป
127.0.0.1 site1.local
127.0.0.1 site2.local
หลังจากที่เตรียมไฟล์ครบแล้ว ก็รัน “ docker-compose up -d ” เพื่อสร้าง Container
สามารถเข้าทดสอบ site1 และ site2 โดยผ่าน url ที่กำหนัดไว
site1.local หรือ site1.local/index.php
site2.local หรือ site2.local/index.php 메타데이터
- post_id
- ee850a90d8e2
- slug
- docker-compose-nginx-multi-site-ee850a90d8e2
- url
- https://medium.com/@yodea/docker-compose-nginx-multi-site-ee850a90d8e2
- canonical_url
- https://medium.com/@yodea/docker-compose-nginx-multi-site-ee850a90d8e2
- author_url
- https://medium.com/@yodea
- status
- ok
- fetched_at
- 2026-07-23 06:28:28