Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages

Servers, nginx and Docker · Troubleshooting walkthrough

An Apache IP allow rule built on SetEnvIf Remote_Addr has no effect

Short answer

SetEnvIf Remote_Addr tests the address of whatever opened the TCP connection to Apache. Behind nginx, a CDN or a load balancer that is the proxy, so a visitor IP pattern never matches. On Apache 2.4 load mod_remoteip with RemoteIPHeader and a trusted proxy list, then write the rule as Require ip, without mixing in Order, Allow or Deny.

  • Applies to Apache httpd 2.4.71, Ubuntu 24.04 apache2 package (2.4.58)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

SetEnvIf Remote_Addr tests the address of whatever opened the TCP connection to Apache. Behind nginx, a CDN or a load balancer, that is the proxy, so a pattern written for a visitor's IP never matches and the allow or block rule does nothing.

A second, separate trap sits next to it: mixing the old Order, Allow and Deny directives with Require. Rule out the proxy first, then the mixed syntax.

The text you see

  • AH01797: client denied by server configuration
  • 403 Forbidden

Stage 1

Compare the logged address with the visitor's

Request a page from the address you want to allow, then read the newest access log lines and the rules for that folder. If every logged client address is 127.0.0.1, a private range or a CDN range, Apache only sees the proxy.

Logged client address and the rules in force

sudo tail -n 5 /var/log/apache2/access.log
grep -nE "SetEnvIf|Order|Allow from|Deny from|Require" /var/www/html/.htaccess

Stage 2

Hand Apache the real visitor address with mod_remoteip

mod_setenvif defines Remote_Addr as the address of the client making the request. With a proxy in front, the visitor's own IP arrives only inside a header such as X-Forwarded-For. mod_remoteip reads that header and replaces the client address for the whole request, and its documentation names Require ip as a feature that then uses the replaced value. SetEnvIf reads Remote_Addr from the same replaced address, so an existing rule starts matching too.

Always name the proxy you trust. With no trusted list, any caller who sends the header can choose its own IP. RemoteIPInternalProxy suits a proxy on your private network; use RemoteIPTrustedProxy for public CDN ranges.

Trust the local proxy, then allow one address

sudo a2enmod remoteip

# inside the <VirtualHost> block
RemoteIPHeader X-Forwarded-For
RemoteIPInternalProxy 127.0.0.1

# .htaccess or <Directory>: allow one address
Require ip 203.0.113.10

Test the syntax and reload: sudo apachectl configtest && sudo systemctl reload apache2. The access log should now show the visitor's public IP.

Stage 3

Order and Allow lines fighting a Require line

SetEnvIf can set the variable correctly and the request is still refused. The Apache 2.4 upgrade guide warns that combining the old Order, Allow and Deny directives with Require gives surprising results, because in some merges the compatibility directives win. Its own example logs AH01797 for a path the configuration appears to open.

Is the compatibility module loaded, and where are the mixed rules?

sudo apachectl -M | grep access_compat
grep -rnE "Order|Allow from|Deny from|Require" /etc/apache2/sites-enabled/ /var/www/html/.htaccess

Stage 4

One rule style, with an anchored pattern

Keep the SetEnvIf line, change Allow from env= to Require env, and delete the Order, Allow and Deny lines in that folder. Escape the dots in the pattern and anchor it, so 10.0.0.1 cannot also match 10.0.0.15.

SetEnvIf with Require env

SetEnvIf Remote_Addr "^203\.0\.113\.10$" office_ip
Require env office_ip

Stage 5

A 200 from the allowed address and a 403 from any other

If the log already shows the visitor's own address and the rule still fails, check whether that visitor connects over IPv6, since an IPv4 pattern cannot match an IPv6 address.

Run it from both addresses

curl -s -o /dev/null -w "%{http_code}\n" https://example.com/admin/

If you only keep one thing

  • Remote_Addr is the connecting machine, which behind a proxy is the proxy.
  • Use mod_remoteip with a trusted proxy list, and write access rules with Require only.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Next step

Want a second pair of eyes on it?

Send me the error and what changed before it started. I reply within 24 hours.