Nginx位置与deny不起作用

时间:2014-07-27 15:25:42

标签: php nginx location

我在使用nginx配置时遇到了一些问题。我想拒绝访问一些文件夹和文件,但我尝试过没有。目前我正在使用下面粘贴的配置:

server {

    listen 80;
    server_name .hostname;

    keepalive_timeout 60;

    root path/htdocs;

    access_log path/logs/access.log;
    error_log path/logs/error.log;

    index index.php index.htm index.html;

    location ~ \.php$ {
        try_files $uri =404;
        proxy_set_header Accept-Encoding "";
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  php5-fpm-sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME path/htdocs$fastcgi_script_name;
    }

    location / {
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    # Common deny or drop locations
    location ~* wp-config.php { deny all; }
    location ~* wp-admin/includes { deny all; }
    location ~* wp-includes/.*\.php$ { deny all; }
    location ~ /\. { access_log off; log_not_found off; deny all; }
    location ~ ~$ { access_log off; log_not_found off; deny all; }

     # Prevent scripts from running in /uploads
     location ~* ^/wp-content/uploads/.*.(html|htm|shtml|php)$ {
        types { }
        default_type text/plain;
     }
}

但即使我删除位置“/”和“php”一个(我认为php一个更具体,并在其余部分之前运行)问题仍然存在。甚至是这样的:

# some code

# Common deny or drop locations
location ~* wp-config.php { deny all; }
location ~* wp-admin/includes { deny all; }
location ~* wp-includes/.*\.php$ { deny all; }
location ~ /\. { access_log off; log_not_found off; deny all; }
location ~ ~$ { access_log off; log_not_found off; deny all; }

 # Prevent scripts from running in /uploads
 location ~* ^/wp-content/uploads/.*.(html|htm|shtml|php)$ {
    types { }
    default_type text/plain;
 }

# some code

没有帮助。我仍然可以通过浏览器访问这些文件和文件夹。

1 个答案:

答案 0 :(得分:0)

按照出现顺序检查正则表达式location直到第一次匹配。因此,在您的情况下,location ~ \.php$匹配任何带有php后缀的内容,而nginx会在其他任何内容之前选择它。把那个块放到最后。