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

标签 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;
}

最佳答案

您可以使用 internal directive为此目的。

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

关于url-rewriting - 使用 nginx 阻止直接访问 php 文件(仅重写),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16541090/

相关文章:

java - Tuckey URL 重写中的转义问号(XML/Regex)

tomcat - UrlRewriteFilter 相当于 mod_rewrite

apache - mod_rewrite 用于友好的 URL

apache - 重命名 URL 中的文件(.php 文件)

python - 使用 unicode_literals 在 Flask 应用程序中添加 header

wordpress - wordpress 的漂亮永久链接结构无法在 Godaddy 主机上使用 Windows 托管和 IIS 服务器

nginx - PHP 语法错误,意外 '['

docker - 如何将nginx设置为kubernetes中rest微服务的反向代理?

nginx - 由 Nginx 代理的 Tomcat 上的古巴

node.js - 如何将 Visual Studio 代码调试器附加到在 nginx 代理后面的 docker 机器上运行的 Node.JS 应用程序