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!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
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: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 replaceyourdomain.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:
Let me know how it goes!
For more information on Nginx reverse proxy setups, I would recommend the following tutorial:
https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-as-a-reverse-proxy-on-ubuntu-22-04
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.