Nginx重写规则:检查不存在的文件和文件夹

发布于 2024-08-09 09:09:16 字数 611 浏览 5 评论 0原文

我正在尝试设置 nginx 重写规则如下:

原始结构:

domain.com/index.php?site

=project 现在我尝试使用 nginx 重写引擎屏蔽它:

if (-f $request_filename) {
        break;
      }
      if (!-f $request_filename) {
        rewrite ^/(.+)$ /index.php?site=$1 last;
        break;
      }

如何禁止文件夹(以斜杠)和不存在的文件? 我读了很多关于

尝试文件

但我无法让它工作。

目前我有

    try_files $uri $uri/ @app;

location @app {
rewrite ^/ /404.php last;
}

但行不通。 我做错了什么?

还: 我需要更改我的 PHP 代码吗? 或者纯粹的重写可以吗?

I'm trying to set-up nginx rewrite rules as the following:

Original structure:

domain.com/index.php?site=project

Now I tried to mask it using the nginx rewrite engine:

if (-f $request_filename) {
        break;
      }
      if (!-f $request_filename) {
        rewrite ^/(.+)$ /index.php?site=$1 last;
        break;
      }

How do I forbid folders (stuff ending with a slash) and non-existent files?
I read a lot about

try_files

but I can't get it to work.

at the moment I have

    try_files $uri $uri/ @app;

and

location @app {
rewrite ^/ /404.php last;
}

but it won't work.
What am I doing wrong?

Also:
Do I need to alter my PHP code?
Or is pure rewrite fine?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南城旧梦 2024-08-16 09:09:16

您可以关闭 autoindex 以防止用户查看文件夹。我不知道“禁止不存在的文件”是什么意思。访客只会收到 404。

You can turn off autoindex to prevent users from looking in folders. I do't know what you mean with 'forbid non-existent files'. The visitor just gets a 404.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文