【问题标题】:nginx configuration, avoid codeigniter rewrite rulesnginx配置,避免codeigniter重写规则
【发布时间】: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


【解决方案1】:

我从 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

【讨论】:

    【解决方案2】:

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

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

    我想你已经尝试过创建proj_new 文件夹了?在我看来,您似乎已经有足够的手段来实现您想要的文件,而且我看不到任何错误。您是在html 文件夹中创建proj_new 文件夹,对吧?

    刚刚玩过这个,它工作正常。您的配置按预期工作。下面附上我的 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;
            }
        }
    }
    

    【讨论】:

    • 对于那些获得 504 Gateway Time-out 的人,使用此代码将端口 9000 更改为 9071 对我有用在 PHP 7.1.5.
    【解决方案3】:

    你用的是什么版本的 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;
        }
    }
    

    【讨论】:

    • 也许是最新版本...无论如何我会试试这个。
    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

      你能补充吗:

      可以访问“后端”文件夹

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

      【讨论】:

        猜你喜欢
        • 2015-03-04
        • 2013-10-21
        • 1970-01-01
        • 2016-05-30
        • 2010-11-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多