Nginx

Nginx Configuration เจาะลึก: Main, Events, HTTP และ Server Context

Nginx Configuration เป็นหัวใจสำคัญของการจัดการ Nginx Web Server อย่างมีประสิทธิภาพ การเข้าใจคุณลักษณะของแต่ละ Context เช่น Main Context, Events Context, HTTP Context และ Server Context จึงเป็นสิ่งจำเป็นสำหรับผู้ดูแลระบบและนักพัฒนา ไม่ว่าคุณจะใช้ Cloud VPS หรือ Cloud Hosting ที่รองรับ Nginx บทความนี้จะอธิบายรายละเอียดของแต่ละส่วนการ Configuration ของ Nginx ให้คุณเข้าใจอย่างถูกต้อง

ภาพรวม Nginx Configuration Contexts

ไฟล์ Configuration ของ Nginx (nginx.conf) แบ่งออกเป็นหลายชั้น (Contexts) ตั้งแต่บนสุดไปจนถึงล่างสุด แต่ละ Context มีหน้าที่และ Directive ที่แตกต่างกัน Contexts หลักในไฟล์ nginx.conf ได้แก่:

  • Main Context — ส่วนระดับบนสุด ตั้งค่าพื้นฐานของ Nginx
  • Events Context — กำหนดวิธีการจัดการการเชื่อมต่อ
  • HTTP Context — ตั้งค่าเกี่ยวกับ HTTP และ HTTPS
  • Server Context — ตั้งค่าเซิร์ฟเวอร์เฉพาะ (Virtual Host)
  • Location Context — ตั้งค่ากฎสำหรับ URL Path เฉพาะ

Main Context — ส่วนหลักของ Nginx Configuration

Main Context คือส่วนที่อยู่นอกสุดของไฟล์ nginx.conf ซึ่งกำหนดค่าทั่วไปสำหรับทั้งเซิร์ฟเวอร์ Nginx Directive ในส่วนนี้จะมีผลต่อการทำงานทั้งหมดของ Nginx

Directive ที่สำคัญใน Main Context:

  • user — ระบุผู้ใช้ที่ Nginx จะทำงาน
  • worker_processes — จำนวน Worker Process
  • worker_rlimit_nofile — จำนวนไฟล์ที่สามารถเปิดได้
  • error_log — ตำแหน่งไฟล์ Error Log
  • pid — ตำแหน่งไฟล์ PID ของ Nginx

ตัวอย่าง Main Context Configuration:

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
worker_rlimit_nofile 65535;

user nginx; หมายถึง Nginx จะทำงานภายใต้ผู้ใช้ชื่อ nginx ส่วน worker_processes auto; จะให้ Nginx ใช้จำนวน Core ของ CPU ที่มีอยู่อัตโนมัติ ซึ่งเหมาะสำหรับ Cloud VPS ที่มีการปรับขนาดไดนามิก

Events Context — จัดการการเชื่อมต่อและ Concurrency

Events Context ใช้สำหรับกำหนดวิธีการในการจัดการการเชื่อมต่อของ Nginx ส่วนนี้มีความสำคัญต่อประสิทธิภาพของเซิร์ฟเวอร์ เนื่องจากจะกำหนดจำนวนการเชื่อมต่อพร้อมกันที่เซิร์ฟเวอร์สามารถรองรับได้

ตัวอย่าง Events Context:

events {
    worker_connections 2048;
    use epoll;
    multi_accept on;
}

worker_connections 2048; หมายถึงแต่ละ Worker Process สามารถรองรับการเชื่อมต่อได้พร้อมกัน 2048 ครั้ง สำหรับ Cloud VPS ที่มี Traffic สูง อาจจำเป็นต้องเพิ่มค่านี้ขึ้น

HTTP Context — ตั้งค่า Protocol และ Module ทั่วไป

HTTP Context เป็นส่วนที่ใหญ่ที่สุดของไฟล์ Configuration ส่วนนี้กำหนดค่าการตั้งค่าทั่วไปสำหรับ HTTP Request และ Response รวมถึงการใช้โมดูลต่าง ๆ

ตัวอย่าง HTTP Context Configuration:

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"';

    access_log /var/log/nginx/access.log main;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    client_max_body_size 100M;

    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_types text/plain text/css text/xml text/javascript
               application/json application/javascript application/xml+rss;

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

sendfile on; และ tcp_nopush on; ร่วมกันช่วยปรับปรุงประสิทธิภาพการส่งไฟล์ ส่วน gzip on; จะบีบอัดการตอบสนอง HTTP ซึ่งช่วยลดการใช้แบนด์วิดท์บน Cloud VPS ของคุณได้อย่างมาก

Server Context — ตั้งค่า Virtual Host เฉพาะ

Server Context ใช้สำหรับตั้งค่า Virtual Host เฉพาะ คุณสามารถโฮสต์เว็บไซต์หลายตัวบนเซิร์ฟเวอร์เดียวกันได้ แต่ละ Server Block จะสอดคล้องกับโดเมนหรือ IP Address เฉพาะ

ตัวอย่าง Server Context Configuration:

server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name example.com www.example.com;

    ssl_certificate /etc/nginx/ssl/certificate.crt;
    ssl_certificate_key /etc/nginx/ssl/private.key;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;

    root /var/www/html;
    index index.html index.php;

    access_log /var/log/nginx/example.com_access.log;
    error_log /var/log/nginx/example.com_error.log;
}

Location Context — จัดการ URL Path เฉพาะ

Location Context ใช้สำหรับตั้งค่ากฎเฉพาะสำหรับ URL Path หรือ Request URI ต่าง ๆ ซึ่งอยู่ภายใน Server Context

วิธีการจับคู่ Location:

  • location / { } — จับคู่ URL ทั้งหมด (Default)
  • location /api/ { } — จับคู่ URL ที่เริ่มต้นด้วย /api/
  • location ~ \.php$ { } — จับคู่ URL ที่ลงท้ายด้วย .php (Regex)
  • location ~* \.(jpg|jpeg|png|gif)$ { } — จับคู่ไฟล์รูปภาพ (Case Insensitive)
  • location = /exact { } — จับคู่ URL ที่ตรงกันเท่านั้น

ตัวอย่าง Location Context:

location / {
    try_files $uri $uri/ /index.html;
}

location ~ \.php$ {
    fastcgi_pass unix:/var/run/php-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 30d;
    add_header Cache-Control "public, immutable";
}

location ~ /\. {
    deny all;
}

ตัวอย่าง Configuration แบบเต็มสำหรับ Cloud VPS

นี่คือตัวอย่าง Configuration ที่สมบูรณ์สำหรับการใช้งาน Cloud VPS:

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
worker_rlimit_nofile 65535;

events {
    worker_connections 2048;
    use epoll;
    multi_accept on;
}

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"';

    access_log /var/log/nginx/access.log main;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    client_max_body_size 50M;

    gzip on;
    gzip_vary on;
    gzip_comp_level 6;
    gzip_types text/plain text/css application/json application/javascript;

    server {
        listen 80;
        server_name example.com www.example.com;
        return 301 https://$server_name$request_uri;
    }

    server {
        listen 443 ssl http2;
        server_name example.com www.example.com;

        ssl_certificate /etc/nginx/ssl/certificate.crt;
        ssl_certificate_key /etc/nginx/ssl/private.key;
        ssl_protocols TLSv1.2 TLSv1.3;

        root /var/www/html;
        index index.html index.php;

        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ \.php$ {
            fastcgi_pass unix:/var/run/php-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }

        location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg|woff|woff2)$ {
            expires 30d;
            add_header Cache-Control "public, immutable";
        }

        location ~ /\. {
            deny all;
        }
    }
}

สรุป

การเข้าใจ Nginx Configuration ลึกซึ้งถึงแต่ละ Context อย่าง Main Context, Events Context, HTTP Context และ Server Context จะช่วยให้คุณสามารถจัดการเซิร์ฟเวอร์ได้อย่างมีประสิทธิภาพและปลอดภัย การตั้งค่า Configuration อย่างถูกต้องจะช่วยให้เซิร์ฟเวอร์ทำงานได้อย่างลื่นไหล รับรองความปลอดภัย และรองรับผู้เยี่ยมชมจำนวนมากได้

แนะนำบริการ Cloud VPS ของ DE

หากคุณต้องการเซิร์ฟเวอร์ที่มีความสามารถและเสถียรภาพสูง DE (Dot Enterprise) ขอแนะนำ Cloud VPS ซึ่งเป็นโซลูชันการโฮสต์ที่ยืดหยุ่นและมีราคาที่เหมาะสม สามารถปรับขนาดได้ตามความต้องการ ควบคุม Nginx Configuration ได้เต็มรูปแบบ พร้อมทีมสนับสนุน 24/7

สำหรับข้อมูลเพิ่มเติม โปรดเยี่ยมชม https://de.co.th/cloud-vps หรือติดต่อทีมของ DE วันนี้