Running regular html site alongside django nginx

March 17, 2016 862 views
Nginx Django

I've got multiple Django applications running on the same Nginx server . I'm using the following components:

  • Nginx
  • PostgreSQL
  • Virtualenv
  • Supervisor on a Debian (7.0) droplet

I would like to know how can I use Nginx to serve my static html webpage?

1 Answer

If you already have multiple Django apps running behind Nginx, you're most of the way there. In order to server a static site, you would just need another "server block" in your Nginx configuration. You'll also need to set up DNS records. By setting the server_name directive in each of your server blocks, Nginx will know where to direct requests.

Here's a bit of a simplified example:

upstream app_server_one {
    server 127.0.0.1:9000 fail_timeout=0;
}

upstream app_server_two {
    server 127.0.0.1:7000 fail_timeout=0;
}

server {
    listen 80 default_server;
    server_name: example.com;
    root /var/www/html;
    index index.html index.htm;

   # [snip...]
}

server {
    listen 80;
    server_name app1.example.com;

   # [snip...]

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server_one;
    }
}

server {
    listen 80;
    server_name app2.example.com;

   # [snip...]

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server_two;
    }
}

In the example, requests to example.com will serve static content from /var/www//html One to app1.example.com will serve a Django app listening on port 9000 and app2.example.com will serve the app listening on port 7000.

You can get more info on how to use Nginx server block in this tutorial:

It's based on Ubuntu 14.04, but it should point you in the right direction for Debian as well.

by Justin Ellingwood
Nginx is an incredibly powerful web server and reverse proxy. In this guide, we'll discuss how to set up server blocks on Ubuntu 14.04 in order to server multiple domains from a single server.
Have another answer? Share your knowledge.