I have static error pages set up for my server, but I want to ignore those for urls starting with /api/
so the handler there can respond with a non-200 code, but not return the static error page.
Here is the existing config:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com;
root /var/www/html/;
index index.php;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_intercept_errors on;
fastcgi_index app.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_read_timeout 600;
}
error_page 404 /error/404.html;
error_page 500 502 503 504 /error/50x.html;
location ^~ /error/ {
root /usr/share/nginx/html/;
}
}
So I think I want to add a new location like:
location /api/ {
}
But I'm not sure what to use to tell it to not follow the server's error_page rule.
Best Answer
Alright, I was struggling with the same thing, but managed to find the (a?) solution.
On the highest level I've got:
/etc/nginx/boilerplate/fancyerror_and_intercept.conf
contains:In order to exclude the
/api/
route (Location starts with/api/
and can contain anything after), add the following:In essence the
/api/
location is a duplicate of the general location, but with error_page redefined and intercept errors set to off.This works, because as Tim stated earlier, the
error_page
directive resets any predefined error_pages. I've chosen error code527
, because we don't use CloudFlare see Wikipedia, List of HTTP status codes#Cloudflare. Because weproxy_pass
our requests, you mileage may vary and you might have to change theproxy_intercept_errors off;
tofastcgi_intercept_errors off;
.