【发布时间】:2011-11-18 13:05:58
【问题描述】:
这是 codeigniter 的 nginx 重写规则。 我们可以通过谷歌搜索轻松找到它。
server
{
server_name .example.com;
access_log /var/log/nginx/example.com.access.log;
root /var/www/example.com/html;
index index.php index.html index.htm;
# enforce www (exclude certain subdomains)
# if ($host !~* ^(www|subdomain))
# {
# rewrite ^/(.*)$ $scheme://www.$host/$1 permanent;
# }
# enforce NO www
if ($host ~* ^www\.(.*))
{
set $host_without_www $1;
rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
}
# canonicalize codeigniter url end points
# if your default controller is something other than "welcome" you should change the following
if ($request_uri ~* ^(/welcome(/index)?|/index(.php)?)/?$)
{
rewrite ^(.*)$ / permanent;
}
# removes trailing "index" from all controllers
if ($request_uri ~* index/?$)
{
rewrite ^/(.*)/index/?$ /$1 permanent;
}
# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d $request_filename)
{
rewrite ^/(.+)/$ /$1 permanent;
}
# removes access to "system" folder, also allows a "System.php" controller
if ($request_uri ~* ^/system)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
# catch all
error_page 404 /index.php;
# use fastcgi for all php files
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/example.com/html$fastcgi_script_name;
include fastcgi_params;
}
# deny access to apache .htaccess files
location ~ /\.ht
{
deny all;
}
}
我想在这个服务器中添加普通的 php 项目。
项目的根目录是/var/www/another/proj_new。
正如我所提到的,这个新项目不使用 codeigniter 框架。 这是普通的 php 文件项目。所以不需要 Codeigniter 重写规则。
所以,我的问题是我可以通过网络访问新项目。
地址可能是这样的:
http://example.com/proj_new
此地址不应调用 codeigniter 的 proj_new 控制器。
我已尝试添加此设置:
server
{
....
....
....
localhost /proj_new {
root /var/www/another/proj_new
index index.php
}
....
....
....
}
但是,http://example.com/proj_new
生成 404 错误页面。
【问题讨论】:
-
在玩过之后更新了我的答案。您的代码似乎工作正常。我已将我的 nginx.conf 文件添加到答案中,因此您可以尝试一下。显然需要一些路径修改等,因为我在 Windows 上对其进行了测试,并且您似乎正在使用某种 Linux/Unix。
标签: codeigniter nginx