Hi i am triying to intall 3 Projects in my droplet with nginx and gunicorn, i have 2 projects created with django and one with only html (a static website), i could install the static website listen the 80 port, also, i could put one django project but listen 8082 port, because when i put it to listen the 80 port only i could see the static website, so my question is, i have to change the port each time i going to install a new project or web site? and how i can install 2 django projects In a same droplet ?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
In order to have more than one Django app listening on port 80, you’ll need to allow Nginx to direct requests to the correct app. You’ll also need to set up DNS records. By setting the server_name directive in each of your server blocks, Nginx will know what to serve.
Here’s 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:
And on using Django with Nginx:
Heya,
I just stumbled upon this question, here is a full answer on how to achieve that from the very beginning.
Running multiple Django projects (or any projects) on a single droplet using Nginx and Gunicorn is a common use case. You don’t necessarily need to change the port for each project. Instead, you should utilize server blocks (virtual hosts) in Nginx to manage multiple sites. Here’s a step-by-step approach:
Each Django project needs its own Gunicorn instance, preferably managed by a system service like systemd.
For example:
gunicorn project1.wsgi:application --bind 127.0.0.1:8001
gunicorn project2.wsgi:application --bind 127.0.0.1:8002
These commands start the Gunicorn server for two Django projects on ports 8001 and 8002 respectively.
Nginx acts as a reverse proxy, directing incoming requests to the appropriate Gunicorn instance based on the domain name or the subdomain.
For example:
For the static website (/etc/nginx/sites-available/staticsite):
server {
listen 80;
server_name staticsite.com;
location / {
root /path/to/static/site/files;
}
}
For the first Django project (/etc/nginx/sites-available/project1):
server {
listen 80;
server_name project1.com;
location / {
proxy_pass http://127.0.0.1:8001;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
For the second Django project (/etc/nginx/sites-available/project2):
server {
listen 80;
server_name project2.com;
location / {
proxy_pass http://127.0.0.1:8002;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
After creating these configurations in the sites-available directory, enable them by creating symbolic links in the sites-enabled directory:
sudo ln -s /etc/nginx/sites-available/staticsite /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/project1 /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/project2 /etc/nginx/sites-enabled/
After setting up the configurations and enabling them, reload Nginx to apply the changes:
sudo systemctl reload nginx
Ensure that the domain names (staticsite.com, project1.com, project2.com in this example) point to the IP address of your droplet. If you’re using subdomains, set them up accordingly.
With this setup:
staticsite.com will be served by Nginx directly from static files.project1.com will be forwarded to the Gunicorn instance running the first Django project on port 8001.project2.com will be forwarded to the Gunicorn instance running the second Django project on port 8002.This way, you can run multiple projects on a single droplet without having to change ports for each new site. The key is to differentiate them using domain names or subdomains.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.