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

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

我这个配置可以吗

阅读 2.7k
1 个回答

也还是要分情况,如果你只想 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 的处理
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题