สร้าง Pretty URL สำหรับเว็บไซต์ บน Nginx
06 ก.ค. 2025
sudo a2enmod expires headers
sudo a2enconf caching
sudo systemctl reload apache2
ตัวอย่างเนื้อหาใน caching.conf
<IfModule mod_expires.c>
ExpiresActive On
# รูปภาพ / ฟอนต์ เก็บ 30 วัน
ExpiresByType image/jpeg "access plus 30 days"
ExpiresByType image/png "access plus 30 days"
ExpiresByType image/gif "access plus 30 days"
ExpiresByType font/woff2 "access plus 30 days"
# CSS / JS เก็บ 7 วัน
ExpiresByType text/css "access plus 7 days"
ExpiresByType application/javascript "access plus 7 days"
# HTML ไม่เก็บ (เสมอตรวจสอบใหม่)
ExpiresByType text/html "access plus 0 seconds"
</IfModule>
<IfModule mod_headers.c>
<FilesMatch "\.(jpg|jpeg|png|gif|svg|css|js|woff2)$">
Header set Cache-Control "public, max-age=2592000, immutable"
</FilesMatch>
<FilesMatch "\.(html|htm)$">
Header set Cache-Control "no-cache, must-revalidate"
</FilesMatch>
</IfModule>
ตรวจสอบผล
curl -I https://your-domain.tld/path/to/file.js
# ดู Cache-Control / Expires ว่าตรงตามที่ตั้งไว้
2. Compression (GZIP/Deflate)
<IfModule mod_deflate.c>
# บีบอัด HTML, XML, TXT
AddOutputFilterByType DEFLATE text/html text/plain text/xml
# บีบอัด CSS, JavaScript, JSON
AddOutputFilterByType DEFLATE text/css application/javascript application/json
# บีบอัด SVG
AddOutputFilterByType DEFLATE image/svg+xml
# เพื่อความเข้ากันกับเบราเซอร์เก่า
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# แจ้งเบราเซอร์ว่ารับ-ส่งแบบบีบอัดได้ (Vary header)
Header append Vary Accept-Encoding
</IfModule>
ตรวจสอบผล
sudo a2enmod mpm_event proxy_fcgi setenvif http2 headers
sudo a2enmod http2
sudo systemctl restart apache2
เพิ่มใน VirtualHost (SSL)
<VirtualHost *:443>
ServerName your-domain.tld
# เปิด HTTP/2 ควบคู่ HTTP/1.1
Protocols h2 http/1.1
# … ส่วนอื่น เช่น DocumentRoot, SSL cert …
</VirtualHost>
ทดสอบ
curl -I --http2 https://your-domain.tld/
# ถ้ามี h2 ใน response แสดงว่าใช้ HTTP/2 สำเร็จ
sudo apt update
sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d your-domain.tld -d www.your-domain.tld
ตั้งค่า HSTS ใน VirtualHost SSL
<IfModule mod_headers.c>
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
</IfModule>
sudo certbot renew --dry-run
ทำตามนี้ครบ จะช่วยให้เว็บไซต์ของคุณ โหลดเร็วขึ้น (performance) ปลอดภัยขึ้น (security) และ ถูกใจ Google ในแง่ Core Web Vitals และ HTTPS ครับ!
สนใจ ให้ช่วย คอนฟิก
📞 โทรเลย: 062-851-8111