FeaturesWP CloudPricingEE CLIContactMy Account
Features
Team Access & User Permissions
Install
Resources
Community
CLEAR
Tutorials
Nginx
Optimizing Nginx Configuration
Topics
On this page
-
- Getting Started
- Git and File-System Layout (non-default setup)
- Version Constraints
- Managing Themes and Plugins (for entire WordPress site)
- Updating Themes and Plugins (for entire WordPress site)
- Adding Composer Support to Your Own Themes and Plugins (for developers)
- Using Composer to Manage Own Theme/Plugin Dependencies (for developers)
-
- Assigning Multiple IP Addresses to Single LAN Card
- Disable IPv6 on Ubuntu 12.04
- dsh – distributed shell
- fdupes – find & replace duplicate files with hardlinks
- GPG Keys Cheatsheet
- Increase "Open Files Limit"
- kill all php, nginx, mysql or any kind of processes
- Passwordless Authentication for SSH
- Screen
- search replace in multiple files useing grep xargs sed
- Setup sftp
- sysctl.conf
- Timezone Sync
- Using Google Public DNS on Ubuntu Server
- Using openssl to match private key, cerificate and CSR
- Configure Postfix to Use Gmail SMTP on Ubuntu
- Logrotate Example for Custom Logs
- PHP 5.5, MySQL, Postfix, Nginx and WordPress on Ubuntu
- Serverdensity
- Upgrade Ubuntu 10.04 to 12.04
- Using find and sed to replace strings in multiple files
- Webmin
-
- .my.cnf – mysql user & password
- Analyse slow-query-log using Anemometer
- Improving MySQL Query Cache
- MySQL Query Profiling
- Reset MySQL root password
- tuning-primer.sh – an alternative for mysqltuner
- Using MariaDB 10
- Install/Upgrade to MySQL 5.6 on Ubuntu 12.04 LTS
- Change WordPress Domain Name
- Character Sets and Collations
- Enable innodb_file_per_table
- Enable Remote Access (Grant)
- Installing Percona Toolkit
- Using tmpfs for temporary folder creation
- YARPP and InnoDB
- Convert MyISAM to InnoDB
- Using tmpfs for mysqldump & mysqlimport
- Convert from INNODB to MYISAM
- Analyse slow-query-log using mysqldumpslow & pt-query-digest
- mysqlcheck with cron to optimize automatically
- Using MySQLTuner to Optimize MySQL configuration
-
-
- Directly connect to PHP-FPM
- Generating core-dump for php5-fpm
- GeoIP
- Having php5 and php7 on same system.
- Install Xdebug and configure it with webgrind
- Installation of PHP Code Review Tools
- memcache
- PHP's Zend Opcache Config & Web Viewer
- Setting Sqlite support with PHP
- Using HHVM with PHP-FPM Fallback
- Using Redis for PHP Sessions on Ubuntu Server
- Installing PhpMyAdmin Quickly
- Moving PHP's session storage to tmpfs
- Install Xdebug and configure it with Netbeans
- Nginx – Enable PHP-FPM Status Page
- PHP-FPM: Socket vs TCP/IP and sysctl tweaking
- APC Cache Optimization & Monitoring Using Web Interface
- Checking if PHP/WordPress can send mails
- Debugging PHP Scripts Using slow_log and more
- Increase file upload size limit in PHP-Nginx
- Increase PHP script execution time with Nginx
-
- Performance Optimization
- Remove Query string of css/js files
- Setup ElasticSearch with ElasticPress
- Using Redis for WordPress Object-Cache
- WooCommerce Window Shopping Caching Technique
- Moving WordPress To New Server (Faster)
- Better wp-cron using linux's crontab
- 404 error- Page not Found!!!
- Debugging WordPress with Nginx
worker_processes
worker_connections
worker_rlimit_nofile
keepalive_timeout
Rarely Used
server_names_hash_bucket_size
server_names_hash_max_size
WordPress Multi-site Subdomain/Domain-Mapping & server_name hashing
More…
Optimizing Nginx Configuration

Nginx needs least tweaking as compared to PHP & MySQL.
Still to get the most out of Nginx, you can tweak few directives in /etc/nginx/nginx.conf
worker_processes
This is very important. It controls number of worker processes Nginx is running.
Set worker_processes = number of processors in your system.
To find out how many processors you have on your server, run the following command:
grep processor /proc/cpuinfo | wc -l
It will display a number only:
32
Set that number as value forworker_processes directive.
worker_connections
If you are running very high traffic sites, its better to increase value of worker_connections. Default is 768.
Theoretically, nginx can handle max clients = worker_processes * worker_connections
We use worker_connections = 10240
worker_rlimit_nofile
Increase number of files opened byworker_process.
This directive is not present by default. You can add it in /etc/nginx/nginx.conf in main section (belowworker_processes)
We use worker_rlimit_nofile 100000;
keepalive_timeout
I have come across many definitions for this. Apart from traffic, response time of FASTCGI backed application is a factor to be considered while tweaking this directive. Its default is 65 or 75 seconds.
We use keepalive_timeout = 30s
Rarely Used
Following two directives need tweaking in rarely. When the time is right, Nginx, itself, will tell you to make a change! 😉
Nginx will throw a friendly error like below:
Reloading nginx configuration: nginx: [emerg] could not build the server_names_hash, you should increase either server_names_hash_max_size: 512 or server_names_hash_bucket_size: 64
server_names_hash_bucket_size
When you have a domain name longer than 64-chars, you will need it.
By default, its value is 64. If you have a domain 80-chars wide, do not set it to 80. Instead use next value of multiple of 2. That means 128, 256 and so on.
server_names_hash_max_size
Its default value is 512. If you are hosting hundreds of sites on your server.
Nginx suggests, you can change eitherserver_names_hash_max_size orserver_names_hash_bucket_size to accomodate large number of sites, but I prefer keepingserver_names_hash_bucket_size as it is and makingserver_names_hash_max_size big in multiple of 2’s till error disappears.
On a server, where we host 300+ sites, we needed to change it to 8192!
I used a trick to find out correct size by using following command:
ls /etc/nginx/sites-available/ | wc -c
Above command list of enabled sites’ name which I pass towc command to get number of total characters allserver_name are using together. In my case, above command returned 7414, for which next 2’s multiple value is 8192.
In each site config, I was using only 1server_name without any wildcard or regex. I guess wildcard & regex will also affect value of server_names_hash_max_size .
WordPress Multi-site Subdomain/Domain-Mapping & server_name hashing
As you know, you can host millions of subdomains/domain-mapping using a WordPress multisite setup. But as far as my experience goes, it doesn’t put any load on Nginx’s server_name hashing.
More…
Nginx config for www to non-www and non-www to www redirection
PREVIOUS
Rewrite Rules for vBulletin SEO-friendly permalinks
NEXT
Comments
-
asiou
thanks whatever is desrcibed in article i have already done but still my cpu load is above 1.15+ and around 250members active at any time. any suggetion?
-
Try using object-cache/database-cache with memcache backend. External link to rtcamp.comhttps://rtcamp.com/tutorials/php/memcache/
-
-
Saiful Alam
Hi,
Nice article. I have a locally hosted site available only locally. Our site is a media site and contains mostly games and videos which we deliver through nginx web server. These files are hosted in a local hadoop cluster managed by cloudera. This webserver mounts the hdfs in fuse mode and is connected by 2 giga bonded ethernets. And the webserver delivers to users through another 2 giga bonded interfaces.Our problem is that nginx is very very slow. Can you advise us something on this?
Thanks
Saiful-
There are many things that could go wrong in your setup.
Replicate same setup in isolated environment to verify if issue is with nginx only.
Test with a large file directly to verify if issue is not with hadoop-cluster.Finally on nginx end – External link to calomel.orghttps://calomel.org/nginx.html (mainly sendfile off, enable aio and set large output buffers)
-
-
Ross
Hi.
I found I had to increase my server_names_hash_bucket_size to 64 when I added a third site (a subdomain) to my list of sites available. The site name length was only 4+7+4 characters. Does that seem right?
-
Doesn’t feel intuitive but it happened with me many times.
May be you can read where it was discussed at length – External link to github.comhttps://github.com/rtCamp/easyengine/issues/27
-
-
Pradeep
we use keepalive on our nginx.conf, but for some reason it seems to be having a detrimental effect on TTFB. The Force SSL file in nginx does a return 301 to the HTTPS version of teh site. Once the browser gets a 301 (with a Keepalive) it waits for bit before making the next request, and this causes high TTFB.
Features
- Server Setup & Management
- Site Creation & Management
- Create WP Cloud Sites
- Speed & Caching
- Firewall Protection
- Backups & Recovery
- Logs & Monitoring
- Deployments & Developer Tools
- Team Access & User Permissions
Useful Links
Product
Other Products
Subscribe for curated product updates, tips, and announcements.
"*" indicates required fields
This field is for validation purposes and should be left unchanged.
Email*
SIGN ME UP
Δ
© rtCamp, since 2009. All rights reserved.