Brian
Brian

Reputation: 385

Return 404 with nginx to all locations except for one

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.

Upvotes: 2

Views: 6923

Answers (1)

Mohammad AbuShady
Mohammad AbuShady

Reputation: 42799

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

Upvotes: 1

Related Questions