Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

APache is installed and is used to serve mostly Djanfo/Python content. I want to install nginx to serve static content. How to do it and what is agood configuration.

I am on ubuntu and Apache was installed via apt-get.

share|improve this question
up vote 2 down vote accepted
  1. first option: do you have few ip addreses? bind apache to one of them, nginx to another.

    in debian world

    for apache in /etc/apache2/ports.conf include:


    in vhost file in /etc/apache2/sites-enabled change:


    for nginx in /etc/nginx/sites-enabled/ add:

  2. second option: move apache to listen on different port [ files as above. tell apache to listen on for instance ] and instruct nginx to reverse-proxy dynamic traffic to your apache while serving /static/ by nginx - for instance:

    server {
     server_name another.dname;
     access_log  /var/log/nginx/something-access.log;
     location / {
      proxy_pass http://localhost:8080;
      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;
     location ~* ^.+\.(jpg|js|jpeg|png)$ {
      root /some/where/on/your/disks;
    ; put your static hosting config here.

btw - think about putting static content on another domain. this should improve end-user speed a little.

share|improve this answer

There are many variations on how to accomplish this. I prefer to have one application dedicated to deciding on which server handles what kind of content, while the back end servers simply serve the files they've been requested.

To that end, I employ the Varnish reverse proxy on the front end, listening on port 80. Behind that, I have Apache (port 8880) and nginx (port 8881), both configured for the same domains and pointing to the same directory structure. In my Varnish config file, I have something like this:

backend apache {
  .host = "";
  .port = "8880";
backend nginx {
  .host = "";
  .port = "8881";


if (req.url ~ "\.(png|gif|jpg|ico|html|htm|js|css|txt)$") {
  set req.backend = nginx;
} else {
  set req.backend = apache;

Of course, there's a little more to it, but you get the idea.

Since you already have Apache and nginx installed, you may want to browse this link, which describes a very simliar situation to yours, but uses nginx as the front end for static content and then passes on requests to Apache.

If you want to go really simple, you could simply use a reverse caching proxy (such as Varnish or nginx) in front of Apache. What it will do is cache requests to quickly serve them out to clients, while at the same time relieving the web server itself from serving identical requests. This, by its very nature, will give the same effect as what you're asking for. Since static pages and images rarely change, they will be almost always cached by the front end, whereas dynamic pages will be detected as such and always passed on to the back end.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.