The first step is to docker-ize Node Server by adding Dockerfile directly to the project. The configuration file for NGINX is as follows
server {
listen 80;
server_name example.org;
server_tokens off;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
return 301
}
}
server {
listen 443 ssl;
server_name example.org;
server_tokens off;
ssl_certificate /etc/letsencrypt/live/example.org/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.org/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
location / {
proxy_pass
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
} Now that we have a valid Nginx Server, we are done connecting Node Server to Nginx. We made some modifications directly to the docker-compose.yml provided in the previous boilerplate.
version: “3”
services:
nodeapp:
Image: NoDeserver :1.0.0 container_name: NodeApp restart: Unless -stopped volumes: - /data/usersFolder:/server/config ports: - "3000:3000" networks: - app-network
nginx:
Image: Nginx: 1.15-Alpine container_name: Nginx_server restart: Unless -stopped volumes: - ./data/nginx:/etwww.pizei.comc/nginx/conf.d - ./data/certbot/conf:/etc/letsencrypt - ./data/certbot/www:/var/www/certbot ports: - "80:80" - "443:443" networks: - app-network command: '/bin/sh -c ''while :; do sleep 6h & wait $${! }; nginx -s reload; done & nginx -g "daemon off;" ' ' '
certbot:
image: certbot/certbot restart: unless-stopped container_name: certbot_one volumes: - ./data/certbot/conf:/etc/letsencrypt - ./data/certbot/www:/var/www/certbot entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${! }; done; '"
networks:
app-network:
driver: bridge
By creating a network that allows Nginx to communicate directly with the node server, Nginx conf can also be written more smoothly. In the past I remember using a — link for container to container communication, but the official preference is to create a network