阻止访问 Nginx 上的文件或位置

Mug*_*mba 7 nginx

我们几周前才开始运行 nginx,需要阻止对某些文件/位置的访问。例如:

/wordpress/wp-admin/
/wp-admin/
/test/wp-admin/
/hudson/login
/phpmyadmin/index.php
/mysql/index.php
/myadmin/index.php
/wp-cron.php
/xmlrpc.php
Run Code Online (Sandbox Code Playgroud)

通常,我们希望阻止除 /index.php 之外的任何文件请求以及任何位置,例如 /wp-admin/、/test/wp-admin/、/wordpress/wp-admin/ 等。这些文件/位置不'不存在,因此任何访问它们的人都试图入侵/滥用系统。

在 Apache 中,我们会.htaccess用来阻止这样的。如何在 Nginx 中阻塞?

当前会议

server {
    listen       80;
    root /home/public_html;
    index index.php;
    server_name  domain.com;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    } 


    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
    }
}
Run Code Online (Sandbox Code Playgroud)

the*_*fog 12

下面的配置应该使 nginx 以 404 状态和基本的 nginx 404 页面响应“滥用”URL;以 结尾的所有其他 URL.php应该像往常一样通过代理传递给 application/php 引擎。我已经对一些模式进行了测试,但是您应该测试要由 nginx 而不是应用程序管理的所有模式。我认为此配置可能在 URL 上存在一些问题,例如代理传递位置/phpmyadmin/index.php\.php正则表达式具有更高的优先级,但我的测试表明它有效,至少对我而言。
另外,是的,如果没有这么多位置块会更好,但我想不出你将如何实现这一点。

server {
    listen       80;
    root /home/public_html;
    index index.php;
    server_name  domain.com;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    } 


    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
    }

    # The ^~ means if this prefix pattern matches use this location block
    # and don't continue onto the regex location blocks - which would load
    # the laravel application
    location ^~ /wordpress/wp-admin/ {
        return 404;
    }
    location ^~ /wp-admin/ {
        return 404;
    }
    location ^~ /test/wp-admin/ {
        return 404;
    }
    location ^~ /hudson/login {
        return 404;
    }
    location ^~ /phpmyadmin/index.php {
        return 404;
    }
    location ^~ /mysql/index.php {
        return 404;
    }
    location ^~ /myadmin/index.php {
        return 404;
    }
    location ^~ /wp-cron.php {
        return 404;
    }
    location ^~ /xmlrpc.php {
        return 404;
    }

} 
Run Code Online (Sandbox Code Playgroud)