nginx设置仅允许访问特定路径,其他路径返回404
|
freeflydom
2026年8月3日 14:47
本文热度 156
|
:nginx设置仅允许访问特定路径,其他路径返回404

server {
listen 88;
server_name test.com;
# 正则匹配任意目录下的 test.ashx
location /test.ashx {
proxy_pass http://192.168.0.199;
}
# 其他所有请求拒绝
location / {
return 404;
}
}
location /test.ashx 是前缀匹配,只会匹配 URI 以 /test.ashx 开头的请求。
例如:
如果您希望匹配任意路径下的该文件名(如 /menu/submenu/... 或 /api/...),请改用正则匹配:
location ~ /test\.ashx$ {
proxy_pass http://192.168.0.199;
}若需要不区分大小写,可用 ~*。
该文章在 2026/8/3 14:52:04 编辑过