Anyone use CI with Nginx?

#1
[eluser]John Fuller[/eluser]
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. Wink

#2
[eluser]John Fuller[/eluser]
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
[eluser]Popcorn[/eluser]
http://blog.skateinmars.net/post/2007/09...th-fastcgi

Try this one.

#4
[eluser]John Fuller[/eluser]
[quote author="Popcorn" date="1220684667"]http://blog.skateinmars.net/post/2007/09/03/Setup-CodeIgniter-on-Nginx-server-with-fastcgi

Try this one.[/quote]

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

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
[eluser]thurting[/eluser]
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):

Code:
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):

Code:
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
[eluser]John Fuller[/eluser]
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.

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

Here is a basic snippet of my Nginx config file.

Code:
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
[eluser]Kenzie[/eluser]
Here's what I've found to work best:

Code:
server {
    listen       80;
    server_name  domain.tld www.domain.tld;
    # redirect to remove www
    if ($host = 'www.domain.tld' ) {
        rewrite  ^/(.*)$  http://domain.tld$uri  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
[eluser]mikeyhell[/eluser]
Here's another way to do this (I put all my js/css/img in a dir under the webroot called assets):

Code:
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
[eluser]vmirjamali[/eluser]
Will these work with routes and query off? atm i have to use something like this:
Code:
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
[eluser]kylemac[/eluser]
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.

Code:
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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.