Ok, so here are the relevant details for how I'm proxying to Apache through nginx.
I'm on ubuntu 9.04. Nginx isn't in the official repositories, but there is a recent version in Universe, so I edited /etc/apt/sources to enable those repositories. I'm running apache2 with mpm-prefork and their packaged php5 and apc modules. I'm sure you can get to the same starting point on other distributions with a little googling.
Starting from the default config, I disabled mod_deflate on Apache, since I want nginx to handle compression. I then enabled the rpaf module so that apache reads the headers nginx sets in order to find out the real client IP for logging, etc.
Next, I edited /etc/apache2/ports.conf to have Apache listen on port 8080, rather than port 80. I will probably go further and just have it listen on localhost.
Similarly, for each VirtualHost entry in /etc/apache2/sites-enabled/*, I set the listening port to 8080.
For nginx, starting with the provided config files:
/etc/nginx/nginx.conf
Code:
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;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
gzip on;
server_names_hash_bucket_size 64; # added this because some of my domain names were too long.
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
The nginx package for ubuntu follows the pattern used for apache (I think this approach comes from Debian) of breaking peices of config out into files and including whole directories, so I took advantage of this to break out my installation wide tweaks for compression and reverse proxying.
/etc/nginx/conf.d/gzip.conf
Code:
#by default, nginix only gzips text/html, and maybe text/plain
gzip_types text/html application/javascript application/x-javascript text/javascript text/css text/xml text/plain application/atom+xml;
gzip_min_length 512; #probably not that important, but it didn't seem worth even trying to gzip small responses.
/etc/nginx/conf.d/
Code:
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffering on;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
/etc/nginx/sites-enabled/mysite:
Code:
server {
listen 72.14.177.137:80; # your server's public IP address
server_name mysite.com www.mysite.com; # your domain name
access_log /var/log/nginx/mysite.access.log;
location / {
root /var/www/mysite; # absolute path to your WordPress installation
index index.php index.html index.htm;
# makes sure that all .php files are served by apache
if ($request_filename ~* "\.php$") {
break;
}
# this serves static files that exist without running other rewrite tests
if (-f $request_filename) {
#expires 30d;
break;
}
# this sends all non-existing file or directory requests to apache
if (!-e $request_filename) {
proxy_pass http://localhost:8080;
}
}
location ~ \.php$ {
proxy_pass http://local host:8080;
}
}
I derived this from someone elses config for fcgi that I found on line. I think it could probably be a little cleaner, but I was deliberately a little cautious.
That's pretty much it. Restart apache, start nginix and you should be good to go. You can check the response headers and the server log files to make sure requests are being handled as expected.
Oh, I did tweak the apache config a little more to reduce the maximum # of apache processes to 6 or 8 or so. I figured at least 4 so there could be one running on each core, and then some extras in case there were any waiting for I/O. Not too many though, because it wastes memory and leads to greater variance in response times.
I also ended up tweaking my apc configuration a little to give it more memory and to enable expiration of cached items (by default it would clear the whole cache if it filled).
There is more tweaking you can do. For example, you can set some cache header policies for both static and dynamic content, and nginx can also cache the output of dynamic pages and serve the cached copy without hitting apache.