【问题标题】:Block direct access to php files (only rewrited) using nginx使用 nginx 阻止对 php 文件的直接访问(仅重写)
【发布时间】:2020-09-29 05:37:53
【问题描述】:

我有像 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;
}

【问题讨论】:

    标签: url-rewriting nginx


    【解决方案1】:

    您可以为此使用internal directive

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

    【讨论】:

    • 这对我有用。显示 404,我们可以将 404 更改为其他内容吗?
    【解决方案2】:
    location ~ \.php$ {
      return 404;
    }
    

    【讨论】:

    • 不是吗?即使它被重写,它也会阻止每个 php 文件。你没看我的帖子吗?
    • 你什么也没说你的配置...你用过fastcgi_pass吗?
    • 是的,我当然知道。我唯一要更改的是阻止包含 .php 的网址
    • 配置现在在第一篇文章中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-14
    • 2018-05-02
    • 2014-10-12
    • 1970-01-01
    • 2012-11-03
    • 2016-10-24
    • 1970-01-01
    相关资源
    最近更新 更多