How to Fix CodeIgniter 404 Errors on Nginx

Date Posted: May 25, 2017
Last Updated: September 23, 2026

Introduction

When a CodeIgniter application is deployed on an Nginx web server, application routes such as /login, /admin, or /dashboard may return a 404 Not Found error even though the application works correctly on Apache. This usually happens because Nginx needs to be explicitly configured to route requests that do not match physical files or directories to CodeIgniter’s front controller, index.php.

In this guide, we will explain how to configure Nginx URL routing for a CodeIgniter application and resolve 404 errors for custom application routes. The guide also covers the required PHP-FPM configuration, Nginx configuration validation, subdirectory deployments, and common troubleshooting steps.

Prerequisites

Before proceeding, ensure you have:

  • A CodeIgniter application deployed on an Ubuntu server
  • Nginx installed and running
  • PHP-FPM installed and configured
  • SSH or sudo access to the server
  • The CodeIgniter application deployed under the configured Nginx document root

Note: The exact Nginx configuration may vary depending on your CodeIgniter version, PHP version, application directory structure, and server setup.

Understanding the Problem

CodeIgniter uses a front controller architecture where application requests are generally processed through:

index.php

For example, a request such as:

https://example.com/dashboard

may not correspond to an actual /dashboard file or directory on the server.

If Nginx is configured to look only for physical files, it may return:

404 Not Found

Instead, Nginx needs to pass unmatched requests to CodeIgniter’s index.php.

Implementation

Step 1: Locate the Nginx Configuration

The default Nginx virtual host configuration is commonly located at:

/etc/nginx/sites-enabled/default

Open the configuration:

sudo vi /etc/nginx/sites-enabled/default

If your application uses a separate virtual host configuration, edit the configuration file associated with your domain instead.

For example:

/etc/nginx/sites-available/example.com

Step 2: Configure the location / Block

Add or update the location / block as follows:

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

The important part is:

try_files $uri $uri/ /index.php?$query_string;

This configuration tells Nginx to:

  1. Check whether the requested URI matches an existing file.
  2. Check whether the requested URI matches an existing directory.
  3. If neither exists, forward the request to CodeIgniter’s index.php.

This allows CodeIgniter to process routes such as:

/login
/admin
/dashboard

Step 3: Verify the PHP-FPM Configuration

Make sure your Nginx configuration contains a PHP location block that points to the correct PHP-FPM socket.

For example:

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.2-fpm.sock;
}

The PHP-FPM socket depends on the PHP version installed on your server.

Check the available PHP-FPM sockets with:

ls /run/php/

For example, you may see:

php8.1-fpm.sock
php8.2-fpm.sock

Use the socket corresponding to your installed PHP version.

Step 4: Verify the Nginx Configuration

Before restarting Nginx, always test the configuration:

sudo nginx -t

A successful configuration should return output similar to:

syntax is ok
test is successful

Step 5: Reload Nginx

If the configuration test succeeds, reload Nginx:

sudo systemctl reload nginx

A reload is generally preferred over a full restart because it applies the configuration without unnecessarily stopping the web server.

If required, you can restart Nginx:

sudo systemctl restart nginx

Step 6: Test the CodeIgniter Routes

Open your CodeIgniter application and test the routes that previously returned 404 errors.

For example:

https://example.com/login
https://example.com/admin
https://example.com/dashboard

The requests should now be routed through CodeIgniter’s index.php.

CodeIgniter Configuration

If Nginx routing is configured correctly but the application still generates unexpected URLs, verify the CodeIgniter configuration.

For CodeIgniter 3, check:

application/config/config.php

Set the base URL appropriately:

$config['base_url'] = 'https://example.com/';

If you are using clean URLs, you may also configure:

$config['index_page'] = '';

This allows URLs such as:

https://example.com/login

instead of:

https://example.com/index.php/login

Deploying CodeIgniter in a Subdirectory

If the CodeIgniter application is deployed under a subdirectory, the Nginx configuration must account for that path.

For example, if the application is available at:

https://example.com/directory/

the configuration can be:

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

After making the change, verify the configuration:

sudo nginx -t

Then reload Nginx:

sudo systemctl reload nginx

Note: The exact configuration for a subdirectory deployment depends on the application’s document root and PHP-FPM setup.

Troubleshooting

Nginx Still Returns 404

Check the following:

  • Verify that the try_files directive is inside the correct server block.
  • Confirm that the Nginx document root points to the correct CodeIgniter application directory.
  • Verify that index.php exists in the expected location.
  • Check the Nginx error log.
sudo tail -f /var/log/nginx/error.log

PHP-FPM Is Not Working

Check the PHP-FPM service:

sudo systemctl status php8.2-fpm

Replace php8.2-fpm with the PHP-FPM version installed on your server.

You can also verify the PHP-FPM socket:

ls -l /run/php/

Nginx Configuration Test Fails

Run:

sudo nginx -t

Review the error message carefully and correct the configuration before reloading Nginx.

Static Files Are Not Loading

If CSS, JavaScript, images, or other static files return 404 errors, verify that:

  • The Nginx document root is correct.
  • The files exist in the expected directories.
  • File and directory permissions allow Nginx to read them.
  • The application is generating the correct asset URLs.

A basic CodeIgniter application configuration can look similar to:

server {
    listen 80;
    server_name example.com;

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

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
    }
}

Important: This is a basic example. Production environments should also include HTTPS, appropriate security headers, access controls, logging, and other server-hardening measures.

Conclusion

CodeIgniter 404 errors after migrating an application from Apache to Nginx are commonly related to URL routing and front-controller configuration.

Using the Nginx try_files directive allows requests that do not correspond to physical files or directories to be passed to CodeIgniter’s index.php.

The key configuration is:

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

After updating the configuration, always run nginx -t before reloading the service.

Frequently Asked Questions

1. Why does CodeIgniter work on Apache but return 404 on Nginx?

Apache and Nginx handle URL rewriting and routing differently. Nginx requires the appropriate try_files configuration to forward application routes to CodeIgniter’s front controller.

2. What does try_files do in Nginx?

The try_files directive checks whether the requested path corresponds to an existing file or directory. If neither exists, the request is forwarded to the specified fallback, which in this case is CodeIgniter’s index.php.

3. Do I need to restart Nginx after changing the configuration?

You should at least reload Nginx after making configuration changes:

sudo systemctl reload nginx

Always test the configuration first:

sudo nginx -t

4. Why is $query_string included in the configuration?

Using:

/index.php?$query_string

preserves the original query parameters when the request is forwarded to CodeIgniter.

5. Can the same configuration be used for CodeIgniter 4?

The general Nginx front-controller approach is similar, but CodeIgniter 4 typically uses the public directory as the web root. The Nginx document root and configuration should therefore be adjusted according to the CodeIgniter 4 deployment structure.

How Pheonix Solutions Can Help

Pheonix Solutins provides web application development, PHP and CodeIgniter development, Linux server administration, Nginx configuration, cloud infrastructure, DevOps, CI/CD, security hardening, monitoring, and application deployment services.

Our team can help deploy, troubleshoot, optimize, secure, and maintain PHP applications across development, staging, and production environments.

Talk to our team:
https://pheonixsolutions.com/contact-us/

When deploying a CodeIgniter application behind Nginx, you may encounter 404 Not Found errors when accessing application routes such as:

https://example.com/login
https://example.com/admin
https://example.com/dashboard

The application may work correctly when hosted on Apache but return 404 errors when deployed on Nginx.

This commonly occurs when Nginx is not configured to forward requests for CodeIgniter routes to the application’s index.php front controller.

This guide explains how to configure Nginx to correctly handle CodeIgniter routes and resolve these 404 errors.

Prerequisites

Before proceeding, ensure you have:

  • A CodeIgniter application deployed on an Ubuntu server
  • Nginx installed and running
  • PHP-FPM installed and configured
  • SSH or sudo access to the server
  • The CodeIgniter application deployed under the configured Nginx document root

Note: The exact Nginx configuration may vary depending on your CodeIgniter version, PHP version, application directory structure, and server setup.

Understanding the Problem

CodeIgniter uses a front controller architecture where application requests are generally processed through:

index.php

For example, a request such as:

https://example.com/dashboard

may not correspond to an actual /dashboard file or directory on the server.

If Nginx is configured to look only for physical files, it may return:

404 Not Found

Instead, Nginx needs to pass unmatched requests to CodeIgniter’s index.php.

Implementation

Step 1: Locate the Nginx Configuration

The default Nginx virtual host configuration is commonly located at:

/etc/nginx/sites-enabled/default

Open the configuration:

sudo vi /etc/nginx/sites-enabled/default

If your application uses a separate virtual host configuration, edit the configuration file associated with your domain instead.

For example:

/etc/nginx/sites-available/example.com

Step 2: Configure the location / Block

Add or update the location / block as follows:

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

The important part is:

try_files $uri $uri/ /index.php?$query_string;

This configuration tells Nginx to:

  1. Check whether the requested URI matches an existing file.
  2. Check whether the requested URI matches an existing directory.
  3. If neither exists, forward the request to CodeIgniter’s index.php.

This allows CodeIgniter to process routes such as:

/login
/admin
/dashboard

Step 3: Verify the PHP-FPM Configuration

Make sure your Nginx configuration contains a PHP location block that points to the correct PHP-FPM socket.

For example:

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.2-fpm.sock;
}

The PHP-FPM socket depends on the PHP version installed on your server.

Check the available PHP-FPM sockets with:

ls /run/php/

For example, you may see:

php8.1-fpm.sock
php8.2-fpm.sock

Use the socket corresponding to your installed PHP version.

Important: Do not blindly copy the PHP-FPM socket from this example. Verify the socket available on your server first.

Step 4: Verify the Nginx Configuration

Before restarting Nginx, always test the configuration:

sudo nginx -t

A successful configuration should return output similar to:

syntax is ok
test is successful

Step 5: Reload Nginx

If the configuration test succeeds, reload Nginx:

sudo systemctl reload nginx

A reload is generally preferred over a full restart because it applies the configuration without unnecessarily stopping the web server.

If required, you can restart Nginx:

sudo systemctl restart nginx

Step 6: Test the CodeIgniter Routes

Open your CodeIgniter application and test the routes that previously returned 404 errors.

For example:

https://example.com/login
https://example.com/admin
https://example.com/dashboard

The requests should now be routed through CodeIgniter’s index.php.

CodeIgniter Configuration

If Nginx routing is configured correctly but the application still generates unexpected URLs, verify the CodeIgniter configuration.

For CodeIgniter 3, check:

application/config/config.php

Set the base URL appropriately:

$config['base_url'] = 'https://example.com/';

If you are using clean URLs, you may also configure:

$config['index_page'] = '';

This allows URLs such as:

https://example.com/login

instead of:

https://example.com/index.php/login

Deploying CodeIgniter in a Subdirectory

If the CodeIgniter application is deployed under a subdirectory, the Nginx configuration must account for that path.

For example, if the application is available at:

https://example.com/directory/

the configuration can be:

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

After making the change, verify the configuration:

sudo nginx -t

Then reload Nginx:

sudo systemctl reload nginx

Note: The exact configuration for a subdirectory deployment depends on the application’s document root and PHP-FPM setup.

Troubleshooting

Nginx Still Returns 404

Check the following:

  • Verify that the try_files directive is inside the correct server block.
  • Confirm that the Nginx document root points to the correct CodeIgniter application directory.
  • Verify that index.php exists in the expected location.
  • Check the Nginx error log.
sudo tail -f /var/log/nginx/error.log

PHP-FPM Is Not Working

Check the PHP-FPM service:

sudo systemctl status php8.2-fpm

Replace php8.2-fpm with the PHP-FPM version installed on your server.

You can also verify the PHP-FPM socket:

ls -l /run/php/

Nginx Configuration Test Fails

Run:

sudo nginx -t

Review the error message carefully and correct the configuration before reloading Nginx.

Static Files Are Not Loading

If CSS, JavaScript, images, or other static files return 404 errors, verify that:

  • The Nginx document root is correct.
  • The files exist in the expected directories.
  • File and directory permissions allow Nginx to read them.
  • The application is generating the correct asset URLs.

A basic CodeIgniter application configuration can look similar to:

server {
    listen 80;
    server_name example.com;

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

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
    }
}

Important: This is a basic example. Production environments should also include HTTPS, appropriate security headers, access controls, logging, and other server-hardening measures.

Conclusion

CodeIgniter 404 errors after migrating an application from Apache to Nginx are commonly related to URL routing and front-controller configuration.

Using the Nginx try_files directive allows requests that do not correspond to physical files or directories to be passed to CodeIgniter’s index.php.

The key configuration is:

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

After updating the configuration, always run nginx -t before reloading the service.

Frequently Asked Questions

1. Why does CodeIgniter work on Apache but return 404 on Nginx?

Apache and Nginx handle URL rewriting and routing differently. Nginx requires the appropriate try_files configuration to forward application routes to CodeIgniter’s front controller.

2. What does try_files do in Nginx?

The try_files directive checks whether the requested path corresponds to an existing file or directory. If neither exists, the request is forwarded to the specified fallback, which in this case is CodeIgniter’s index.php.

3. Do I need to restart Nginx after changing the configuration?

You should at least reload Nginx after making configuration changes:

sudo systemctl reload nginx

Always test the configuration first:

sudo nginx -t

4. Why is $query_string included in the configuration?

Using:

/index.php?$query_string

preserves the original query parameters when the request is forwarded to CodeIgniter.

5. Can the same configuration be used for CodeIgniter 4?

The general Nginx front-controller approach is similar, but CodeIgniter 4 typically uses the public directory as the web root. The Nginx document root and configuration should therefore be adjusted according to the CodeIgniter 4 deployment structure.

  • How to Install Nginx on Ubuntu
  • How to Configure PHP-FPM with Nginx
  • How to Deploy a PHP Application on Nginx
  • How to Configure SSL/TLS with Nginx
  • Nginx Server Security and Hardening Best Practices

How Pheonix Solutions Can Help

Pheonix Solutions provides web application development, PHP and CodeIgniter development, Linux server administration, Nginx configuration, cloud infrastructure, DevOps, CI/CD, security hardening, monitoring, and application deployment services.

Our team can help deploy, troubleshoot, optimize, secure, and maintain PHP applications across development, staging, and production environments.

Talk to our team:
https://pheonixsolutions.com/contact-us/

How to Fix CodeIgniter 404 Errors on Nginx

Date Posted: May 25, 2017
Last Updated: September 23, 2026

Introduction

When deploying a CodeIgniter application behind Nginx, you may encounter 404 Not Found errors when accessing application routes such as:

https://example.com/login
https://example.com/admin
https://example.com/dashboard

The application may work correctly when hosted on Apache but return 404 errors when deployed on Nginx.

This commonly occurs when Nginx is not configured to forward requests for CodeIgniter routes to the application’s index.php front controller.

This guide explains how to configure Nginx to correctly handle CodeIgniter routes and resolve these 404 errors.

Prerequisites

Before proceeding, ensure you have:

  • A CodeIgniter application deployed on an Ubuntu server
  • Nginx installed and running
  • PHP-FPM installed and configured
  • SSH or sudo access to the server
  • The CodeIgniter application deployed under the configured Nginx document root

Note: The exact Nginx configuration may vary depending on your CodeIgniter version, PHP version, application directory structure, and server setup.

Understanding the Problem

CodeIgniter uses a front controller architecture where application requests are generally processed through:

index.php

For example, a request such as:

https://example.com/dashboard

may not correspond to an actual /dashboard file or directory on the server.

If Nginx is configured to look only for physical files, it may return:

404 Not Found

Instead, Nginx needs to pass unmatched requests to CodeIgniter’s index.php.

Implementation

Step 1: Locate the Nginx Configuration

The default Nginx virtual host configuration is commonly located at:

/etc/nginx/sites-enabled/default

Open the configuration:

sudo vi /etc/nginx/sites-enabled/default

If your application uses a separate virtual host configuration, edit the configuration file associated with your domain instead.

For example:

/etc/nginx/sites-available/example.com

Step 2: Configure the location / Block

Add or update the location / block as follows:

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

The important part is:

try_files $uri $uri/ /index.php?$query_string;

This configuration tells Nginx to:

  1. Check whether the requested URI matches an existing file.
  2. Check whether the requested URI matches an existing directory.
  3. If neither exists, forward the request to CodeIgniter’s index.php.

This allows CodeIgniter to process routes such as:

/login
/admin
/dashboard

Step 3: Verify the PHP-FPM Configuration

Make sure your Nginx configuration contains a PHP location block that points to the correct PHP-FPM socket.

For example:

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.2-fpm.sock;
}

The PHP-FPM socket depends on the PHP version installed on your server.

Check the available PHP-FPM sockets with:

ls /run/php/

For example, you may see:

php8.1-fpm.sock
php8.2-fpm.sock

Use the socket corresponding to your installed PHP version.

Important: Do not blindly copy the PHP-FPM socket from this example. Verify the socket available on your server first.

Step 4: Verify the Nginx Configuration

Before restarting Nginx, always test the configuration:

sudo nginx -t

A successful configuration should return output similar to:

syntax is ok
test is successful

Step 5: Reload Nginx

If the configuration test succeeds, reload Nginx:

sudo systemctl reload nginx

A reload is generally preferred over a full restart because it applies the configuration without unnecessarily stopping the web server.

If required, you can restart Nginx:

sudo systemctl restart nginx

Step 6: Test the CodeIgniter Routes

Open your CodeIgniter application and test the routes that previously returned 404 errors.

For example:

https://example.com/login
https://example.com/admin
https://example.com/dashboard

The requests should now be routed through CodeIgniter’s index.php.

CodeIgniter Configuration

If Nginx routing is configured correctly but the application still generates unexpected URLs, verify the CodeIgniter configuration.

For CodeIgniter 3, check:

application/config/config.php

Set the base URL appropriately:

$config['base_url'] = 'https://example.com/';

If you are using clean URLs, you may also configure:

$config['index_page'] = '';

This allows URLs such as:

https://example.com/login

instead of:

https://example.com/index.php/login

Deploying CodeIgniter in a Subdirectory

If the CodeIgniter application is deployed under a subdirectory, the Nginx configuration must account for that path.

For example, if the application is available at:

https://example.com/directory/

the configuration can be:

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

After making the change, verify the configuration:

sudo nginx -t

Then reload Nginx:

sudo systemctl reload nginx

Note: The exact configuration for a subdirectory deployment depends on the application’s document root and PHP-FPM setup.

Troubleshooting

Nginx Still Returns 404

Check the following:

  • Verify that the try_files directive is inside the correct server block.
  • Confirm that the Nginx document root points to the correct CodeIgniter application directory.
  • Verify that index.php exists in the expected location.
  • Check the Nginx error log.
sudo tail -f /var/log/nginx/error.log

PHP-FPM Is Not Working

Check the PHP-FPM service:

sudo systemctl status php8.2-fpm

Replace php8.2-fpm with the PHP-FPM version installed on your server.

You can also verify the PHP-FPM socket:

ls -l /run/php/

Nginx Configuration Test Fails

Run:

sudo nginx -t

Review the error message carefully and correct the configuration before reloading Nginx.

Static Files Are Not Loading

If CSS, JavaScript, images, or other static files return 404 errors, verify that:

  • The Nginx document root is correct.
  • The files exist in the expected directories.
  • File and directory permissions allow Nginx to read them.
  • The application is generating the correct asset URLs.

A basic CodeIgniter application configuration can look similar to:

server {
    listen 80;
    server_name example.com;

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

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
    }
}

Important: This is a basic example. Production environments should also include HTTPS, appropriate security headers, access controls, logging, and other server-hardening measures.

Conclusion

CodeIgniter 404 errors after migrating an application from Apache to Nginx are commonly related to URL routing and front-controller configuration.

Using the Nginx try_files directive allows requests that do not correspond to physical files or directories to be passed to CodeIgniter’s index.php.

The key configuration is:

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

After updating the configuration, always run nginx -t before reloading the service.

Frequently Asked Questions

1. Why does CodeIgniter work on Apache but return 404 on Nginx?

Apache and Nginx handle URL rewriting and routing differently. Nginx requires the appropriate try_files configuration to forward application routes to CodeIgniter’s front controller.

2. What does try_files do in Nginx?

The try_files directive checks whether the requested path corresponds to an existing file or directory. If neither exists, the request is forwarded to the specified fallback, which in this case is CodeIgniter’s index.php.

3. Do I need to restart Nginx after changing the configuration?

You should at least reload Nginx after making configuration changes:

sudo systemctl reload nginx

Always test the configuration first:

sudo nginx -t

4. Why is $query_string included in the configuration?

Using:

/index.php?$query_string

preserves the original query parameters when the request is forwarded to CodeIgniter.

5. Can the same configuration be used for CodeIgniter 4?

The general Nginx front-controller approach is similar, but CodeIgniter 4 typically uses the public directory as the web root. The Nginx document root and configuration should therefore be adjusted according to the CodeIgniter 4 deployment structure.

Talk to our experts

Have a technology challenge or looking for the right solution for your business? Our team can help you with cloud, DevOps, development, infrastructure, design, and more. Feel free to reach out to our experts here.

admin

Our team has expertise across software and web development, WordPress, e-commerce, mobile applications, UI/UX, cloud and infrastructure, DevOps, CI/CD, API integration, security, testing, automation, and technical support. The team also works with AI-based software solutions, LLMs, AI workflows, AI agents, and intelligent application development to help businesses automate processes and build smarter digital solutions. We focus on developing, deploying, maintaining, and optimising secure, scalable, and reliable technology solutions while helping businesses adopt modern technologies and drive digital transformation.

Leave a Reply

Scroll to Top