Migrating WordPress Sites from Apache to Nginx: Everything You Need to Know
What is Apache Server?
Apache is a web server. Apache handles requests by creating a separate process or thread for each connection. Apache supports many modules for extra features. Apache processes dynamic content such as PHP or Python inside the server. Apache uses .htaccess files for directory-level configuration.
What is Nginx server?
Nginx is a web server. Nginx uses an event-driven model to handle many connections in a single process. Nginx serves static files quickly. Nginx relies on external processors like PHP-FPM for dynamic content. Nginx does not use .htaccess and only supports root-level configuration.
Key differences:
| Feature | Apache | Nginx |
|---|---|---|
| Architecture | Process/thread-based | Event-driven/asynchronous |
| Dynamic content | Handled internally | Sent to external processor |
| Static content | Less efficient | Very efficient |
| Config flexibility | Supports .htaccess | No directory configs |
| Modules | Load/unload at runtime | Set at build-time |
| Use case | Custom setups, dynamic sites | High traffic, static sites |
Apache is flexible and easy to customize. Nginx is fast and uses less memory. Many users combine Nginx and Apache to use both strengths.
How to Fix 404 Errors on Internal Pages?
When migrating a website from Apache to Nginx, 404 errors on internal pages usually occur because Nginx does not use Apache’s .htaccess files and rewrite rules. To fix this, you must manually add URL rewrite rules in the Nginx configuration.
Add WordPress Permalink Rewrite Rules in Nginx
In your Nginx server block configuration, add the following common WordPress rewrite settings, inside server { } enclosure:
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ .php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/php8.3-fpm.sock; # Adjust PHP version/socket path if needed
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
Together, these settings ensure:
- Static files or existing directories are served directly.
- Non-existent files are routed to WordPress’s
index.phpfor dynamic processing. - PHP files run through PHP-FPM to execute code and generate content.
This setup replaces Apache’s .htaccess functionality with equivalent Nginx rules and enables WordPress permalinks and PHP processing on the Nginx server.
Reload or Restart Nginx [Optional]
After updating the config file, reload or restart Nginx to apply changes:
sudo nginx -t sudo systemctl reload nginx
The first command tests the configuration, the second reloads safely.
Check Other Rewrite Rules
If your Apache .htaccess had other rules (redirects, security), manually translate those to Nginx format in your server block.
Clear Cache and Browser History
Clear any server-side cache (like caching plugins or Nginx cache) and clear browser cache to see changes.
By correctly configuring URL rewrites in Nginx, you will resolve the 404 errors and ensure all internal pages display properly after migration from Apache.
