Configure Nginx to proxy virtual hosts to Apache
-
You want to take advantage of nginx's speed serving static files, while making use of the .htaccess files bundled with applications such as Wordpress or Drupal, which contain Apache-specific configuration directives.
-
Fastcgi is too unstable for you. Fastcgi sometimes requires careful tuning to avoid things such as processes unexpectedly dying. Apache is quite good at managing processes. but you want to avoid the overhead of running the PHP (or or Ruby, or Python) interpreter to only serve static files.
Solution? Use nginx as a proxy in front of Apache. Match any static files with an nginx location directive and serve them directly. Pass all other requests to Apache.
Example configurations
We start with Apache and make it listen on a port other than 80 and only on the local address (so no one can access it directly from outside):
You do not need to have your static files in a separate directory. You can also use regular expressions to match static file extensions:
In addition to the proxy_pass directive, other directives allow you to pass various information about the original request to the upstream server for logging purposes or similar. Refer to the nginx documentation for the proxy module.
What about Apache name-based virtual hosts?
Since Apache sees requests as emanating from nginx, you need to use the proxy_set_header directive in nginx's config to tell Apache about which hostname was requested originally. You need an nginx virtual host corresponding to every Apache virtual host to match every incoming request and pass down a the appropriate hostname. For example, if you have this in your httpd.conf:
server { server_name fghrt.com; proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; }
Now you can enjoy the speed of nginx without translating every Apache configuration directive, and you can also let Apache manage the interpreter for the dynamic language of your choice.