nginx配置,避免codeigniter重写规则

时间:2011-11-18 13:05:59

标签: codeigniter nginx

这是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错误页面。

5 个答案:

答案 0 :(得分:4)

我建议来自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包含以下信息:

$config['base_url'] = "http://nginxcodeigniter.net/";
$config['index_page']   = "";
$config['uri_protocol'] = "REQUEST_URI";

来源:Nginx

答案 1 :(得分:2)

以下代码部分中的!-e表示如果文件,目录或符号链接不存在,则重定向以使用重写规则。你有这个现在的事实应该足以让你创建一个文件夹proj_new,并且应该忽略重写规则。

if (!-e $request_filename)
{
    rewrite ^/(.*)$ /index.php?/$1 last;
    break;
}

我认为你已经尝试过只创建proj_new文件夹了吗?它看起来好像你已经有足够的方法在你的文件中实现你想要的东西,我看不出它有任何错误。您正在proj_new文件夹中创建html文件夹,对吗?

刚开始玩这个游戏,它运行正常。您的配置按预期工作。下面是我的nginx.conf文件,所以你可以看看。这是CI2.1,Nginx 1.0.1稳定,Windows 7,PHP 5.3.1。

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        index index.php index.html index.htm;

        # 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 $document_root$fastcgi_script_name;
            include fastcgi_params;
        }

        # deny access to apache .htaccess files
        location ~ /\.ht
        {
            deny all;
        }
    }
}

答案 2 :(得分:1)

您使用的是哪种版本的nginx?这应该适用于较新的版本,使用try_files指令。

http://ericlbarnes.com/post/12197460552/codeigniter-nginx-virtual-host

server {
    server_name .mysecondsite.com;
    root /sites/secondpath/www;

    index index.html index.php index.htm;

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

答案 3 :(得分:0)

s //尝试使用您的项目名称

更改“后端”
location / {
    # Check if a file or directory index file exists, else route it to index.php.
    try_files $uri $uri/ /index.php;
}

# canonicalize 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 ^/backend/(.*)$ /backend/ permanent;
}

# removes trailing "index" from all controllers
if ($request_uri ~* index/?$)
{
    rewrite ^/backend/(.*)/index/?$ /backend/$1 permanent;
}

# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d $request_filename)
{
    rewrite ^/backend/(.+)/$ /backend/$1 permanent;
}

# removes access to "system" folder
if ($request_uri ~* ^/system)
{
    rewrite ^/backend/(.*)$ /backend/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 ^/backend/(.*)$ /backend/index.php?/$1 last;
    break;
}

答案 4 :(得分:0)

你可以添加:

可以访问“后端”文件夹

    if ($request_uri ~* ^/backend)
    {
        rewrite ^/(.*)$ /backend/index.php?/$1 last;
        break;
    }