Optimizing PHP with Clean URLs on Nginx: A Guide for Open Source CMS

Learn how to configure Nginx for PHP-based CMS like Drupal, WordPress, and Joomla to achieve clean, SEO-friendly URLs without compromising performance.

As an open source enthusiast and indie developer, I’ve often grappled with the challenge of configuring web servers for optimal performance and SEO. Today, I’m excited to share a powerful Nginx configuration that elegantly solves the clean URL conundrum for PHP-based content management systems (CMS) like Drupal, WordPress, and Joomla.

Why Clean URLs Matter

Before diving into the configuration, let’s briefly discuss why clean URLs are crucial:

  1. Improved SEO: Search engines prefer human-readable URLs.
  2. Enhanced User Experience: Clean URLs are easier to remember and share.
  3. Professionalism: They give your site a more polished, professional appearance.

The Nginx Configuration Solution

Here’s the Nginx server block that achieves our goal:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
server {
    listen 80;
    server_name www.domain.com;
    index index.html index.htm index.php;
    root /path/to/domain/files;

    location / {
        error_page 404 = //e/index.php?q=$uri;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /path/to/domain/files$fastcgi_script_name;
    }

    access_log /usr/local/nginx/logs/domain.access_log;
    error_log /usr/local/nginx/logs/domain.error_log;
}

Breaking Down the Configuration

Let’s analyze the key components:

  1. Server Block: Defines the basic server settings, including the domain and root directory.

  2. Location Block for Clean URLs:

    1
    2
    3
    
    location / {
        error_page 404 = //e/index.php?q=$uri;
    }
    

    This clever trick redirects 404 errors to your CMS’s index.php, allowing it to handle clean URLs.

  3. PHP Processing:

    1
    2
    3
    4
    5
    6
    
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /path/to/domain/files$fastcgi_script_name;
    }
    

    Configures FastCGI to process PHP files efficiently.

  4. Logging: Sets up access and error logs for monitoring and troubleshooting.

Implementation Tips

  1. Replace www.domain.com with your actual domain.
  2. Adjust /path/to/domain/files to match your site’s root directory.
  3. Ensure your PHP-FPM is configured to listen on 127.0.0.1:9000.

Conclusion

This configuration offers a streamlined approach to implementing clean URLs for PHP-based CMS on Nginx. It’s a testament to the power and flexibility of open source solutions, allowing developers to create robust, SEO-friendly websites without complex setups.

As you implement this solution, remember that the world of web development is constantly evolving. Stay curious, keep experimenting, and don’t hesitate to share your own innovations with the community. Happy coding!

Writing about the internet