How to deny from 404 to nginx

I want to protect some locations in nginx by providing deny / allow directives, but I do not want outsiders to know that this place is denied. I want an outsider to get 404, not 403 http code. My configuration snippet

location /admin/ { uwsgi_pass myupstream1; include /path/to/uwsgi_params; allow 127.0.0.1; deny all; } 

When I try to visit / admin / nginx, HTTP 403 answers, but I want it to respond HTTP 404. Any recipe for this?

+7
source share
4 answers
 error_page 403 404 /404.html; location = /404.html { internal; #return 404 } location /admin/ { uwsgi_pass myupstream1; include /path/to/uwsgi_params; allow 127.0.0.1; deny all; } 

By default, 'internal' returns 404.

Adapted from this answer here

+5
source share

A more elegant way is to create a custom error page. On this page, instead of displaying an HTTP error code, you can specify your own message.

Name the error page

 error_page 403 =404 /40X.html; location /admin/ { uwsgi_pass myupstream1; include /path/to/uwsgi_params; allow 127.0.0.1; deny all; } location /40X.html { root path/to/public; } 

In your 40x.html you can write any message

 <html> <body> The requested resource is not available </body> </html> 

put this 40x.html in your path / in / public directory

+2
source share

You can use rewrite instead of deny all

 location /admin/ { uwsgi_pass myupstream1; include /path/to/uwsgi_params; #allow 127.0.0.1; #deny all; rewrite ^/admin/(.*)$ /404.php?id=$1; } 

Put 404.php in the root of your domain (for example, _http: //127.0.0.1/404.php) or change "path / to / file / 404.php? Id = $ 1"

-one
source share

return 404; will do what you want. Take a look below.

  location /admin/ { uwsgi_pass myupstream1; include /path/to/uwsgi_params; allow 127.0.0.1; deny all; #Following line returns 404 return 404; } 
-one
source share

All Articles