การจัดการ URL เป็นหนึ่งในทักษะสำคัญที่นักพัฒนาและผู้ดูแลระบบเซิร์ฟเวอร์ต้องเรียนรู้ URL Rewriting ใน Nginx ช่วยให้คุณเปลี่ยนเส้นทาง URL ได้อย่างยืดหยุ่น ซึ่งมีประโยชน์มากในการ SEO เพิ่มความปลอดภัย หรือจัดการโครงสร้าง URL ของเว็บไซต์ให้ดีขึ้น บทความนี้จะแนะนำวิธีใช้งาน rewrite directive อย่างละเอียด พร้อมตัวอย่างโค้ดและการประยุกต์ใช้จริง
หากคุณเป็นเจ้าของเว็บไซต์หรือผู้บริหารเซิร์ฟเวอร์ที่ใช้ Nginx การเข้าใจ URL Rewriting จะช่วยให้คุณควบคุมการไหลของผู้ใช้ได้ดีขึ้น และสร้าง user experience ที่ดียิ่งขึ้น
Nginx Rewrite Directive คืออะไร
Rewrite directive ใน Nginx เป็นคำสั่งที่ใช้สำหรับเปลี่ยนแปลง URI (Uniform Resource Identifier) ของคำขอ HTTP ก่อนที่เซิร์ฟเวอร์จะประมวลผล โดยใช้ regular expression (regex) เพื่อจับคู่รูปแบบ URL และแปลงเป็นเส้นทางใหม่
Nginx rewrite directive ทำงานในขั้นตอน rewrite phase ซึ่งมี priority สูงในการประมวลผล request ก่อน location blocks ตัวอื่นๆ จึงมีความสำคัญในการจัดการ URL ขาเข้า
ลำดับการประมวลผล Rewrite Phase
- 1. Rewrite Phase — ประมวลผลคำสั่ง rewrite และ return
- 2. Finding Location — หา location block ที่ตรงกับ URI ใหม่
- 3. Location Phase — ประมวลผลคำสั่งภายใน location block ที่เลือก
- 4. Response Phase — ส่งผลลัพธ์กลับไปยังไคลเอนต์
Return vs Rewrite — ความแตกต่างที่สำคัญ
นักเรียนรู้ใหม่มักจะสับสนระหว่าง return และ rewrite ดังนั้นสิ่งสำคัญคือต้องเข้าใจความแตกต่างของทั้งสอง
| ลักษณะ | Return | Rewrite |
| วัตถุประสงค์ | ส่งค่ากลับโดยตรง (redirect/response code) | เปลี่ยน URI และเข้าสู่ location ถัดไป |
| การไหลของ Request | หยุดการประมวลผลทันที | ดำเนินการประมวลผลต่อโดยใช้ URI ใหม่ |
| HTTP Status Code | มีค่าตั้งแต่ 200-599 | ไม่มี status code เนื่องจาก request ยังดำเนินต่ออยู่ |
| ตัวอย่าง | return 301 https://newurl.com; | rewrite ^/old$ /new last; |
ตัวอย่าง: Return สำหรับ Redirect
server {
listen 80;
server_name example.com;
# Redirect ไปยัง URL ใหม่พร้อมส่ง HTTP 301
return 301 https://example.com$request_uri;
}
ตัวอย่าง: Rewrite สำหรับการเปลี่ยนเส้นทาง
server {
listen 80;
server_name example.com;
root /var/www/html;
# เปลี่ยน /old เป็น /new และดำเนินการต่อ
rewrite ^/old$ /new last;
}
Rewrite Flags — ธงควบคุมการประมวลผล
ในการเขียนคำสั่ง rewrite Nginx มีธงพิเศษ (flags) ที่ใช้ควบคุมวิธีการประมวลผลของ rewrite directive เรียนรู้เกี่ยวกับธงสำคัญๆ ต่อไปนี้
ธง last — หยุดประมวลผล Rewrite ปัจจุบัน
ธง last ใช้เพื่อหยุดการประมวลผลคำสั่ง rewrite ที่เหลือและเริ่มต้นใหม่โดยใช้ URI ที่เปลี่ยนแปลง
server {
listen 80;
server_name example.com;
# เปลี่ยน /blog เป็น /new-blog
rewrite ^/blog$ /new-blog last;
location / {
proxy_pass http://backend;
}
}
ธง break — หยุดประมวลผลใน Location Block
ธง break ใช้เพื่อหยุดการประมวลผล rewrite directive ที่เหลือ แต่ยังคงประมวลผลภายใน location block ต่อไป
location /files/ {
# เปลี่ยน .html เป็น .php แต่ยังประมวลผลใน location ต่อไป
rewrite ^([^.]+)\.html$ $1.php break;
proxy_pass http://backend;
}
ธง redirect — ส่งการ Redirect 302 (Temporary)
ธง redirect ใช้เพื่อส่ง HTTP 302 (Temporary Redirect) ไปยังไคลเอนต์
server {
listen 80;
server_name example.com;
# ส่ง redirect 302 ไปยัง https
rewrite ^/(.*)$ https://example.com/$1 redirect;
}
ธง permanent — ส่งการ Redirect 301 (Permanent)
ธง permanent ใช้เพื่อส่ง HTTP 301 (Permanent Redirect) ไปยังไคลเอนต์
server {
listen 80;
server_name oldsite.com;
# ส่ง redirect 301 ไปยัง newsite
rewrite ^/(.*)$ https://newsite.com/$1 permanent;
}
ตัวอย่างการใช้ URL Rewriting ทั่วไป
ตัวอย่าง 1: Rewrite SEO-friendly URLs
สมมติว่าคุณมี URL เดิมเป็น /article.php?id=123 และต้องการเปลี่ยนเป็น /article/123
server {
listen 80;
server_name example.com;
root /var/www/html;
# Rewrite /article/123 เป็น /article.php?id=123
rewrite ^/article/([0-9]+)$ /article.php?id=$1 last;
}
ตัวอย่าง 2: Redirect HTTP ไปยัง HTTPS
server {
listen 80;
server_name example.com www.example.com;
# Redirect ทั้งหมดไปยัง HTTPS
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name example.com www.example.com;
# SSL configuration
ssl_certificate /path/to/cert.crt;
ssl_certificate_key /path/to/key.key;
root /var/www/html;
}
ตัวอย่าง 3: Rewrite ไฟล์ที่ไม่มีอยู่
หากผู้ใช้ขอไฟล์ที่ไม่มี ให้ redirect ไปยัง index.php
server {
listen 80;
server_name example.com;
root /var/www/html;
# ถ้า URI ไม่ใช่ไฟล์หรือ directory ให้ redirect ไปยัง index.php
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?url=$1 last;
}
}
ตัวอย่าง 4: Remove .php extension
ซ่อน .php extension เพื่อให้ URL ดูสะอาดขึ้น
server {
listen 80;
server_name example.com;
root /var/www/html;
# เปลี่ยน /page เป็น /page.php
rewrite ^([^.]+)$ $1.php last;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
การแก้ไขปัญหา URL Rewriting
ปัญหา 1: Rewrite ไม่ทำงาน
ถ้า rewrite directive ไม่ทำงาน ให้ตรวจสอบ:
- ตรวจสอบ syntax ของ rewrite rule
- เปิด debug mode:
error_log /var/log/nginx/debug.log debug; - ตรวจสอบ permission ของไฟล์และ directory
- ตรวจสอบ regex pattern ด้วย tools เช่น regex101.com
- ล้าง cache ของเบราว์เซอร์
ปัญหา 2: Infinite Redirect Loop
หากเกิด infinite loop ให้ใช้ flags อย่างถูกต้อง:
# ❌ ผิด - เกิด infinite loop
rewrite ^/page$ /page permanent;
# ✓ ถูก - ใช้ last flag
rewrite ^/old-page$ /new-page last;
ปัญหา 3: Query String หายไป
หาก query string หายไปเมื่อ rewrite ให้ใช้ $args:
# ✓ ถูก - เก็บ query string
rewrite ^/api/v1/(.*)$ /api/v2/$1?$args last;
Best Practices สำหรับ URL Rewriting
- ใช้ permanent redirect (301) สำหรับ URL ที่เปลี่ยนแปลงถาวร
- ใช้ temporary redirect (302) สำหรับการเปลี่ยนแปลงชั่วคราว
- ทำ rewrite ให้เรียบง่าย และหลีกเลี่ยง rewrite ที่ซับซ้อน
- ตรวจสอบ regex ให้ดีก่อน กับ online regex tools
- ใช้ comments เพื่ออธิบาย rewrite rules
- บันทึก logs เพื่อติดตามการเปลี่ยนแปลง
- ทดสอบ rewrite rules ในสภาพแวดล้อมทดสอบก่อน production
สรุป
URL Rewriting เป็นเครื่องมือที่มีประสิทธิภาพสำหรับการจัดการ URL ใน Nginx ด้วยการทำความเข้าใจเกี่ยวกับ rewrite directive, flags, และ best practices คุณสามารถสร้าง URL structure ที่สะอาดและ SEO-friendly ได้
จำไว้ว่า URL rewriting เป็นทักษะที่ต้องฝึกฝน ดังนั้นอย่าลังเลที่จะทดลองและเรียนรู้จากข้อผิดพลาด
แนะนำบริการ DE
หากคุณต้องการควบคุม Nginx configuration อย่างเต็มที่เพื่อจัดการ URL rewriting ได้ตามต้องการ DE Cloud VPS เป็นตัวเลือกที่ดีเลิศ ด้วย Cloud VPS คุณจะมีการเข้าถึง root และสามารถปรับแต่ง Nginx configuration ได้อย่างสมบูรณ์
หากต้องการให้ DE ดูแล Nginx configuration สำหรับคุณ ให้พิจารณา DE Cloud Hosting ซึ่งเรามีทีมผู้เชี่ยวชาญที่พร้อมตั้งค่า URL rewriting rules ตามความต้องการของคุณ
ไม่ว่าคุณจะเลือกแบบไหน DE มีบริการ 24/7 support พร้อมช่วยเหลือเมื่อใดก็ได้ เยี่ยมชมเว็บไซต์ DE เพื่อเรียนรู้เพิ่มเติมเกี่ยวกับบริการของเรา

