Codeigniter show `welcome to nginx` page when requset controller page

hi all,

I’m new with codeigniter, usually i just write native php code but today i got one request project from someone that need to use codeigniter. but, a problem just occur when i start code with CI (codeigniter).

i am usually use, nginx - php - mysql and i have read CI user_guide properly and i just understand how to use that framework. ok, let’s face the problem:

I’ve create a controller just like this:

<?php

    class post extends CI_Controller
    {
        
        public function __construct() {
            parent::__construct();
            
            // load site_model
            $this->load->model('site_model');
        }
        
        public function index()
        {
            $data['posts'] = $this->Site_model->getPosts();            
            $data['title'] = 'Index Page!';
            
            // load view
            $this->load->view('template/header', $data);
            $this->load->view('post/index', $data);
            $this->load->view('template/footer');
        }
        
        public function view($id)
        {
            $data['post'] = $this->Site_model->getPosts($id);
            
            /*
             * show 404 page error if the requested ID
             * is not found in the database.
             */
            if (empty($data['post']))
            {
                show_404();
            }
            
            $data['title'] = $data['post']['post_title'];
            
            // load the needed view
            $this->load->view('template/header', $data);
            $this->load->view('post/view', $data);
            $this->load->view('template/footer');
        }
    }

i think this is very basic CI code, but when try to access this url: http://localhost/cif/post and the server just show welcome to nginx page :frowning:

fyi, http://localhost/cif is my base project and i was set permission both of dir and files properly :slight_smile:

How is your NGiNX virtual host defined?

It should have something like try_files $uri $uri/ /index.php?$query_string; in it somewhere.

this is my nginx config (/etc/nginx/sites-available/default):

# You may add here your

# statements for each of your virtual hosts to this file

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

server {
        listen   80; ## listen for ipv4; this line is default and implied
        listen   [::]:80 default ipv6only=on; ## listen for ipv6

        root /var/www;
        index index.php index.html index.htm;

        # Make site accessible from http://localhost/
        server_name localhost;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to index.html
                try_files $uri $uri/ /index.html;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }

        location /doc/ {
                alias /usr/share/doc/;
                autoindex on;
                allow 127.0.0.1;
                deny all;
        }

    # Only for nginx-naxsi : process denied requests
        #location /RequestDenied {
                # For example, return an error code
                #return 418;
        #}

        #error_page 404 /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /var/www;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \\.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\\.php)(/.+)$;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\\.ht {
                deny all;
        }
}

# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen 8000;
#    listen somename:8080;
#    server_name somename alias another.alias;
#    root html;
#    index index.html index.htm;
#
#    location / {
#        try_files $uri $uri/ /index.html;
#    }
#}


# HTTPS server
#
#server {
#    listen 443;
#    server_name localhost;
#
#    root html;
#    index index.html index.htm;
#
#    ssl on;
#    ssl_certificate cert.pem;
#    ssl_certificate_key cert.key;
#
#    ssl_session_timeout 5m;
#
#    ssl_protocols SSLv3 TLSv1;
#    ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
#    ssl_prefer_server_ciphers on;
#
#    location / {
#        try_files $uri $uri/ /index.html;
#    }
#}

Hi @mildfoam,

To use CodeIgniter it is advisable to make sure that the initial installation is correct and the ‘Welcome to CodeIgniter’ default view is appearing with no errors.

Once that is running then investigate:

  1. config/config.php and play about with the log file settings. $config[‘log_threshold’] = 0; // 1,2,3, 4; and check differences in log file output
  2. config/config.php removal of index.php along with .htaccess file settings

This is your modified file which I have tested locally using:

http://localhost/index.php/post


<?php
class Post extends CI_Controller
{
public function __construct()
{
  parent::__construct();
  
  // load site_model
  #$this->load->model('site_model'); // unremm once "Welcome to CodeIgniter" is working
}
        
public function index()
{
  $view_file = 'welcome_message.php';
  
  if(file_exists( VIEWPATH .$view_file ) )
  {
    $this->load->view($view_file);
  }
  else
  {
    echo 'Missing file: ' .$view_file;
  }
}#

public function OLD_index()
{
  $this->load->model('site_model');
  
  $data['posts'] = $this->Site_model->getPosts();            
  $data['title'] = 'Index Page!';
  
  // load view
  $this->load->view('template/header', $data);
  $this->load->view('post/index', $data);
  $this->load->view('template/footer');
}#
        
public function view($id)
{
  $data['post'] = $this->Site_model->getPosts($id);
  
  /*
   * show 404 page error if the requested ID
   * is not found in the database.
   */
  if (empty($data['post']))
  {
      show_404();
  }
  
  $data['title'] = $data['post']['post_title'];
  
  // load the needed view
  $this->load->view('template/header', $data);
  $this->load->view('post/view', $data);
  $this->load->view('template/footer');
}#

}


Change the first server section to this and then restart NGiNX:


server {
        listen   80; ## listen for ipv4; this line is default and implied
        listen   [::]:80 default ipv6only=on; ## listen for ipv6

        root /var/www;
        index index.php index.html index.htm;

        # Make site accessible from http://localhost/
        server_name localhost;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to index.html
                try_files $uri $uri/ /index.html;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
        }

        location /doc/ {
                alias /usr/share/doc/;
                autoindex on;
                allow 127.0.0.1;
                deny all;
        }

        location /cif/ {
             try_files $uri $uri/ /cif/index.php$uri?$query_string;
        }

    # Only for nginx-naxsi : process denied requests
        #location /RequestDenied {
                # For example, return an error code
                #return 418;
        #}

        #error_page 404 /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /var/www;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \\.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\\.php)(/.+)$;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\\.ht {
                deny all;
        }
}

(I added the location /cif/ part)

hi

i’ve change the error log configuration to 4 then i open the error log file but there is no any error message i got.

hi ScallioXTX,

i’ve change the nginx configuration then restart the server so i am try to open my CI project and the result is:

[h=1]500 Internal Server Error[/h] [HR][/HR]nginx/1.1.19

what should i do?

@mildfoam;
Did you get the default “welcome to CodeIgniter” view working?

You need to find index.php and make sure that is being called.

yes, i did :slight_smile:

Ok change the config.php log file path setting to = FCPATH: run the welcome message again and check your log file.

I am typing on my tablet and waiting for a virus check to finish on my desktop :frowning:

Edit: after FCPATH it should be ; and not :

[B][COLOR=#0071d8]John_Betong[/COLOR][/B]

@@John_Betong

thanks for your answer but still error and display:

500 Internal Server Error

here is my /var/log/nginx

2012/11/10 17:40:23 [error] 14539#0: *1 rewrite or internal redirection cycle while internally redirecting to “/cif/index.php/cif/index.php/cif/index.php/cif/index.php/cif/index.php/cif/index.php/cif/index.php/cif/index.php/cif/index.php/cif/index.php/cif/index.php/cif/post”, client: 127.0.0.1, server: localhost, request: “GET /cif/post HTTP/1.1”, host: “localhost”


btw, hope you can fix your computer (desktop) system.

From the error it looks like there is a loop and index.php is being called repeatedly.

Ensire your error reporting is set to 4 and run the default “Welcome to CodeIgniter”.
Check your error_log and it should be similar to the results from my error log below.
Next, delete the error log and run your http://localhost/index.php/post then check your error log again.


<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?>

DEBUG - 18:53:02 --> Config Class Initialized
DEBUG - 18:53:02 --> Hooks Class Initialized
DEBUG - 18:53:02 --> Utf8 Class Initialized
DEBUG - 18:53:02 --> UTF-8 Support Enabled
DEBUG - 18:53:02 --> URI Class Initialized
DEBUG - 18:53:02 --> Router Class Initialized
DEBUG - 18:53:02 --> Output Class Initialized
DEBUG - 18:53:02 --> Security Class Initialized
DEBUG - 18:53:02 --> Input Class Initialized
DEBUG - 18:53:02 --> Global POST and COOKIE data sanitized
DEBUG - 18:53:02 --> Language Class Initialized
DEBUG - 18:53:02 --> Loader Class Initialized
DEBUG - 18:53:02 --> Config file loaded: ci_test/config/config_ci_jokes.php
DEBUG - 18:53:02 --> Config file loaded: ci_test/config/config_max_sess.php
DEBUG - 18:53:02 --> Helper loaded: url_helper
DEBUG - 18:53:02 --> Helper loaded: string_helper
DEBUG - 18:53:02 --> Helper loaded: inflector_helper
DEBUG - 18:53:02 --> Helper loaded: html_helper
DEBUG - 18:53:02 --> Helper loaded: text_helper
DEBUG - 18:53:02 --> Helper loaded: form_helper
DEBUG - 18:53:02 --> Database Driver Class Initialized
DEBUG - 18:53:02 --> User Agent Class Initialized
DEBUG - 18:53:02 --> Model Class Initialized
DEBUG - 18:53:02 --> Model Class Initialized
DEBUG - 18:53:02 --> Controller Class Initialized
DEBUG - 18:53:02 --> File loaded: ci_test/views/welcome_message.php
DEBUG - 18:53:02 --> Final output sent to browser
DEBUG - 18:53:02 --> Total execution time: 0.1855


You could also try inserting the following into your post.php
controllers/post.php


<?php

  // DEBUG - HALT TO ENSURE THIS CLASS IS CALLED
  echo __FILE__;die 


class Post extends CI_Controller
{
public function __construct()
{
  parent::__construct();
  
  // load site_model
  #$this->load->model('site_model');
}


Also check to ensure:

config/routes.php


<?php

  $route["default_controller"] = "welcome"; // DEFAULT

  $route["post"]    = "post";


>>> btw, hope you can fix your computer (desktop) system.
Thanks but Opera is still refusing to load Gmail and I am having to use another browser.

That looks like it can’t find cif/index.php. Does that file exist?
If so, try

location /cif/ {
try_files $uri $uri/ /index.php$uri?$query_string;
}

(I would try if it works, but I’m on my mobile phone and don’t have access to a desktop here, so… )