Is Nginx + php-fpm Supposed to Be Much Faster Than Apache + mod-php?
You likely have seen the classic performance charts comparing web stacks. They almost invariably show Nginx handling concurrency far better than Apache. The logic is sound: Nginx uses an event-driven, asynchronous model, while traditional Apache (specifically mpem or prefork) relies on one thread per connection. When you hit a limit of 100 concurrent users, Apache threads often starve each other, leading to the massive spikes in latency seen in benchmark graphs like the ones from Evan Schwartz.
However, real-world results tell a more nuanced story. In your specific test using Nginx 1.0.15 and Apache 2.2.15 with PHP 5.4.38, you observed only marginal gains. You configured Nginx workers to match CPU cores, yet the latency graphs for login submissions and home page access remained largely flat compared to your mod_php setup. This discrepancy often stems from a fundamental misunderstanding of what each architecture actually handles.
The Static vs. Dynamic Bottleneck
The primary reason Nginx is touted as faster is its efficiency serving static contentâimages, CSS, and JavaScript files. Because it uses a single thread to handle thousands of connections via epoll (Linux) or kqueue (BSD), the overhead of spawning new threads for every request is non-existent. Apache, conversely, creates a new thread per connection in its default configurations.
When you mix Nginx with PHP-FPM, you are introducing a bottleneck that negates Nginx's concurrency advantages. The request flow looks like this:
- Nginx accepts the connection asynchronously (very fast).
- Nginx spawns a worker process to handle the specific request.
- That worker forks a child PHP-FPM master to execute the script.
At step 4, the Nginx worker must wait for the PHP-FPM process to finish. This is a synchronous operation. If your PHP scripts are computationally heavy (e.g., complex database queries or image processing), both Apache with mod_php and Nginx with PHP-FPM will hit that same CPU-bound ceiling. In this scenario, Nginx's asynchronous nature offers no speedup because the execution model is effectively synchronous once the dynamic content handler is involved.
Your test results confirm this. If your "heavy backend" processing dominates the request time, optimizing the static file serving pipeline won't reduce the total latency significantly. The graphs you generated showing similar latencies for login and home page access suggest that the PHP execution time was the dominant factor, masking any upstream efficiency gains Nginx might have offered during the initial handshake.
Configuration Nuances and Resource Utilization
You noted that server resource utilization didn't differ much between the two stacks. This is expected if both are CPU-bound by PHP scripts. However, there are specific configuration areas where you can still gain ground on Nginx without switching to a different execution model.
If you want to extract more performance from Nginx + PHP-FPM, focus on tuning the PHP-FPM pool rather than just the Nginx worker count. Increasing pm.max_children in php-fpm.conf allows more concurrent PHP processes if your server has sufficient RAM. Conversely, if memory is tight, you might see the opposite problem where Apache's thread pool handles short-lived requests slightly better than a bloated PHP-FPM pool.
Another critical area is timeout handling. Nginx's default timeouts (proxy_connect_timeout, proxy_read_timeout) can sometimes cause issues with slow PHP scripts that don't send output for a long time, leading to premature connection drops. Adjusting these values in your Nginx config ensures you aren't artificially limiting performance due to premature disconnections.
Furthermore, ensure you are not hitting the pm.max_requests limit too quickly. Recycling PHP-FPM workers after handling a certain number of requests prevents memory leaks from accumulating over time, which can degrade performance under load. Setting this to 500 or 1000 is a standard best practice.
When to Switch Stacks
Is Nginx recommended for heavy server operations? The answer depends on your bottleneck. If your site is limited by disk I/O or CPU during PHP execution, switching from mod_php to PHP-FPM is usually the right move regardless of whether you use Apache or Nginx as the reverse proxy. However, if you are strictly comparing Nginx vs. Apache with the same backend processing model (i.e., using PHP-FPM for both), the raw speed difference might indeed be negligible for dynamic content generation.
The massive concurrency graphs often cited online usually assume that static file caching is handled efficiently by the server stack, which allows the event loop to shine. If your application logic is heavy, the theoretical advantage of Nginx's event loop diminishes because you are waiting on CPU-bound tasks anyway.
If you require a highly optimized stack for a high-traffic PHP application, consider looking into modern PHP execution environments like Swoole or Roadrunner, which allow for true asynchronous processing within PHP itself, bypassing the synchronous blocking issue entirely. For standard Laravel applications, leveraging Nginx's superior static caching and compression settings remains a valid strategy to reduce backend load, even if the raw PHP execution speed remains comparable.