-1

How to configure nginx to deny all browser requests except from specific IPs?

The nginx must serve only non browser requests and browser requests from specific IPs

closed as off-topic by Andrew Schulman, masegaloeh, Massimo, Hyppy, mdpc May 8 '15 at 0:32

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Downvoting for not showing that you've tried to figure this out on your own. – Andrew Schulman May 7 '15 at 6:25
  • 1
    Strange idea. What the reason? What have you tried? How will you distinguish non-browser from browser? – Alexey Ten May 7 '15 at 7:09
  • i have tried checking "http_user_agent" check it was for working fine – user3323241 May 7 '15 at 15:11
1
map $remote_addr $whitelist {
    default                     1;
    ~^(192.168.123.*)$          0;
}

[...]

location / {
    if ($whitelist) { return 404; }
}

:: with in map, if $remote_addr matches the second line the variable $whitelist will become 0, else 1

:: with in location, if the variable $whitelist equals 1 or higher the client will be denied access with a 404 (in this case it is 1 when no match is made with the example ip wildcard or zero when it does allowing access)

For doing this with user agents:

map $http_user_agent $whitelist {
    default              0;
    ~*Unknown.*Client    1;
}

:: note the reversed result values (0 and 1)

  • 2
    This would be a better answer if you explained what is going on. – Michael Hampton May 7 '15 at 22:39
  • My configuration: location / { set $agent 0; #Allowing specific IP set deny **.*.**.***; allow **.**.**.***; deny all; #Non browser hit check if ($http_user_agent !~* "Mozilla|Chrome") { root /usr/local/AdminUI/; set $agent 1; } if ($agent = 0) { return 403; } Individually this checks are working fine,But nginx is not allowing to try allow <ipaddress> inside if block or two root /usr/local/AdminUI/; inside "Location" – user3323241 May 18 '15 at 9:38
  • i want to allow all hits from mobile app (non browser) which will not have any http_user_agent – user3323241 May 18 '15 at 9:42

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