I have a domain like sub.example.com. I'd like to allow GET access to certain paths uploads only through example.com

Something like this:

  location ~ ^/uploads {
         allow example.com;
         deny  all;
        root /www/uploads;

But nginx gives error for domain after allow. It only accepts IP address and I don't want to hardcode IP. There should be a better way?

How can I achieve this in nginx configs?

listen 80;
listen 443;

#put your certs here if you have, if not, just listen to port 80.

server_name example.com; #only requests coming from this host will enter this server bracket 

 location ~ ^/uploads {
     root /www/uploads;

Set a server block to listen to a certain domain, and accept /uploads and deny them in the other server blocks(same location block but returning a 401 or 403).

If you want to treat different domains differently, it's good in nginx to separate the server blocks.

| improve this answer | |
  • Well I already have server_name sub.example.com; configured on this server. – supermario Jan 15 '19 at 13:43
  • So set another one for example.com. In the sub.example.com you return 403; in uploads, and in example.com block you root /www/uploads. Everything else the same. Like this both domain and subdomain will do the same, except when they ask for /uploads (one will return error, the other will return upload). If you edit your post with an aproximate configuration of your sub.domain.com server block, I can aproximate my answer as much as I can on what you want. – flaixman Jan 15 '19 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.