限制访问某个 URL 前缀
Restrict access to a certain URL prefix
我想阻止对我服务器中所有 URL 的访问,但以 /myapp/path
开头的 URL 除外,/myapp/path/nope
也应被阻止。
我试过了:
nginx.org/server-snippets: |
location = /myapp/path/nope { return 404; }
location ^~ /myapp/path {}
location / { return 404; }
但是在以 /myapp/path
开头的 URL 上也收到了 404 条消息。坦率地说,即使在阅读了文档并尝试了各种尝试之后,我似乎还没有弄清楚 nginx 如何确定服务的位置。我的代码片段有什么问题?谢谢!
最终我使用负正则表达式解决了这个问题。
在我的问题中,我错误地使用了位置和正则表达式,因为我从未告诉 nginx 如何处理我写的路径。
因此,为了限制对任何不以 /myapp/path
开头的内容的访问,请使用:
location ~ ^/(?!myapp/path) { return 404; } # Or deny all;
我想阻止对我服务器中所有 URL 的访问,但以 /myapp/path
开头的 URL 除外,/myapp/path/nope
也应被阻止。
我试过了:
nginx.org/server-snippets: |
location = /myapp/path/nope { return 404; }
location ^~ /myapp/path {}
location / { return 404; }
但是在以 /myapp/path
开头的 URL 上也收到了 404 条消息。坦率地说,即使在阅读了文档并尝试了各种尝试之后,我似乎还没有弄清楚 nginx 如何确定服务的位置。我的代码片段有什么问题?谢谢!
最终我使用负正则表达式解决了这个问题。 在我的问题中,我错误地使用了位置和正则表达式,因为我从未告诉 nginx 如何处理我写的路径。
因此,为了限制对任何不以 /myapp/path
开头的内容的访问,请使用:
location ~ ^/(?!myapp/path) { return 404; } # Or deny all;