Return 404 with nginx to all locations except for one

可紊 提交于 2021-01-28 03:00:42

问题


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

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.


回答1:


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; }


来源:https://stackoverflow.com/questions/17636503/return-404-with-nginx-to-all-locations-except-for-one

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!