废话不多说,直接上配置文件:
代码语言:javascript复制server {
listen 9999;
server_name localhost;
location /docs {
proxy_pass http://127.0.0.1:8999/docs;
}
location /openapi.json {
proxy_pass http://127.0.0.1:8999/openapi.json;
}
location / {
# 设置密钥
set $key "123456";
set $iskey false;
# 如果路由参数中的key等于密钥
if ($arg_key = $key) {
set $iskey true;
}
# 如果请求头参数中的key等于密钥
if ($http_key = $key) {
set $iskey true;
}
# 上方2个条件均未满足
if ($iskey = false) {
rewrite ^ /error403 last;
}
proxy_pass http://127.0.0.1:8999;
}
location = /error403 {
internal;
add_header Content-Type 'application/json';
return 200 '{"error": "密钥错误"}';
}
}