Configure nginx on Ubuntu to forward Django server

Omasuaku
2 min readNov 5, 2024

To configure Nginx on Ubuntu to forward requests to a Django server as the default for your domain, follow these steps:

Credit (https://pragnakalp.com/wp-content/uploads/2024/04/Untitled.png)

Prerequisites

  • Ensure you have Nginx installed on your Ubuntu server. You can install it using:
sudo apt update
sudo apt install nginx
  • Make sure your Django application is running, typically on a port like 8000 or 8080

Step 1: Create a Nginx Server Block

Create a new configuration file for your domain in the sites-available directory:

sudo nano /etc/nginx/sites-available/yourdomain.com

Add the following configuration, adjusting the server_name, root, and proxy_pass directives as necessary:

server {
listen 80;
server_name yourdomain.com www.yourdomain.com;

location / {
proxy_pass http://127.0.0.1:8000; # Change port if your Django server runs on a different port
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_set_header X-Forwarded-Proto $scheme;
}
}

Step 2: Enable the Server Block

Create a symbolic link to enable the new site:

sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/

Step 3: Test Nginx Configuration

sudo nginx -t

If there are no errors, proceed to reload Nginx:

sudo systemctl reload nginx

Step 4: Configure Your Domain’s DNS Settings

Ensure that your domain’s DNS settings point to your server’s IP address. This usually involves setting an A record in your DNS provider’s control panel.

Step 5: Start Your Django Application

Make sure your Django application is running on the specified port (e.g., 8000). You can use a WSGI server like Gunicorn to run your Django app:

gunicorn --bind 127.0.0.1:8000 myproject.wsgi:application

Replace myproject with the name of your Django project.

Conclusion

After completing these steps, visiting http://yourdomain.com should forward requests to your Django application running on the specified port. Ensure that you have configured any necessary security settings, such as enabling HTTPS with Let's Encrypt if required.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Omasuaku
Omasuaku

Written by Omasuaku

Programmer from Africa. I’m here just to say a hello to the world.

No responses yet

Write a response