我的应用编程接口位于api.example.com/api --我怎么能否认其他一切呢?因此用户不能访问,例如,api.example.com/或api.example.com/login
不幸的是,这也拒绝了所有的API流量
location ~ /(?!api).* {
deny all;
}我目前在nginx config file中的所有位置设置
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /(?!api).* {
deny all;
}
location ~ /\.(?!well-known).* {
deny all;
}有什么建议吗?
发布于 2019-10-17 19:03:00
正则表达式不起作用的原因是它匹配URI中不跟api的任何/。所以/api和/api/api不会被拒绝,但/foo和/api/foo会被拒绝。
通过在正则表达式的开头添加一个锚点(^)可以很容易地解决这个问题:
location ~ ^/(?!api).* { ... }另一种方法是使用前缀位置:
location / {
deny all;
}
location /api {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
internal;
...
}internal指令阻止直接访问以.php结尾的URI。详情请参见this document。
https://stackoverflow.com/questions/58426445
复制相似问题