nginx如何配置只允许访问index.php其它的一律不能访问?

2023-06-14 290 0

nginx如何配置只允许访问index.php其它的一律不能访问?
比如 我目录中有index.php 和test.php等 文件
如何配置只允许访问 index.php, 而test.php不能访问

server
    {
        listen 80;
        server_name 192.168.16.86;
        index index.php;
        root  /home/wwwroot/web;
        include enable-php.conf;
        location = /index.php {
           try_files $uri $uri/ /index.php?$query_string;
        }
        location / {
           deny all;
        }
        location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }
        location ~ .*.(js|css)?$
        {
            expires      12h;
        }
        location ~ /.well-known {
            allow all;
        }
        location ~ /.
        {
            deny all;
        }
        access_log /home/logs/web.log;
    }

我这个配置可以吗

也还是要分情况,如果你只想 index.php 被访问,其他所有路径都屏蔽,那可以像下面这样配置

location = /index.php {
    # TODO 对于 index.php 的处理
}
location / {
  deny all;
}

那如果你只想所有 php 中,只有 index.php 被访问,而其他 php 不允许,但是对于其他请求又正常放行(比如静态资源),那就要调整一下。


location / {
  # TODO 其他资源
}
location ~ .php$ {
  # 拒绝所有 .php 文件的访问(除了 /index.php)
  deny all;
}
# 仅允许 /index.php 的访问
location = /index.php {
  # 对于 index.php 的处理
}

回答

相关文章

nuxt2部署静态化和ssr的时候访问首页先报404再出现首页为什么?
`clip-path` 如何绘制圆角平行四边形呢?
多线程wait方法报错?
VUE 绑定的方法如何直接使用外部函数?
vue2固定定位该怎么做?
谁有redis实现信号量的代码,希望借鉴一下?