简体   繁体   中英

Nginx custom error page and geoip block.

I am trying to display custom error page if error found (eg 404, 403..etc) in nginx. The 404 error pages shown when I access /test with following configuration.

error_page 403 /403.html;
error_page 404 /404.html;

location = /403.html {
  root /usr/share/nginx/html/errors;
  allow all;
}

location = /404.html {
  root /usr/share/nginx/html/errors;
  allow all;
}

# Everything is a 404
location /test {
  return 404; #return the code 404
}

After that, I am trying to add conditional checking based on geoip module. If $deny_request equal to 1, nginx will return 403 directly.

# Geoip checking to set deny request value. 
if ($blocked_country) {
  set $deny_request 1;
}

if ($deny_request = 1) {
  return 403;
}

http status 403 is returned as expected. However, instead of the custom error page, it return nginx default 403 error page. It seems not possible to put the checking to server block with custom error page. Am I missing anything?

Same problem drove me crazy for 2 weeks or more, bytheway I've been found a working solution, it's the follow:

# - error
error_page 502 @offline;
location @offline {
    root /var/web/example.org/www;
    try_files /502.html 502;
} 

I found the solution in this answer Nginx won't use custom 502 error page although I don't understand why your solution doesn't work, it should, I think.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM