我是 nginx 新手,我不知道如何在 nginx 中使用 .htaccess 以及将生成的代码放在 nginx 配置中的何处。有人可以帮我将其转换为 nginx 吗?
这是我的 .htaccess 文件
<IfModule mod_rewrite.c>
RewriteCond ${REQUEST_URI} ^.+$
RewriteCond %{REQUEST_FILENAME} \.(admin)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteCond %{QUERY_STRING} [^a-z](cast|char|convert|declare|delete|drop|exec|insert|meta|script|select|set|truncate|update)[^a-z] [NC]
RewriteRule (.*) - [F]
RewriteEngine On
# redirect with username
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(admin|assets|include)(.*)?$ /$1$2 [L,QSA,R=301]
RewriteRule ^user/([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^user/([a-zA-Z0-9_-]+)/$ user.php?username=$1
RewriteRule ^media/([a-zA-Z0-9_-]+)$ media.php?id=$1
RewriteRule ^media/([a-zA-Z0-9_-]+)/$ media.php?id=$1
RewriteRule ^tag/([^/.]+)$ tag.php?tag=$1 [QSA,L]
RewriteRule ^tag/([^/.]+)/$ tag.php?tag=$1 [QSA,L]
# turn on the mod_rewrite engine
RewriteCond %{REQUEST_FILENAME}.php -f
# IF the request filename with .php extension is a file which exists
RewriteCond %{REQUEST_URI} !/$
# AND the request is not for a directory
RewriteRule (.*) $1\.php [L]
# redirect to the php script with the requested filename
</IfModule>
请帮助我将其转换为 nginx,并指导我准确地将这些代码放在哪里。谢谢!
Nginx 不使用 htaccess 文件;你必须在 Nginx 站点配置中指定规则,比如
/etc/nginx/sites-enabled/my-site.com.conf
。你可以使用像 this 这样的服务将现有的 htaccess 文件转换为 Nginx conf,但转换后你必须重新检查是否有任何错误。
下面是从上述服务转换而来的conf,以了解所需的配置更改,但可能会有一些与您当前的 nginx 配置相关的更改。
location ~ \.(admin)$ {
}
location ~ /$ {
}
location / {
if ($query_string ~* "[^a-z](cast|char|convert|declare|delete|drop|exec|insert|meta|script|select|set|truncate|update)[^a-z]"){
return 403;
}
if (!-e $request_filename){
rewrite ^/(admin|assets|include)(.*)?$ /$1$2 redirect;
}
rewrite ^(.*)$ /$1\.php break;
}
location /user {
rewrite ^/user/([a-zA-Z0-9_-]+)$ /user.php?username=$1;
rewrite ^/user/([a-zA-Z0-9_-]+)/$ /user.php?username=$1;
}
location /media {
rewrite ^/media/([a-zA-Z0-9_-]+)$ /media.php?id=$1;
rewrite ^/media/([a-zA-Z0-9_-]+)/$ /media.php?id=$1;
}
location /tag {
rewrite ^/tag/([^/.]+)$ /tag.php?tag=$1 break;
rewrite ^/tag/([^/.]+)/$ /tag.php?tag=$1 break;
}