By Lea Cards
With django(backend) and NextJS (frontend) I wonder how to set up my nginx file so it can serve both django and NextJs from the same digitalocean IP.
I have a project with
my project
The docker-compose.yml runs and mounts the django project.
For the frontend to run I need to
Locally django runs on port 8000 and NextJS on port 3000.
I pushed the project to a digitalocean dockerized droplet. I ran docker-compose and it mounted as expected.
But I struggle to grasp how to set up my nginx file so it can serve both folders from the same digitakl-ocean IP.
Any return would be highly appreciated.
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!
Accepted Answer
Hi there,
To serve both Django and Next.js from the same DigitalOcean IP using nginx, you will need to set up a reverse proxy. This means that nginx will listen for requests and forward them to the appropriate application based on the path of the URL or the port number.
Here’s a general template of what your nginx configuration file might look like, located at /etc/nginx/sites-available/default
or /etc/nginx/conf.d/default.conf
, depending on the Docker image that you are using:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
location / {
proxy_pass http://localhost:3000; # NextJS App
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;
}
location /api {
proxy_pass http://localhost:8000; # Django API
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;
}
}
In this configuration, nginx listens on port 80 (the default HTTP port). It then routes requests to your Django API if the URL path begins with /api
and to your Next.js app for all other requests. You’ll replace yourdomain.com
with your actual domain name or IP address.
Note that this assumes your Django application is set up to treat /api
as a base path for all its routes. If it isn’t, you’ll need to adjust the configuration and your Django routes accordingly.
Also remember to restart or reload nginx after changing the configuration:
sudo systemctl restart nginx
Let me know how it goes!
For more information on Nginx reverse proxy setups, I would recommend the following tutorial:
Hope that this helps!
Best,
Bobby
Hi, I’m having the same issue, have you manage to make it work Django and NextJs ? If you did, do you mind sharing your dockerfiles and docker-compose.yml file ? thank you.
Hi, I’m having the same issue, have you manage to make it work Django and NextJs ? If you did, do you mind sharing your dockerfiles and docker-compose.yml file ? thank you.
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.