1

I've been developing a website with Codeigniter on my local machine, running an Apache server. I did not realise the production server would be running Nginx. When attempting to run CI, I now run into the problem that those pretty URLs with segments in them do not work. I keep getting a 404 page.

I have no experience with Nginx, but I found a few code snippits via Google that I tried.

I'm in a shared hosting situation, meaning I have limited configuration options. This results in the configuration interface rejecting most of the configuration snippits I've copy-pasted into it in an attempt to get it to work.

So far, I've found out it rejects the keywords server_name, root and include, which seem to appear in every single solution I've found.

As I have little knowledge on the subject, I'm not sure if what I'm trying to do (i.e. get Codeigniter up and running with slash-separated URL parts rather than a query string) is even possible when I'm not able to use the afformentioned keywords.

Is there a 'default' piece of Nginx configuration available for Codeigniter that might help me out here? Or is my situation too limited to even allow for a solution? Should I just ask my host for help?

EDIT: Note that I'm not trying to remove index.php from my URLs to make them more appealing - I'm not at that point yet. This is about URL segments in general - you know, the default behaviour of Codeigniter.

2
  • "Hiding index.php" and "url segments in general" - is really the same task - need to enable rewriting option. Commented Aug 23, 2013 at 5:39
  • I disagree. On ellislab.com/codeigniter/user-guide/general/urls.html, it says that by default it does include index.php, and provides a rewrite to fix this. The URI segments are a default functionality without rewrites. That appear to be two different scenario's. Commented Aug 23, 2013 at 7:38

1 Answer 1

2

Then you say "Slash-separated URLs", you must understand, that it's just some URL, that is leads to non-existing file (for example, site.com/controller/action/param1/value1 leads to "folders" /controller/action/param1 and "file" value1), and tghis situation is solved by using mod_rewrite in apache - it just rewrites any URL, that points to non-existing file to a url, pointing to index.php. So, In nginx you need just the same.

In your nginx configuration you have to add this locations:

# for rewriting non-existing url-s to index.php
location / {
    try_files $uri $uri/ /index.php?$args;
}

and

#location, that enables to process *.php files thorugh php-fpm (fastcgi)
#it's possible that you already have this block configured, and don't need to change it.
location ~ \.php$ {
    try_files $uri =404;        
    fastcgi_pass 127.0.0.1:9999;
    fastcgi_index index.php;        include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
    fastcgi_ignore_client_abort off;
}

Also, If it doesn't help, just google "Configure nginx for codeigniter", or, ask your webhoster support "please, make changes to config so url rewriting could start to work"

Sign up to request clarification or add additional context in comments.

2 Comments

That's exactly the config I've been finding all evening, but the input field my host provides doesn't allow the include statement on line 6 of your second snippit. I'll be asking them why, I guess.
You have to ask your hosting' technical support for it. It's a very simple and widely used config, so, if your hosting doesn't give you ability to set it manually, they should do it by hands.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.