Nginx阻止访问PHP文件

我想只允许访问我的index.php文件,并阻止所有其他的PHP文件。 但是,我的窗口框有什么作用,给我在Debian麻烦。

# Route all requests for non-existent files to index.php if (!-e $request_filename) { rewrite ^/(.*)$ /index.php/$1 last; } # Hide all PHP scripts location ~ \.php { rewrite ^/(.*)$ /index.php/$1 last; } # Forward index.php requests to php-fastcgi location ~ ^/index.php { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass 127.0.0.1:9000; } 

目标是确保一切都通过索引,所以没有人可以加载任何碰巧可以访问的PHP类。

你不需要if检查。 我也尝试清理一下configuration文件,以避免不必要的捕获和指令。 我认为它应该可以工作,但它是免费的,所以可能会出现语法错误等。

 try_files $uri @missing; location @missing { rewrite ^ /index.php$request_uri last; } # This will only run if the below location doesn't, so anything other than /index.php location ~ \.php { rewrite ^ /index.php$request_uri last; } # Remove the = if it doesn't match because of the rewrite appending the URI. location = /index.php { include fastcgi.conf; # Notice that I changed the file, fastcgi_pass 127.0.0.1:9000; }