将mod_重写规则转换为nginx规则

将mod_重写规则转换为nginx规则,nginx,Nginx,我正在努力从apache切换到nginx,我对nginx还不熟悉,但仍在努力找出规则。我需要将旧站点的.htaccess转换为nginx语法。我使用了一个在线语法转换器,得到了这个,但它不工作。它尝试下载*.html文件,而不是将其作为php解析并显示 .htaccess Options +FollowSymLinks -MultiViews RewriteEngine On RewriteCond %{HTTP_HOST} www.example.com [nocase] Rewrite

我正在努力从apache切换到nginx,我对nginx还不熟悉,但仍在努力找出规则。我需要将旧站点的.htaccess转换为nginx语法。我使用了一个在线语法转换器,得到了这个,但它不工作。它尝试下载*.html文件,而不是将其作为php解析并显示

.htaccess

Options +FollowSymLinks -MultiViews

RewriteEngine On

RewriteCond %{HTTP_HOST}  www.example.com [nocase]
RewriteRule ^(.*)      http://example.com/$1 [last,redirect=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+).html$ index.php?q=$1 [L,QSA]
我试图转换成nginx的代码被破坏了,我被卡住了

location / { 
if ($http_host ~ "www.example.com"){ 
rewrite ^/(.*) http://example.com/$1 redirect; } 
if (!-e $request_filename){ 
rewrite ^/([^.]+).html$ /index.php?q=$1 break; } 
}

查看
试用文件

try_files $uri $uri/ /index.php?q=$uri&$args;
它将从左到右尝试每个参数。如果找不到文件/目录,它将使用uri和查询字符串默认为/index.php

try_files $uri $uri/ /index.php?q=$uri&$args;