Mod rewrite nginx重写:除了空的基本url之外的所有内容

Mod rewrite nginx重写:除了空的基本url之外的所有内容,mod-rewrite,url-rewriting,nginx,rewrite,Mod Rewrite,Url Rewriting,Nginx,Rewrite,经过大约两个小时的谷歌搜索和各种尝试后,我向你求助 任务: 将空白URL重写为某物,并将其他内容改写为NGIX.中不同的东西。 因此,如果我导航到subdomain.somedomain.tld,我想得到index.php,如果我转到subdomain.somedomain.tld/BlAaA,我会被重定向到index.php?url=BlAaA。例外情况是/img、/include下的文件以及index.php本身。它们不会被重写 第二部分已经起作用了,白名单也起作用了,但我无法找出或找到一些

经过大约两个小时的谷歌搜索和各种尝试后,我向你求助

任务: 将空白URL重写为某物,并将其他内容改写为NGIX.

中不同的东西。 因此,如果我导航到subdomain.somedomain.tld,我想得到index.php,如果我转到subdomain.somedomain.tld/BlAaA,我会被重定向到index.php?url=BlAaA。例外情况是/img、/include下的文件以及index.php本身。它们不会被重写

第二部分已经起作用了,白名单也起作用了,但我无法找出或找到一些东西来完成整个想法

工作部分:

server {
  listen       80;
  server_name  subdomain.domain.tld;

  location / {
    include php.conf;
    root    /srv/http/somefolder/someotherfolder/;

    if ( $uri !~ ^/(index\.php|include|img) ){
      rewrite /(.*) /index.php?url=$1 last;
    }

    index   index.php;
  }
}
@pablo-b提供的答案几乎解决了我的问题。 这种方法只存在两个问题:1:PHP-FPM现在需要在security.limit_extensions下的/etc/PHP/PHP-FPM.conf中设置/include/(例如style.css,background.jpg)下的文件扩展名。我最初的php.conf按照

location ~ \.php {
    #DO STUFF
}
哪个nginx不喜欢,因为它有点覆盖了您建议中的location/index.php部分。不过,只要有足够的时间,我可以解决这个问题


2:$request_uri将“/whatever”而不是“whatever”作为我的url=参数的值。我当然可以在php代码中解析“/”,但我的原始解决方案没有添加前导“/”。有什么优雅的方法可以解决这个问题吗?

我建议避免使用
if
并利用与所用模式匹配方法()相关的优先级处理不同的位置:

在一个名为
公共设置
的单独配置文件中:

include php.conf;
root    /srv/http/somefolder/someotherfolder/;
index   index.php;
编辑:添加了删除url中的第一个斜杠:

在您的配置中,在任何
服务器
指令之外:

map $request_uri $uri_without_slash {
    ~^/(?P<trailing_uri>.*)$ $trailing_uri;
}
map$request\u uri$uri\u不带斜杠{
~^/(?P.*)$$trailing_uri;
}

我添加了删除
/
部分的功能,尽管它看起来不是很优雅(从性能上看,只有在使用
$uri\u而不使用斜杠时才会进行映射)。对于1),您可以将
location=/index.php
替换为
location~\.php
,但它不会重定向任何包含字符串
.php
的url。谢谢!那真是太棒了!作为其他有这个问题的ppl的参考:如果我在我的页面本身中检查了$GET['url']=='',这个问题本来可以用我原来的方法解决。但这个解决方案更干净。
map $request_uri $uri_without_slash {
    ~^/(?P<trailing_uri>.*)$ $trailing_uri;
}