การใช้งาน Nginx (Engine-x) บน Docker

Docker by Keptcode.com

Last Updated: 2026-03-04 23:57

Repo: Github.com (รบกวนกด Start ให้ด้วยนะคับ)

https://github.com/coachmaxz/docker-nginx-example

โครงสร้างโฟลเดอร์

Path: [skeleton]

Language: Project Structure

├── conf.d/
│  ├── nginx.conf
│  └── localhost.conf
├── www/
│  └── index.html
└── docker-compose.yml

สร้างไฟล์: ตั้งค่า Nginx สำหรับเป็น Web Server เบื่องต้น

Path: [skeleton]\conf.d\nginx.conf

Language: Nginx

user nginx;
worker_processes auto;

pid /var/run/nginx.pid;
error_log /var/log/nginx/error.log warn;

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 off;
  error_log /var/log/nginx/access.error.log;

  sendfile on;

  keepalive_timeout  500;
  send_timeout  600;

  client_max_body_size  100M;
  client_header_timeout  600;
  client_body_timeout  600;

  fastcgi_read_timeout  300;

  proxy_read_timeout  600;
  proxy_connect_timeout  600;
  proxy_send_timeout  600; 

  include /etc/nginx/conf.d/*.conf;

}

ไฟล์: nginx.conf

[skeleton]/conf.d/nginx.conf

สร้างไฟล์: ตั้งค่า Web Server บน Nginx Server เบื้องต้น

Path: [skeleton]\conf.d\localhost.conf

Language: Nginx

server {

  listen 80 default_server;
  listen [::]:80 default_server;

  server_name _;
  charset utf-8;

  client_max_body_size 100M;

  index index.html index.htm;
  root /usr/share/nginx/html/www;

  add_header X-Frame-Options "SAMEORIGIN";
  add_header X-XSS-Protection "1; mode=block";
  add_header X-Content-Type-Options "nosniff";

  location / {
    try_files $uri $uri/ @rewrites;
  }

  location @rewrites {
    rewrite ^(.+)$ /index.html last;
  }

  location = /favicon.ico {
    log_not_found off;
    access_log off;
  }

  location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
  }

  location ~ /\.(ht|svn|git) {
    deny all;
    return 404;
  }

  location ~ /.well-known {
    allow all;
  }

  location ~ /\. {
    deny all;
  }

  access_log off;
  error_log /var/log/nginx/localhost-access.error.log;
  error_page 401 403 404 /404.html;

}

ไฟล์: localhost.conf

[skeleton]/conf.d/localhost.conf

สร้างไฟล์: รัน Container บน Docker Engine เบื้องต้น

Path: [skeleton]\docker-compose.yml

Language: Docker Engine

services:

  server_nginx_01:
    image: nginx:1.27.4-alpine
    container_name: server-nginx-01
    working_dir: /usr/share/nginx/html
    restart: always
    environment:
      - NGINX_PORT=80
      - TERM=xterm
    volumes:
      - ./www:/usr/share/nginx/html/www
      - ./conf.d/nginx.conf:/etc/nginx/nginx.conf
      - ./conf.d/localhost.conf:/etc/nginx/conf.d/default.conf
    ports:
      - "80:80"
    networks:
      - server_nginx_network

networks:
  server_nginx_network:
    driver: bridge

ไฟล์: docker-compose.yml

[skeleton]/docker-compose.yml

รัน: คำสั่ง Docker เพื่อสร้าง Container ที่ต้องการ

Path: [skeleton]

Language: Bash

docker compose up -d