I’m trying to run two sites on django on the same server under different ip, an error occurs that the port is busy, I fixed the ports, but the site does not start. Tell me where is the error please? Ip work, when I go to the second ip I get redirects to the first site. All settings were specified for the second site. At the end, I added the nginx setting of the first site
This is the second docker-compose file and its settings. I would be very grateful for your help
.env
#Django
# Should be one of dev, prod
MODE=prod
PORT=8008
#postgres
DB_NAME=xxx
DB_USER=xxx
DB_HOST=xxx
DB_PASSWORD=xxxx
DB_PORT=5432
POSTGRES_PASSWORD=mysecretpassword
#WSGI
WSGI_PORT=8008
WSGI_WORKERS=4
WSGI_LOG_LEVEL=debug
# Celery
CELERY_NUM_WORKERS=2
# Email
EMAIL_HOST_USER=xxxx
EMAIL_HOST_PASSWORD=xxxx
docker-compose.yml
version: '3'
services:
backend:
build: ./
container_name: site_container
restart: always
command: ./commands/start_server.sh
ports:
- "${PORT}:${WSGI_PORT}"
volumes:
- ./src:/srv/project/src
- ./commands:/srv/project/commands
- static_content:/var/www/site
env_file:
- .env
depends_on:
- postgres
postgres:
image: postgres:12
volumes:
- pg_data:/var/lib/postgresql/data
env_file:
- .env
# environment:
# - DJANGO_SETTINGS_MODULE=app.settings.${MODE}
nginx:
image: nginx:1.19
volumes:
- ./nginx:/etc/nginx/conf.d
- static_content:/var/www/site
ports:
- 81:80
- 444:443
env_file:
- .env
depends_on:
- backend
volumes:
pg_data: {}
static_content: {}
default.conf
server {
listen 80 default_server;
server_name 183.22.332.12;
location /static/ {
root /var/www/site;
}
location /media/ {
root /var/www/site;
}
location / {
proxy_set_header Host $host;
proxy_pass http://backend:8010;
}
}
default.conf for first site
server {
#listen 80 default_server;
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name site1 ip_site1;
ssl_certificate /etc/letsencrypt/live/site1/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/site1/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/site1/chain.pem;
location /static/ {
root /var/www/artads;
}
location /media/ {
root /var/www/artads;
}
location / {
proxy_set_header Host $host;
proxy_pass http://backend:8008;
}
}
server {
listen 80 default_server;
server_name ip_site2 site2;
location /static/ {
root /var/www/gdr_mr;
}
location /media/ {
root /var/www/gdr_mr;
}
location / {
proxy_set_header Host $host;
proxy_pass http://backend:8013;
}
}
server {
listen 80;
listen [::]:80;
server_name www.site1 site1;
location / {
return 301 https://site1$request_uri;
}
}
3
Answers
If you’re running two virtual servers with different IPs on the same machine, you’d want to specify the IP address in the listen directive:
More on how
nginx
processes requests can be found here: http://nginx.org/en/docs/http/request_processing.htmlIf you want run more than one site in a server, you can
ip
port
domain
…
You can choose one of top list tips.
In your config, you choose different ip and same port, but you set all the site to default and not listen the different ip
Usually IP is just omitted.
Or you can one ip and different port.
Or you can one ip and one port but different domain.
Thanks to @Roman Tokaren and @Oleksandr
Here the english translated version submitted by @Roman Tokaren here
You can always argue a lot about the "correct" launch – after all, how many people, so many opinions, but I will describe an example + – of a "convenient" and scalable configuration. For the "convenience" of working in such a configuration, I would suggest installing nginxproxymanager as a reverse proxy and combining containers and nginxproxymanager into one network – after which it will be possible to forward container ports via http (s), tcp, udp to an external interface using the GUI as well as a number of other goodies, such as the generation of SSL certificates and their auto renewal
First, let’s create the network itself
Let’s configure NPM (nginxproxymanager) – by default we will consider the reverse proxy as the last network node, as a result we will get
And configure the container itself
After that, we carry out the initial configuration of NPM according to the instructions and add the host