Cons of nginx:
- Limited modules
- everything has to code when compare to apache. (nightmare for new server admins)
- static modules (deploy only once while installing the server)
- not handles dynamic content it self (its a pros)
Pros of Nginx
- less cpu & memory required to handle 1000+ concurrent connection (existed to address apache problems)
Why nginx created in 2004 2 version open source and premium nginx plus
Apache core architecture
mpm-prefork: process with 1 thread to handle 1 connection 1 process = (126+MB memory just on limit in php )
mpm-worker: process with 2 threads thread for 1 connection handling 2 connections by 1 process. but keep alive connections (idle which pages active in browser but no request from user also count as active connection holds some memory)
mpm-event: only uses threads for active connections. keep alive connection handles separately.
you have to select best for your server or websites need.
Note: we can’t limit apache process memory limit not like php or php-fpm but we can do it by limiting processes or threads)
//traffic = visitors >> connection. 100 real time visitors = not 100 active connections to db,php server proxy.
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 150
MaxRequestsPerChild 0
</IfModule>
<IfModule mpm_worker_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxClients 150
MaxRequestsPerChild 0
</IfModule>
<IfModule mpm_event_module>
StartServers 2
MaxClients 150
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestsPerChild 0
</IfModule>
(based on above php -fpm pool manger set as on demand,dynamic,static in nginx)
Nginx core architecture
Nginx workers equal to cpu cores
user www www; ## Default: nobody worker_processes auto; ## Default: error_log logs/error.log; pid logs/nginx.pid; worker_rlimit_nofile 8192; events { worker_connections 4096; ## Default: 1024 }
nginx workers auto =2
worker process = 1024 default
2*1024=2048 concurrent connections per seconds or 2 vcore server*big deal
single-threaded and processes use an asynchronous, non-blocking, event-driven connection handling algorithm.
not spawn process based on connection (every process = memory cpu holding)
2 process in nginx based on cores.
connection handling:
connections handled by the worker are placed within the event loop where they exist with other connections. Within the loop, events are processed asynchronously, allowing work to be handled in a non-blocking manner. When the connection closes, it is removed from the loop.
why people use nginx as proxy for apache?
nginx passes the connection to php-fpm then database
and waits until the connection process.
modules: apache modules easy install and configure dynamically and lot of features.
nginx modules can de deployer only once as static. however nginx introduced dynamic module
feature but it has to deploy with again fresh nginx installation(install fresh nginx with new module as dynamic in same /live or another server with same os and version of nginx then copy the module file into production server)
.htaccess vs nginx server block
nginx does not allow to overwrite rules every directory. so problems in shared hosting, (memory limit, compression,ssl etc)
cpanel only works with apache.
file system vs uri (request interpretation)
apache processes to file system while nginx to url
The try_files directive commonly uses the $uri variable, which represents the part of the URL after the domain name
and its new 2004 vs 1995 apache
how litespeed faster than nginx and nginx plus
Litespeed released on 2003 in enterprise version its got popular when its released 2014 openlitespeed free version
& 2017 litespeed cache plugin for wordpress.
same architecture as nginx. built in litespeed cache. while windows IIS server not free at all.
also know how to tune nginx performance