How to deny with 404 on nginx

你。 提交于 2019-12-03 13:31:01
ARGB32
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;
}

The 'internal' returns 404 by default.

Adapted from this answer here

A more elegant way is to create a custom error page. In that page instead of showing http error code, you can specify a custom message.

Name an 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>

place this 40x.html in your path/to/public directory

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 into the root of your domain (eg _http://127.0.0.1/404.php) or change "path/to/file/404.php?id=$1"

return 404; will do what you want . Look bellow

    location /admin/ {
        uwsgi_pass myupstream1;
        include /path/to/uwsgi_params;

        allow 127.0.0.1;
        deny all;
        #Following line returns 404
        return 404;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!