gpt4 book ai didi

Nginx 只允许 root 和 api 位置

转载 作者:行者123 更新时间:2023-12-04 12:04:58 30 4
gpt4 key购买 nike

我有一台服务器配置为我的服务器的反向代理。我想拒绝除两个位置之外的所有请求,一个用于 root,另一个用于 api root。

所以服务器应该只允许对给定路径的请求

example.com/ (only the root)
example.com/api/ (every url after the api root)

预期的行为是服务器应该拒绝以下所有可能性。
example.com/location
example.com/location/sublocation
example.com/dynamic-location

我目前的 nginx 配置,
server {

# server configurations

location / {

# reverse proxy configurations

}

}

如何设置此配置?

最佳答案

这里是:

   location = / {
# would serve only the root
# ...
}

location /api/ {
# would serve everything after the /api/
# ...
}

您需要一个特殊的 '=' 修饰符才能使根位置按预期工作

来自 docs :

Using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates. For example, if a “/” request happens frequently, defining “location = /” will speed up the processing of these requests, as search terminates right after the first comparison.

关于Nginx 只允许 root 和 api 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48702676/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com