使用 nginx 将 404 返回到除一个位置之外的所有位置 [英] Return 404 with nginx to all locations except for one

查看:71
本文介绍了使用 nginx 将 404 返回到除一个位置之外的所有位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个子域来托管我们的 API.我试图弄清楚如何配置 nginx 以只允许对 API 位置的请求(/2/,看起来像 https://api.example.com/2/) 并为 api.example.com 的所有其他请求返回 404

I've created a subdomain to host our API. I'm trying to figure out how to configure nginx to only allow requests to the API location (/2/, which would look like https://api.example.com/2/) and return 404s for all other requests to api.example.com

我们使用 PHP 和非常标准的 PHP 设置——通过 index.php 路由大多数请求并匹配 php,如下所示:

We're using PHP with a pretty standard PHP setup--routing most requests through index.php and matching php as show below:

if (!-e $request_filename) {
           rewrite ^/(.*)$ /index.php last;
        }


location ~ \.php$ { config here; }

我希望我多虑了,并且有一个简单的解决方案.

I'm hoping I'm over-thinking this and that there is a simple solution.

推荐答案

只需配置 /2 位置并将其他所有内容标记为 404

Just configure the /2 location and mark every thing else as 404

location / {
    return 404;
}
location /2 {
    try_files $uri /2/index.php$request_uri;
}
location ~ \.php$ { config here; }

这篇关于使用 nginx 将 404 返回到除一个位置之外的所有位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆