.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.

GETPOSThttps://apibox.io.vn/v1/webserver-config
Tiện ích lập trình▶ Dùng thử online

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ênVị tríKiểuBắt buộcMặc định
actionbody|querystringkhôngconvertconvert, lint hoặc generate (tên khác mode; cũng tự suy ra từ trường được gửi).
htaccessbodystringkhôngconvert: nội dung .htaccess (tối đa 300 KB).
nginxbodystringkhônglint: nội dung cấu hình nginx (tối đa 1 MB).
server_namebody|querystringkhôngexample.comconvert: tên server cho khối sinh ra.
rootbody|querystringkhôngThư mục gốc (convert mặc định /var/www/html, generate /var/www/<domain>).
php_fpmbody|querystringkhôngunix:/run/php/php8.3-fpm.sockĐịa chỉ PHP-FPM.
presetbody|querystringkhônggenerate: static, spa, wordpress, laravel, php, reverse-proxy.
domainbody|querystringkhônggenerate: tên miền, ví dụ example.vn.
wwwbody|querystringkhôngapexgenerate: apex (www → tên miền gốc), www (gốc → www) hoặc none.
upstreambody|querystringkhônghttp://127.0.0.1:3000generate reverse-proxy: địa chỉ backend.
httpsbody|querybooleankhôngtruegenerate: dùng HTTPS kèm chuyển hướng.
brotlibody|querybooleankhôngfalsegenerate: nén brotli (cần module ngx_brotli).
serverbody|querystringkhôngbothgenerate: 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"}'

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

Bắt đầu với 1.000 request miễn phí mỗi tháng. Nhận API key miễn phí →