Notes‎ > ‎

Nginx


About Nginx

Pronounce "Engine X"

Originally created in 2004 by a guy in Russia to power the second-largest site in Russia: www.rambler.ru

Nginx hosts 7.50% (20.5M) of all domains worldwide and is one of a handful of servers written to address the C10K problem.

Nginx doesn't rely on threads to handle requests. Instead it uses a much more scalable event-driven (asynchronous) architecture. This architecture uses small, but more importantly, predictable amounts of memory under load.

Nginx scales in all directions: from the smallest VPS all the way up to clusters of servers.


Setup

PHP-FPM (PHP FastCGI Process Manager)

Very secure conf

Conf variables


Override PHP defaults

/etc/php-fpm.d/www.conf

php_admin_* means it cannot be overridden in PHP

eg

php_value['date.timezone'] = America/Vancouver

php_admin_value[upload_max_filesize] = 10M

php_flag[display_errors] = off
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on

php_admin_value[memory_limit] = 384M

php_value['xcache.var_count'] = 12
php_value['xcache.var_size'] = 24M

Setup GD

yum install php-gd
Say Yes and let it update PHP and all dependencies

Server modules


Out of Memory

The default php is too memory-hungry

php-fpm (used to handle php scripts in a nginx setup) wants too much memory.

The default setting uses upto 6gb, which causes out of memory errors.

Suggestions
  • PHP defaults to a max of 128MB of memory per instance - reduce that to 64mb
  • Changed the default max of 50 php-fpm processes to 20.
50 x 128MB is where the 6gb comes from.  With these suggestions, it will use just over 1gb.

(A lot of people complaining about this very same problem with regards to php-fpm / nginx default setup)

Also set it up to restart a child process if it has served more then 500 requests since that child process was started.

Redirect

server
{
    listen   80;
    server_name  domain1.com www.domain1.com;
    rewrite ^/(.*) http://domain2.com/$1 permanent;
}


WordPress

WordPress quick setup

WordPress setup

WordPress Nginx proxy cache integrator

Installing apc (or another opcode cache) and - alternatively - a wordpress object cache. Definitely install an opcode cache - leave the object cache for later. You may find you don't need it!

nginx Compatibility

WP  Super Cache and Sitemaps


MediaWiki



GeoIP


http {
    geoip_country  /usr/share/GeoIP/GeoIP.dat; # country IP database
#   geoip_city     /usr/share/GeoIP/GeoLiteCity.dat; # city IP database (takes more RAM, use only if necessary)
}

server {
  server_name play-runesofmagic.tgn.tv *.play-runesofmagic.tgn.tv;

if ($geoip_country_code ~ US|CA|MX) {
 rewrite ^/(.*) http://affiliate.account.frogster-america.com/1/ROM-US/en/tgn/0/;
}

if ($geoip_country_code !~ US|CA|MX) {
 rewrite ^/(.*) http://affiliate.account.yusho.de/1/ROM/en/rev_tgn/0/;
}
}


Password protection

Docs

Steps

1. htpasswd -cd [password file] [username]
(prompt you for a password)

2. edit nginx .conf file

auth_basic            "Restricted";
auth_basic_user_file  /path/to/passwords/file;

3. set permissions on password file so nginx can read it
chown -R nginx:nginx [web folder]

Load time comparison vs Apache

Feb, 2011

Empty CentOS Linux server running only one website with default settings for the latest stable versions of both web servers. Exactly the same hardware, database and network, the only difference is the web server.

Nginx was twice as fast as Apache for 30 active users!

Nginx with WordPress 3.1 and static page caching

Few more tweaks

500% faster than default Nginx configuration!

20110418
^-- 1.25 sec across the board!

20110420 - with CloudFlare.com
^-- 0.8 sec across the board!

20110825 - BuddyPress social network
1.16 1.19 1.25 1.26 1.36

Comments