deez
deez

Reputation: 1704

nginx backend returns 404, when using intercept directive it returns 403

I have nginx configuration with the following locations:

location /some/path- {
  limit_except GET { deny all; }
  proxy_pass            http://my_app;
}

location / {
  deny all;
}

I allowed only GET/HEAD requests to /some/path-. I need all requests to /some/path- that are not valid paths in the backend to return the 404.html of the nginx and not the backend.

Good location: http://some/path-exists

Bad location: http://some/path-doesnt-exist

When I try to add:

proxy_intercept_errors on;

and

error_page 404 /404.html;

When browsing to the Bad location I seem to get 403 Forbidden instead of the nginx 404.html page. I don't understand why.

Upvotes: 0

Views: 881

Answers (1)

Timo Stark
Timo Stark

Reputation: 3071

Based on your configuration this is normal behavior. NGINX finds the best matching location block for an incoming request. In your case:

/some/path-sfsdafhsdaiufasdf` -> Match /some/path-
/some/path/sadfasdf           -> Match /

But your fallback location / has a deny all. That's why you see the 403 forbidden. This has nothing to do with proxy_intercept_errors on.

Try this

server {                                        
  listen 8001;
  # Convert all 403 to 404 and send it to the internal 404-location.                                  
  error_page 403 =404 /@404;                    
  location /some/path- {                         
    limit_except GET { deny all; }              
    proxy_pass http://mybackend;           
  }                                             
                                                
  location / {                                  
    deny all;                                   
  }                                             
                                                
  location /@404 {                              
   return 404 "Ouch, that did not work well!\n";   
  }                                             
                                                
}                                               

Upvotes: 0

Related Questions