NGINX
From Docunext Technology Wiki
Contents |
Summary
Nginx is a lightweight but very capable web server. It looks like a good candidate to me as it is fast, supports FastCGI, and can act as a reverse proxy.
Some highlights I've noted while using NGINX over the past several months:
- It can handle tons of keepalive (HTTP 1.1) connections while using very little memory.
- It uses HTTP 1.0 for proxy connections to backend servers, so there is no point to supporting keepalive on backend servers.
- It only compresses text/html by default, so I add "gzip_types text/plain application/xml application/x-javascript;"
Example Configuration File
user www-data;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
sendfile on;
keepalive_timeout 65;
tcp_nodelay on;
gzip on;
include /etc/nginx/sites-enabled/*;
}
Initial Impressions
I like it so far. Setting up FastCGI for PHP isn't completely straightforward, but eventually I was able to get it going. I used a basic bash script to run php5-cgi, but I'm also interested in the spawn-fcgi program shipped with lighttpd.
NGINX FastCGI Init Script for PHP5
In-Depth Review
I really like NGINX. It gives me the impression that it is extremely efficient in every way. Bare bones is not the right description though, it has many truly excellent features. The one thing I miss is digest authentication, but its possible that may be included at a later date.
Things I like about NGINX
- No CGI support - yes I like this lack of a feature.
- XSLT support - such a good implementation too!
- FastCGI support - he did this very well too.
- Configuration file syntax - reminds me of varnish configuration files, almost like a programming language.
- TLS / SSL Module has support for the VIA Padlock engine.
Things I miss in NGINX
- HTTP Digest authentication (it is possible to use Apache as a digest authenticating proxy).
- I really miss digest authentication in NGINX, so I'm starting some notes on the possibility of implementing. NOT NGINX Digest Authentication Only Notes
Things I'm looking forward to with NGINX
- Writing my own module - I'd like to write a module which will dump the contents of some POST requests to a file. My First NGINX Module - UPDATE: I'm using DAV for this.
- Using the email proxies. UPDATE - Finally getting around to this: NGINX Mail
- I gotta try out the memcached module - requires something to put data in.
- I want to check out the DAV module, looks interesting. - I have checked it out and it works great.
- Using the embedded Perl module. Kind of like mod_perl?
- I'd love to see Ruby, lua or Tcl embedded into NGINX.
- I still want to write a filter module, specifically one that leverages discount as a filter.
- See what all the fuss is about when combining NGINX with mongrel, Rack or another Ruby web application framework. See Unicorn and Ruby Unicorn.
Things I'm actually doing with NGINX
- The FastCGI implementation works very well so I'm mainly writing FastCGI applications in C. Although I was looking forward to trying to write an NGINX filter module, I decided to hold off after scanning through the module writing guide and reading that the filter modules are an advanced task. I need to get more experience with C before I tackle that. Its not NGINX specific work, but doing it in NGINX is working out great. For filters like ImageMagick resizing and exif, I'll use Apache's mod_ext_filter for now. I may also use neon to create a FastCGI proxy and then process the data stream that way, which is almost like a filter. (LibXML2 also has ah HTTP client.)
- Lot's of XSLT work - its a very good implementation.
- Redirects seem to work fine, so I'm experimenting with large redirect configurations. I need to try the map module instead of the numerous if statements I'm currently using. Towards that end, I've written some NGINX focused XSL stylesheets for YoDNSConf which export configuration data in NGINX format.
- Using DAV in interesting new (to me at least) ways - as a CMS via a JQuery PUT method. Again not NGINX specific, but it works great. I'm probably going to switch to Apache's mod_dav because it has such a great interface to Subversion.
- Also using DAV as a non-scripted mail gateway. With the JQuery PUT method, bsd-mailx, and an incron table, form mail is very very easy! See also Jquery-form2xml.
- I might still try the filter module, using the mod_strip as an example and linking with the discount library.
- Gzip - I use NGINX as a gzip proxy as etags for filtered content are apparently messed up in Apache 2.2:
gzip on; gzip_http_version 1.0; gzip_comp_level 2; gzip_proxied any; gzip_types text/plain application/xhtml+xml text/css application/x-javascript application/javascript text/xml application/xml application/xml+rss text/javascript;
Troubleshooting
Ran into this problem with long domain names.
server_names_hash_bucket_size 128;
- Stub Status
This is a simple, but useful module. Here is a sample output:
Active connections: 3 server accepts handled requests 257 257 365 Reading: 0 Writing: 1 Waiting: 2
Epoll
Problems with epoll? I'm getting this on an OpenVZ system:
2010/03/20 09:17:03 [alert] 18972#0: epoll_wait() failed (22: Invalid argument) 2010/03/20 09:17:03 [alert] 18972#0: epoll_wait() failed (22: Invalid argument) 2010/03/20 09:17:03 [alert] 18972#0: epoll_wait() failed (22: Invalid argument) 2010/03/20 09:17:03 [alert] 18972#0: epoll_wait() failed (22: Invalid argument) 2010/03/20 09:17:03 [alert] 18972#0: epoll_wait() failed (22: Invalid argument)
Possibly because the system is overloaded?
Proxy Headers Gotcha
I was thrown for a loop today when I was trying to clean up my NGINX configuration files! I consolidated the proxy_set_headers Host $host lines into the server section instead of the location section. It worked at first, but then the higher level header setting stopped working when I added proxy_set_headers Cookie "" to a lower level section.
In a nutshell, all the proxy_set_headers have to be all the same, or repeated for every location, even if the other headers stay the same.
NGINX Tips
- The spawn-fcgi binary from lighttpd is very helpful for setting up fastcgi daemons for use with NGINX.
- The xslt module works really well.
- I think that qDecoder is the perfect companion for NGINX.
- NGINX Failover Setup
NGINX Modules
Official Modules
- NGINX Proxy Module
- NGINX Addition Module
- NGINX xslt module
- NGINX Rewrite Module
- NGINX Perl Module
- NGINX Image Filter
- NGINX Gzip Precompression Module
- NGINX SSI Module
- NGINX Geo
Third Party Modules
- NGINX ncache module
- NGINX parsed vars module
- NGINX token module
- NGINX Circle GIF Module
- NGINX V8 Module
- NGINX Passenger
- NGINX HTTP Push Module
NGINX FastCGI Recipes
See Also
External Links
- http://blog.kovyrin.net/2006/05/30/nginx-php-fastcgi-howto/
- http://xzdev.com/nginx_fastcgi.html - fastcgi in c, looks interesting. Does it include some basic socket code as well?
- http://mikewest.org/2008/11/generating-etags-for-static-content-using-nginx
- Backup of NGINX Git repository at the Docunext Mirrors
- NGINX cross reference at the Docunext Mirrors