将Apache的.htaccess转换为Nginx的重写规则

6

我需要从apache转换到Nginx,但.htaccess在Nginx服务器上不起作用。

以下是我在Apache中使用的.htaccess内容:

RewriteEngine On 

# always run through the indexfile
RewriteRule .$ index.php

# don't let people peek into directories without an index file
Options -Indexes

当我在Nginx服务器上放置.htaccess文件时,该文件会被服务器删除。请问我应该将.htaccess数据放在哪种类型的文件中(名称和后缀名),以及如何为Nginx重写该文件,并将其放在哪里?
我希望所有子目录文件都通过我的index.php文件处理,以便我可以从index.php文件中包含特定文件...
我尝试过类似以下内容的方法:
# nginx configuration

autoindex off;

location / {
    rewrite .$ /index.php;
}

但它不起作用。目录看起来像这样:

── root
    ├── folder1
    │   └── folder2
    │       └── file.php
    └── index.php

如果我请求 root/folder1/folder2/file.php,我希望服务器加载 root/index.php,因为我仍然有服务器URL请求, 我可以从我的index.php中包含root/folder1/folder2/file.php

在我的Apache安装上所有这些都可以工作,但在Nginx服务器上却不能。 请帮忙解决。

编辑: 原来nginx.conf文件必须位于根目录之上,并且只能访问到根目录之上的文件夹。需要具备服务器管理员权限才能访问。


你找到了 http://www.anilcetin.com/ 或者 http://winginx.com/en/htaccess 吗? - MonkeyZeus
是的,还有许多其他网站。我尝试将我的代码放在nginx.conf和同一文件夹中的nginx.conf.txt中,但不起作用。nginx服务器会自动删除htaccess文件。代码是否应该放在htaccess文件中? - Medda86
1个回答

4

Nginx没有.htaccess文件。代码需要放入Nginx配置文件中。另外,尝试在重写规则中添加“last”标志:

# nginx configuration

autoindex off;

location / {
    rewrite .* /index.php last;
}

文件会被命名为nginx.conf并位于根目录下的index.php吗? - Medda86
1
@user3434534 不是的。请查看初学者指南中的第三段,其中告诉您它在哪里。在不同的系统上可能会有所不同。 - Jon Lin
如果我没有访问根目录的权限,我是否可以通过在我正在使用的目录中放置自己的nginx.conf文件来覆盖它? - Medda86
@Medda86 不是这样的,nginx不是这么工作的。如果你没有访问配置文件的权限,就无法改变nginx的行为。 - Jon Lin
好的,谢谢。我需要与管理员交谈,让他们为我设置。感谢您的解释。 - Medda86

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接