ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Anyone use CI with Nginx?

September 05, 2008 9:59pm

Subscribe [15]
  • #1 / Sep 05, 2008 9:59pm

    John Fuller

    779 posts

    And if so, are you able to use CI without adding the query string after index.php to view your controller methods?

    Looking all over the net, I see instructions on “how to get CodeIgniter to work with Nginx” but that isn’t my problem.  CodeIgniter works if I add the query string.

    I imagine this is the same problem as ExpressionEngine.  I can’t figure out how to configure Nginx to deliver the proper PATH_INFO information.

    I’m using Nginx with FastCGI if that helps.  Just as with anything in the open source ecosystem, the more I Google and read the more I pick up.  Unfortunately I don’t have the man hours to spend on this problem to figure it out all the way.

    Other options are to live with the query string.  Remove it using the Nginx rewrite module.  Or just use Apache like every other sane individual.  😉

  • #2 / Sep 05, 2008 11:01pm

    John Fuller

    779 posts

    Hm, found this thread and apparently this guy did figure out how to remove the query string from the URL.  I guess I just have a configuration problem then.  Ugh.

  • #3 / Sep 05, 2008 11:04pm

    Popcorn

    225 posts

  • #4 / Sep 05, 2008 11:25pm

    John Fuller

    779 posts

    http://blog.skateinmars.net/post/2007/09/03/Setup-CodeIgniter-on-Nginx-server-with-fastcgi

    Try this one.

    Yeah, thanks.  That is how I found the thread that I linked to in my second post.  I suppose I should have mentioned that.  😉

    I really hate configuring web servers.  Actually, I think I hate administration in general.  I spend more time screwing with servers than I do actually coding.

  • #5 / Sep 06, 2008 1:40am

    thurting

    213 posts

    EDIT:

    Something like the following should work (of course you may have to mod for your setup and replace CAPS with proper values - BTW, this was ripped from one of my vhost configs and my main nginx.conf file contains much more general server config):

    server {
      listen PORT;
      server_name NAME;
      root ROOT;
    
      location / {
        index index.php;
    
        if (-e $request_filename) {
          break;
        }
    
        if (!-e $request_filename) {
          rewrite ^(.*)$ /index.php?q=$1 last;
        }
      }
    
      location ~ \.php$ {
        if (!-f $request_filename) {
          return 404;
        }
    
        fastcgi_pass  127.0.0.1:PORT;
        fastcgi_index index.php;
    
        include FASTCGI_PARAMS;
      }
    }

    Also, make sure the following is set in your fastcgi_params file or you will get no input file specified (other vars must be set too - but see the manual):

    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param SCRIPT_NAME     $fastcgi_script_name;
    fastcgi_param DOCUMENT_ROOT   $document_root;

    Nginx can seem a bit strange at first if you are coming from Apache, but once you get the hang of it you will be amazed at how easy and intuitive the config is.  You can do things really simply that are a pain with Apache.  Also, compared to Apache, your RAM consumption will decrease by about 30-40% right off the bat.  In any case, you should always RTFM if you want to gain expertise.

  • #6 / Oct 05, 2008 1:54am

    John Fuller

    779 posts

    I just wanted to update this thread for future peeps looking to do the same thing.

    I did figure out how to use URL’s without either the query string or the index.php.

    Here is a Slicehost thread where I asked the same there and figured out the issue.

    Basically you need to make sure that SCRIPT_FILENAME points directly to your index.php file.  The other link mentioned in this thread also seems correct.  Not sure why it didn’t work for me until now though.

    As both linked areas mentioned, you also need to make the following config.php change.

    $config['uri_protocol']  = "REQUEST_URI";

    Here is a basic snippet of my Nginx config file.

    server {
    
      server_name domain.com;
    
        location  /   {
            fastcgi_pass   localhost:8888;
            fastcgi_param SCRIPT_FILENAME
            /home/manofsteel/ci/index.php;
            ...
          }
    
        location ~ \.(js|ico|gif|jpg|png|css)$ {
          root /home/manofsteel/ci;
          }
    }
  • #7 / Apr 19, 2009 10:18pm

    Kenzie

    13 posts

    Here’s what I’ve found to work best:

    server {
        listen       80;
        server_name  domain.tld <a href="http://www.domain.tld">http://www.domain.tld</a>;
        # redirect to remove www
        if ($host = 'www.domain.tld' ) {
            rewrite  ^/(.*)$  <a href="http://domain.tld$uri">http://domain.tld$uri</a>  permanent;
        }
        location / {
            root /var/www/codeigniter/domain.tld/;
            index index.php index.html;
            # file doesn't exist, let CI handle it
            if (!-f $request_filename) {
                rewrite ^(.*) /index.php?$1 last;
            }
        }
        location ~ \.php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include /opt/nginx/conf/fastcgi_params;
            fastcgi_param SCRIPT_FILENAME /var/www/codeigniter/domain.tld/index.php;
         }
    }

    (Tested with Nginx 0.6.3, CodeIgniter 1.7.1)

  • #8 / Jul 13, 2009 4:44pm

    mikeyhell

    81 posts

    Here’s another way to do this (I put all my js/css/img in a dir under the webroot called assets):

    server {
    
            listen   80;
            server_name <domain>.com;
    
            access_log /home/sites/<domain>/logs/access.log;
            error_log /home/sites/<domain>/logs/error.log;
    
            location ^~ /assets/ {
                root /home/sites/<domain>/public;
            }
    
            location / {
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  /home/sites/<domain>/public/index.php;
                fastcgi_param  PATH_INFO $document_uri;
                include /etc/nginx/fastcgi_params;
            }
    
            error_page  404  /404.html;
            location = /404.html {
                root /home/sites/<domain>/public;
            }
    
            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root /home/sites/<domain>/public;
            }
    
    }
  • #9 / Aug 10, 2009 6:53pm

    vmirjamali

    7 posts

    Will these work with routes and query off? atm i have to use something like this:

    location / {
    index index.php;
    root /path/to/domain.net/httpdocs;
    
     if (-f $request_filename) {
          expires 30d;
          break;
        }
    if (!-e $request_filename) {
          rewrite . /index.php last;
        }
    
    }
    
    location ~ \.php$
    {
    fastcgi_pass 127.0.0.1:9001;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param  REQUEST_URI      $request_uri;
    fastcgi_param SCRIPT_FILENAME /path/to/domain/httpdocs/index.php;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
    }

    For some reason though it causes my other directories like /forums/ to give 404’s, any ideas?

    I use the cache system on ci as well if that’s neded.

  • #10 / May 06, 2010 3:40pm

    kylemac

    4 posts

    Hey all, this may be an old post - but it comes up quite high in the google search so I thought I might mention that some of what you guys are doing can now be handled natively by nginx. Pay attention to the location block.

    server {
            root /home/deploy/public_html/project/public;
    
            access_log /log/access.log;
            error_log /log/error.log;
    
            # the try_files declaration looks for the actual file, and if !exist goes to your fallback.
            location / { try_files $uri $uri/ /index.php; }
    
    
            # this is just an external file for all of your fastcgi_params;
            include php;
            
    }

    Here is the nginx documentation of try_files - http://wiki.nginx.org/NginxHttpCoreModule#try_files

  • #11 / May 06, 2010 8:35pm

    Zack Kitzmiller

    175 posts

    http://blog.skateinmars.net/post/2007/09/03/Setup-CodeIgniter-on-Nginx-server-with-fastcgi

    Try this one.

    Yeah, thanks.  That is how I found the thread that I linked to in my second post.  I suppose I should have mentioned that.  😉

    I really hate configuring web servers.  Actually, I think I hate administration in general.  I spend more time screwing with servers than I do actually coding.

    That is exactly the reason why we started CloudIgniter. It let’s developers get back to developing.

  • #12 / May 25, 2010 1:11am

    Sire

    109 posts

    Hey all, this may be an old post - but it comes up quite high in the google search so I thought I might mention that some of what you guys are doing can now be handled natively by nginx. Pay attention to the location block.

    ...
            # the try_files declaration looks for the actual file, and if !exist goes to your fallback.
            location / { try_files $uri $uri/ /index.php; }
     ...

    Here is the nginx documentation of try_files - http://wiki.nginx.org/NginxHttpCoreModule#try_files

    This didn’t work for me, but I wanted to use try_files and tested a few options.  Here’s what I have so far…

    server {
        listen   80;
        server_name  domain;
    
        location / {
            root /var/www/domain;
            index index.php;
            
            if (-f $request_filename) {
                expires max;
                break;
            }
    
            try_files /maintenance.html $uri $uri/ @codeigniter;
        }
    
        location @codeigniter {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_split_path_info ^(.+\.php)(.*)$;
            fastcgi_param SCRIPT_FILENAME /var/www/domain/index.php;
            include fastcgi_params;
            fastcgi_param QUERY_STRING $request_uri;
        }
    
        location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
            access_log        off;
            expires           30d;
        }
    }

    The file /maintenance.html doesn’t usually exist, but if I want to bring the site down temporarily while I work on it, all I have to do is create maintenance.html in the web root and it will be displayed to users until it is removed.

  • #13 / Nov 24, 2010 6:34pm

    dotted

    1 posts

    Hey all, this may be an old post - but it comes up quite high in the google search so I thought I might mention that some of what you guys are doing can now be handled natively by nginx. Pay attention to the location block.

    I will go ahead and use the same excuse for posting here 😊

    Now i wanted to do a proper config for codeigniter that did not use any Ifs whatsoever, because Ifs are evil, and worked using PATH_INFO to mix segmented URLs and querystrings.

    These were made using CodeIgniter 2.0, and haven’t tested them using 1.7.2, but should work

    server {
        server_name     _;
        index           index.html;
        root            /var/www/;
    
        location / {
            try_files $uri $uri/ @codeigniter;
        }
        #Restrict access to application and system folders
        #location /system { #For 1.7.2, uncomment this and comment below to use
        location /(application|system) { #2.0, uncomment this and comment above to use - remember to edit application to your apps folder if changed
            internal;
        }
    
        #Cache static content
        location ~* ^.+\.(jpg|jpeg|gif|css|png|js|ico|swf|wav)$ {
            expires max;
        }
        location @codeigniter {
            include /etc/nginx/fastcgi_params;
            fastcgi_pass   localhost:9000;
            fastcgi_index  index.php;
    
            fastcgi_split_path_info ^(/)(.*)$;
            fastcgi_param SCRIPT_FILENAME /var/www/index.php;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            fastcgi_param DOCUMENT_ROOT /var/www/;
            fastcgi_param SERVER_NAME $http_host;
        }
    }
  • #14 / Nov 08, 2011 12:52pm

    Ali Fattahi

    48 posts

    Hello guys
    i have installed nginx 1.0.9 in my server , but i have a problem in codeigniter .
    but the $_POST is empty yet :(
    i’m using codeigniter 2.0.3 , nginx 1.0.9 and $config[‘uri_protocol’] = “QUERY_STRING”;
    how can i solve this problem ?


    That is my nginx’s config file
    =====================——————————————-

    server
    {
    
     listen 443 default_server ssl;
     ssl on;
     server_name mysite.com <a href="http://www.mysite.com">http://www.mysite.com</a>;
    
     root /home/site/mysite.com/;
     ssl_certificate /etc/nginx/config/www_mysite_com.crt;
     ssl_certificate_key /etc/nginx/config/www_mysite_com.key;
     keepalive_timeout    3;
     index index.html index.htm index.php;
     access_log /var/log/nginx/nginx.vhost.access.log;
     error_log /var/log/nginx/nginx.vhost.error.log;
     
     location / { 
      try_files $uri $uri/ /index.php; 
      if (-f $request_filename) {
                  expires max;
                  break;
             }
     }
     if ($request_uri ~* ^(/welcome(/index)?|/index(.php)?)/?$)
     {
      rewrite ^(.*)$ / permanent;
     }
     if ($request_uri ~* index/?$)
     {
      rewrite ^/(.*)/index/?$ /$1 permanent;
     }
     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;
     # deny access to apache .htaccess files
     location ~ /\.ht
     {
      deny all;
     }
     location ~ \.php$ 
     {
      if ($uri !~ "^/ftp/")
      {
       fastcgi_pass   127.0.0.1:9000;
      }
      fastcgi_index  index.php;
      include fastcgi_params;
      fastcgi_param  PATH_INFO $document_uri;
      fastcgi_param  SCRIPT_FILENAME  /home/site/mysite.com$fastcgi_script_name;
      
     }
     # Block Download Agents
     if ($http_user_agent ~* LWP::Simple|BBBike|wget) 
     {
                return 403;
          }
     if ($http_user_agent ~* msnbot|scrapbot) 
     {
                return 403;
          }
     
    
    }
  • #15 / Nov 11, 2011 1:53pm

    Eric Barnes

    487 posts

    I recently switched to nginx and here is how I setup my host -

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

    I also kept the protocol to auto with out any issues.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases