Nginx - Allowing origin IP

I got it working with the following configuration, using the http_geo module:

geo $remote_addr $give_access {
    proxy; # <-- Private IP range here
    default 0; 1; # <-- Allowed IP here

server {

    # more config ... 

    location ^~ /secure_url_here {
        if ($give_access = 0) {
          return 403; 
        try_files $uri $uri/ /index.php?$args; # <-- Your directive here


remote_addr will refer to the proxy, but you can configure the proxy to send the client address with header fields X-Real-IP/X-Forwarded-For.

Combined with the ngx_http_realip module, you can modify the incoming header to use the real client address for remote_addr. I believe this will work as expected with allow/deny syntax.

Just to clarify -- allow/deny syntax should be identical after enabling and configuring the module. Substitute your IP and your proxy addresses below.

Back-end nginx allow/deny:

location / {
    allow <your ip>;
    deny  all;

Back-end nginx realip configuration:

set_real_ip_from  <your proxy>;
real_ip_header    X-Forwarded-For;

On your nginx proxy configuration:

proxy_set_header        X-Real-IP       $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

If you have multiple intermediate proxies involved, you'll need to enable real_ip_recursive and whitelist additional addresses with the set_real_ip_from directive.