使用nginx阻止直接访问php文件(仅重写)

时间:2013-05-14 10:42:49

标签: url-rewriting nginx

我有像Ucp.php Somesite.php这样的文件,我想为nginx创建一个重写规则。这不成问题。

但我也想阻止对这些php文件的直接访问,以便enetring http://mysite.com/Ucp.php将返回404 Not Found。

apache有一个解决方案(使用THE_REQUEST),但是如何在nginx上执行此操作?

server
{
        listen 80;
        server_name site.com;
        root /home/site/public_html;

        index Main.php;
        rewrite ^/SomeAddr/$ /SomeAddr.php last;
        rewrite ^/SomeOtherAddr/$ /SomeOtherAddr.php last;


        location ~ \.php$
        {
                fastcgi_pass unix:/var/run/php-fpm/site_com.sock;
                include fastcgi.conf;
        }
        include security.conf;
}

2 个答案:

答案 0 :(得分:0)

您可以为此使用internal directive

location ~ \.php$
{
        internal;
        fastcgi_pass unix:/var/run/php-fpm/site_com.sock;
        include fastcgi.conf;
}

答案 1 :(得分:-2)

location ~ \.php$ {
  return 404;
}