Score:0

NGINX - allow access to specific URL only via a subdomain

us flag

I have a web page, located on example.com/app.

Is there a way to deny direct access to example.com/app and allow only via app.example.com by means of NGINX?

us flag
Please share the output of `nginx -T` so that we can see your full nginx configuration and give a proper answer.
Score:1
cn flag

In your example.com server block, location ~ app { deny all; } (or similar):

server {
    server_name example.com;
    location ~ api { deny all; }

<configuration for example.com>
}

server {
    server_name api.example.com;

<configuration for api.example.com>
}
Tarlan Mammadzada avatar
us flag
But will I access example.com/api via api.example.com?
Paul avatar
cn flag
As long as `api.example.com` is in a different `server` block, yes.
Tarlan Mammadzada avatar
us flag
Could you please add in your answer a server block for `api.example.com` which accesses `example.com/api`?
Paul avatar
cn flag
The subdomain and domain are simply in different `server` blocks. They are not accessing in the same way a browser does. The access is to the directories and files on the server. By configuring separate `server` blocks for the domain and the subdomain, access to files is easier to manage.
Tarlan Mammadzada avatar
us flag
In your example I have to redirect `api.example.com` to `example.com/api`, because I don't have a folder named `app` to set in the root of `server`.
Paul avatar
cn flag
The `location` is what is requested to `nginx` by a client. With the `deny all;` directive, `nginx` will not look for the matched `location`. As far as what is actually in the server file structure is another story, because that is not what is being evaluated in this case. Even if there is no file or directory called `api`, `nginx` will still respond to the client with 403.
sv flag
@TarlanMammadzada Please update your question by posting your existing configuration and `curl` output of what's happening and what you are looking for. This answer gives the exact answer that you asked for in your OP. If you have a different or additional questions, post it in your OP or create a new question so that everyone knows what you are trying to achieve.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.