htaccess转nginx配置
将Apache的.htaccess配置转换为Nginx配置格式
使用说明
常见转换规则
1. URL重写规则
Apache (.htaccess):
RewriteEngine On
RewriteRule ^old-page$ /new-page [R=301,L]
Nginx:
rewrite ^/old-page$ /new-page permanent;
2. 重定向规则
Apache (.htaccess):
Redirect 301 /old-url /new-url
Nginx:
return 301 /new-url;
3. 强制HTTPS
Apache (.htaccess):
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
Nginx:
if ($scheme != "https") {
return 301 https://$host$request_uri;
}
4. 禁止访问文件
Apache (.htaccess):
<Files ".htaccess">
Order allow,deny
Deny from all
</Files>
Nginx:
location ~ /\.ht {
deny all;
}
注意事项
- 此工具提供基本的转换规则,复杂配置可能需要手动调整
- 转换后的配置需要放在Nginx的server块中
- 建议在测试环境中验证转换后的配置
- 某些Apache特有的指令在Nginx中可能没有直接对应
- 转换后记得重启Nginx服务:
nginx -s reload
常用Nginx指令
rewrite: URL重写return: 返回状态码或重定向location: 匹配URL路径try_files: 尝试访问文件proxy_pass: 反向代理