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

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
or8080
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.