将 IIS 重写转换为 Nginx 重写语法?
Posted
技术标签:
【中文标题】将 IIS 重写转换为 Nginx 重写语法?【英文标题】:Convert IIS rewrite to Nginx rewrite syntax? 【发布时间】:2022-01-17 06:20:12 【问题描述】:我想将 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;
【问题讨论】:
当用户导航到example.com/leaderboard
时应该显示什么文件?
那就是 pages.php?page=leaderboard
谢谢。我反转并更正了原始问题中的源/目标 URL。
【参考方案1】:
server
rewrite ^/([^/]+)/?$ /pages.php?page=$1 break;
...
匹配项 (see tests):
❌ /
✅ /foo
✅ /foo/
❌ /foo/bar
❌ /foo/bar/baz
【讨论】:
以上是关于将 IIS 重写转换为 Nginx 重写语法?的主要内容,如果未能解决你的问题,请参考以下文章