基于正则表达式的NGINX别名路径

基于正则表达式的NGINX别名路径,nginx,Nginx,我正在尝试添加一个新的位置块,其中包含一个别名指令,该指令基于一个动态URI来访问不同的API。现在我可以手动添加每个位置块,但是我想知道是否可以使用REGEX映射它 问题是它返回了404错误。我正在服务器上的另一个文件夹中运行laravel子应用程序 有什么线索吗 ** 手动工作 location /api/product { alias /path/to/api/product/public; try_files $uri $uri/ @rewrite; locat

我正在尝试添加一个新的位置块,其中包含一个别名指令,该指令基于一个动态URI来访问不同的API。现在我可以手动添加每个位置块,但是我想知道是否可以使用REGEX映射它

问题是它返回了404错误。我正在服务器上的另一个文件夹中运行laravel子应用程序

有什么线索吗

**

手动工作

location /api/product {
    alias /path/to/api/product/public;
    try_files $uri $uri/ @rewrite;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    }
}

location @rewrite {
    rewrite /api/product/(.*)$ /api/product/index.php?/$1 last;
}
错误404/未指定输入文件

location ~ /api/(.*) {
    alias /path/to/api/$1/public;
    try_files $uri $uri/ @rewrite;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    }
}

location @rewrite {
    rewrite /api/(.*)/(.*)$ /api/$1/index.php?/$2 last;
}
更多测试

A

URL:app.tdl/api/tweets

Result:'Index of/api/tweets/'

$request\u文件名的输出:/home/vagrant/code/Gateway/modules/tweets/app

location /api/tweets {
  alias /home/vagrant/code/Gateway/modules/tweets/app;
  autoindex on;
}
location ~ "/api/(?<apiName>[^/]+)" {
  alias "/home/vagrant/code/Gateway/modules/$apiName/app" ;
  autoindex on;
}
B

URL:app.tdl/api/tweets

Result:Nginx的404

$apiName:tweets的输出

$request\u文件名的输出:/home/vagrant/code/Gateway/modules/tweets/app

location /api/tweets {
  alias /home/vagrant/code/Gateway/modules/tweets/app;
  autoindex on;
}
location ~ "/api/(?<apiName>[^/]+)" {
  alias "/home/vagrant/code/Gateway/modules/$apiName/app" ;
  autoindex on;
}
location~“/api/(?[^/]+)”{
别名“/home/vagrant/code/Gateway/modules/$apiName/app”;
自动索引;
}

别名
在正则表达式中
位置
要求捕获文件的完整路径。有关详细信息,请参阅

另外,您现有的捕获过于贪婪

使用
try_files
alias
时可能会遇到问题,因为您可能希望用
if
块替换它

例如:

location ~ ^/api/(?<product>[^/]+)/(?<filename>.*)$ {
    alias /path/to/api/$product/public/$filename;

    if (!-e $request_filename) { 
        rewrite ^ /api/$product/index.php?/$filename last;
    }

    location ~ \.php$ {
        if (!-f $request_filename) { return 404; }
        ...
    }
}
location~^/api/(?[^/]+)/(?.*)${
别名/path/to/api/$product/public/$filename;
如果(!-e$request_filename){
重写^/api/$product/index.php?/$filename last;
}
位置~\.php${
如果(!-f$request_filename){return 404;}
...
}
}
正则表达式
位置
块按顺序计算。此块需要放置在任何冲突的正则表达式
位置
块之上,例如
服务器
块级别的另一个
位置~\.php$


第二个
if
块是为了避免。请参阅关于使用
if

的说明,谢谢,Richard。这是有道理的,我认为它应该工作,但仍然得到“没有指定输入文件”的消息。也许重写规则可能遗漏了什么?我不确定
rewrite
语句是否正确-哪里是
index.php
?在
public
目录内或其上方?每个API都有自己的/absolute/path/to/apiName/public/index.php(Laravel应用程序)。我已经检查过,$product和$filename按预期工作。重写语句应该负责404。我试过这个:
rewrite/api/$product/$filename/api/$product/index.php?/$filename last/public/
不应出现在重写的URI中。
rewrite
语句的第一个参数需要是正则表达式,我使用
^
,因为它将匹配任何内容。在上面,将在
/path/to/api//public/index.php
中搜索URI
/api//index.php