nginx配置,避免codeigniter重写规则 [英] nginx configuration, avoid codeigniter rewrite rules
问题描述
这是针对Codeigniter的Nginx重写规则. 我们可以通过谷歌搜索轻松找到它.
This is nginx rewrite rule for codeigniter. We can find this easily with googling.
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
.
I want to add normal php project in this server.
The project's root directory is /var/www/another/proj_new
.
正如我提到的,这个新项目不使用codeigniter框架. 这是正常的php文件项目.因此它不需要Codeigniter重写规则.
As I mentioned, this new project do not use codeigniter frame. This is normal php file project. So it doesn't need Codeigniter rewrite rule.
因此,我的问题可能是我可以通过网络访问新项目.
So, my question is is possible that I can access the new project through the web.
地址可能是这样的:
http://example.com/proj_new
此地址不应调用codeigniter的proj_new控制器.
This address should not call codeigniter's proj_new controller.
我尝试添加此设置:
server
{
....
....
....
localhost /proj_new {
root /var/www/another/proj_new
index index.php
}
....
....
....
}
但是,http://example.com/proj_new
产生404错误页面.
but, http://example.com/proj_new
makes 404 error pages.
推荐答案
我建议从Nginx进行此配置
I suggest this configuration from Nginx
server {
server_name nginxcodeigniter.net;
root /var/www/codeigniter;
index index.html index.php;
# set expiration of assets to MAX for caching
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
location / {
# Check if a file exists, or route it to index.php.
try_files $uri $uri/ /index.php;
}
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
此后,请确保您的codeIgniter config.php包含以下信息:
After this, make sure that your codeIgniter config.php contains the following information:
$config['base_url'] = "http://nginxcodeigniter.net/";
$config['index_page'] = "";
$config['uri_protocol'] = "REQUEST_URI";
来源: Nginx
这篇关于nginx配置,避免codeigniter重写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!