.htaccess sang nginx / Caddy & kiểm tra nginx API
Chuyển .htaccess của Apache thành khối server nginx và Caddyfile, lint nginx.conf tìm lỗi thường gặp, sinh cấu hình WordPress, Laravel, SPA, proxy.
convert (mặc định) chuyển file .htaccess thành khối server của nginx và Caddyfile: mod_rewrite (RewriteCond / RewriteRule với cờ L, R=301, NC, QSA, QSD, F, G, OR, RewriteBase, tham chiếu %N), mẫu front controller (→ try_files), chuyển hướng HTTPS và www, Redirect / RedirectMatch, ErrorDocument, DirectoryIndex, Options, Header, mod_expires (→ map theo content type), mod_deflate, Deny / Allow / Require, <Files> / <FilesMatch>, basic auth, LimitRequestBody, ServerSignature. Mỗi chỉ thị có confidence (high / medium / low / none) và ghi chú; cấu hình PHP (php_value, php_flag) được chuyển thành đoạn cấu hình pool PHP-FPM. Kết quả qua được nginx -t với các chỉ thị được hỗ trợ.
lint kiểm tra tĩnh cấu hình nginx: thiếu ngoặc, thiếu dấu chấm phẩy, if trong location ("if is evil"), lỗ hổng alias path traversal (location không có / + alias có /), add_header ở khối con làm mất header của khối cha, proxy_pass có URI trong location regex, biến không có resolver, giao thức TLS cũ và cipher yếu, listen 443 thiếu ssl, cú pháp listen … http2 đã lỗi thời, CRLF injection qua $uri trong redirect, location PHP không neo $ / thiếu try_files, autoindex, server_name trùng, thiếu HSTS / header bảo mật và server_tokens.
generate sinh cấu hình nginx và Caddy cho site static, spa, wordpress, laravel, php hoặc reverse-proxy: chuyển HTTP → HTTPS, chuyển www / tên miền gốc, đường dẫn chứng chỉ Let’s Encrypt, gzip (brotli tuỳ chọn), cache file tĩnh và header bảo mật. Hãy chạy nginx -t hoặc caddy validate trước khi áp dụng.
Markdown · OpenAPI · Postman · llms.txt
Tham số
| Tên | Vị trí | Kiểu | Bắt buộc | Mặc định | |
|---|---|---|---|---|---|
action | body|query | string | không | convert | convert, lint hoặc generate (tên khác mode; cũng tự suy ra từ trường được gửi). |
htaccess | body | string | không | convert: nội dung .htaccess (tối đa 300 KB). | |
nginx | body | string | không | lint: nội dung cấu hình nginx (tối đa 1 MB). | |
server_name | body|query | string | không | example.com | convert: tên server cho khối sinh ra. |
root | body|query | string | không | Thư mục gốc (convert mặc định /var/www/html, generate /var/www/<domain>). | |
php_fpm | body|query | string | không | unix:/run/php/php8.3-fpm.sock | Địa chỉ PHP-FPM. |
preset | body|query | string | không | generate: static, spa, wordpress, laravel, php, reverse-proxy. | |
domain | body|query | string | không | generate: tên miền, ví dụ example.vn. | |
www | body|query | string | không | apex | generate: apex (www → tên miền gốc), www (gốc → www) hoặc none. |
upstream | body|query | string | không | http://127.0.0.1:3000 | generate reverse-proxy: địa chỉ backend. |
https | body|query | boolean | không | true | generate: dùng HTTPS kèm chuyển hướng. |
brotli | body|query | boolean | không | false | generate: nén brotli (cần module ngx_brotli). |
server | body|query | string | không | both | generate: nginx, caddy hoặc both. |
Ví dụ
curl -X POST "https://apibox.io.vn/v1/webserver-config" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"htaccess":"RewriteEngine On\nRewriteCond %{HTTPS} off\nRewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]\nRewriteRule ^old-page$ /new-page [R=301,L]\nRewriteCond %{REQUEST_FILENAME} !-f\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteRule . /index.php [L]\nphp_value upload_max_filesize 64M\n","server_name":"example.com","root":"/var/www/example"}'const res = await fetch('https://apibox.io.vn/v1/webserver-config', {
method: 'POST',
headers: {
Authorization: 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json',
},
body: JSON.stringify({
"htaccess": "RewriteEngine On\nRewriteCond %{HTTPS} off\nRewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]\nRewriteRule ^old-page$ /new-page [R=301,L]\nRewriteCond %{REQUEST_FILENAME} !-f\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteRule . /index.php [L]\nphp_value upload_max_filesize 64M\n",
"server_name": "example.com",
"root": "/var/www/example"
}),
});
const data = await res.json();
console.log(data);import requests
res = requests.post(
"https://apibox.io.vn/v1/webserver-config",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={"htaccess": "RewriteEngine On\nRewriteCond %{HTTPS} off\nRewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]\nRewriteRule ^old-page$ /new-page [R=301,L]\nRewriteCond %{REQUEST_FILENAME} !-f\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteRule . /index.php [L]\nphp_value upload_max_filesize 64M\n", "server_name": "example.com", "root": "/var/www/example"},
timeout=30,
)
print(res.json())<?php
$ch = curl_init('https://apibox.io.vn/v1/webserver-config');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => ['Authorization: Bearer YOUR_API_KEY', 'Content-Type: application/json'],
CURLOPT_POSTFIELDS => '{"htaccess":"RewriteEngine On\\nRewriteCond %{HTTPS} off\\nRewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]\\nRewriteRule ^old-page$ /new-page [R=301,L]\\nRewriteCond %{REQUEST_FILENAME} !-f\\nRewriteCond %{REQUEST_FILENAME} !-d\\nRewriteRule . /index.php [L]\\nphp_value upload_max_filesize 64M\\n","server_name":"example.com","root":"/var/www/example"}',
]);
$response = curl_exec($ch);
print_r(json_decode($response, true));Kết quả trả về
{
"action": "convert",
"nginx": "server {\n listen 80;\n listen [::]:80;\n server_name _;\n return 301 https://$host$request_uri;\n}\n\nserver {\n listen 443 ssl;\n listen [::]:443 ssl;\n http2 on;\n # ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;\n # ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;\n server_name example.com;\n root /var/www/example;\n index index.php index.html;\n\n rewrite ^/old-page$ /new-page permanent;\n location / {\n try_files $uri $uri/ /index.php?$args;\n }\n\n location ~ \\.php$ {\n try_files $uri =404;\n fastcgi_split_path_info ^(.+\\.php)(/.+)$;\n fastcgi_pass unix:/run/php/php8.3-fpm.sock;\n fastcgi_index index.php;\n include fastcgi_params;\n fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\n }\n\n location ~ /\\.(?!well-known) {\n deny all;\n }\n}\n",
"caddy": "example.com {\n root * /var/www/example\n @r1 {\n path_regexp r1 ^/old-page$\n }\n redir @r1 /new-page 301\n try_files {path} {path}/ /index.php?{query}\n php_fastcgi unix//run/php/php8.3-fpm.sock\n file_server\n}\n",
"php_fpm_pool": "php_value[upload_max_filesize] = 64M",
"items": [
{
"line": 2,
"directive": "RewriteCond %{HTTPS} off\nRewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]",
"nginx": "server { listen 80; return 301 https://$host$request_uri; }",
"caddy": "(automatic: Caddy redirects HTTP to HTTPS by itself)",
"confidence": "high",
"note": "HTTPS redirect moved to a separate port-80 server block"
},
{
"line": 5,
"directive": "RewriteCond %{REQUEST_FILENAME} !-f\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteRule . /index.php [L]",
"nginx": "location / { try_files $uri $uri/ /index.php?$args; }",
"caddy": "try_files {path} {path}/ /index.php?{query}",
"confidence": "high",
"note": "front controller (WordPress / Laravel / most PHP apps): use try_files instead of if (!-f)"
},
"…"
],
"confidence": {
"high": 4,
"none": 1
},
"parse_errors": [],
"note": "Test the result (nginx -t, caddy validate) before going live: complex mod_rewrite rules are marked low confidence."
}
Ứng dụng
- Chuyển site WordPress / Laravel từ hosting cPanel (Apache) sang VPS chạy nginx
- Chuyển luật rewrite cũ khi đổi sang Caddy
- Rà soát cấu hình nginx tìm lỗi alias traversal và mất header bảo mật
- Sinh cấu hình nginx hoặc Caddy an toàn cho tên miền mới
Hỏi đáp
Mọi luật mod_rewrite đều chuyển được?
Các mẫu phổ biến được chuyển với độ tin cậy cao; chuỗi phức tạp (nhiều điều kiện, [P], [S], [C]) được đánh dấu low kèm ghi chú. Luôn chạy nginx -t và thử các URL quan trọng.
Dòng php_value đi đâu?
Không vào nginx: kết quả có các dòng php_value / php_flag cho pool PHP-FPM (hoặc đặt trong php.ini / .user.ini).
Lint có đọc file include không?
Không: hãy gửi toàn bộ nội dung cấu hình. Đây là kiểm tra tĩnh, không phải nginx -t.
API liên quan
Tạo UUID
Sinh tối đa 1.000 UUID v4, UUID v7 theo thời gian, ULID, Nano ID hoặc mã hex ngẫu nhiên mỗi request. Ngẫu nhiên an toàn, hỗ trợ chữ in hoa.
⏱Phân tích biểu thức cron
Kiểm tra biểu thức cron và xem các lần chạy tiếp theo theo múi giờ bất kỳ, kèm mô tả dễ hiểu. Hỗ trợ cú pháp cron 5, 6 và 7 trường.
🕒Giờ & múi giờ
Lấy giờ hiện tại ở mọi múi giờ, đổi giữa các múi giờ và Unix timestamp (giây hoặc mili giây), kèm độ lệch UTC và thứ trong tuần cho tối đa 30 múi giờ.