将 IIS 重写转换为 Nginx 重写语法?
Convert IIS rewrite to Nginx rewrite syntax?
我想将 IIS 重写转换为 Nginx 重写语法。我正在寻找一种将 /leaderboard
之类的 URL 重写为 /pages.php?page=leaderboard
的方法。我的 IIS 重写规则是:
<match url="^([^/]+)/?$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="pages.php?page={R:1}" />
当前的 NGiNX 会议:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 302 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
ssl_certificate /etc/ssl/cert.pem;
ssl_certificate_key /etc/ssl/key.pem;
server_name example.com www.example.com;
root /var/www/example.com/;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server {
rewrite ^/([^/]+)/?$ /pages.php?page= break;
...
}
匹配 (see tests):
❌ /
✅ /foo
✅ /foo/
❌ /foo/bar
❌ /foo/bar/baz
我想将 IIS 重写转换为 Nginx 重写语法。我正在寻找一种将 /leaderboard
之类的 URL 重写为 /pages.php?page=leaderboard
的方法。我的 IIS 重写规则是:
<match url="^([^/]+)/?$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="pages.php?page={R:1}" />
当前的 NGiNX 会议:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 302 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
ssl_certificate /etc/ssl/cert.pem;
ssl_certificate_key /etc/ssl/key.pem;
server_name example.com www.example.com;
root /var/www/example.com/;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server {
rewrite ^/([^/]+)/?$ /pages.php?page= break;
...
}
匹配 (see tests):
❌ /
✅ /foo
✅ /foo/
❌ /foo/bar
❌ /foo/bar/baz