Skip to content

nginx+nodejs

Dockerfile

Dockerfile
FROM node:14.17.6

RUN npm install npm@latest -g

nginx.conf

conf
user root;
worker_processes auto;
pid /run/nginx.pid;
events {
    worker_connections 768;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 0;
    types_hash_max_size 2048;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    access_log /var/log/nginx/nginx_access.log;
    error_log /var/log/nginx/nginx_error.log;
    gzip on;
    gzip_disable "msie6";

    server {
        listen ${HTTPS_PORT} ssl;
        server_name localhost;
        ssl_certificate     ${SSL_CERTIFICATE};
        ssl_certificate_key ${SSL_CERTIFICATE_KEY};
        ssl_session_timeout 2m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers on;
        
        location / {
            proxy_pass ${HTTP_SERVER};
        }
    }
}

Makefile

Makefile
WORK_DIR  := /root/project
CACHE_DIR := /root/.cache
IMAGE_TAG := node14

DOCKER_RUN := docker run \
--rm -it \
-v $(PWD):$(WORK_DIR) \
-v $(PWD)/cache:$(CACHE_DIR) \
-w $(WORK_DIR) \
$(IMAGE_TAG)

build:
	docker build . -t $(IMAGE_TAG)
install:
	$(DOCKER_RUN) npm install
dev:
	$(DOCKER_RUN) npm run dev
release:
	$(DOCKER_RUN) npm run build
cert:
	openssl req -new -newkey rsa:4096 -days 3650 -nodes -x509 \
-subj "/C=US/ST=WO/L=Stan/O=microsoft.com/CN=ms" -keyout ./nginx/cert/privkey.pem -out ./nginx/cert/fullchain.pem

docker-compose.yml

yml
version: '3'
services:
  frontend:
    build: 
      context: .
      dockerfile: Dockerfile
    image: node14
    restart: always
    volumes:
      - ./:/root/project
      - ./cache:/root/.cache
    working_dir: /root/project
    command: npm run dev
    ports:
      - "127.0.0.1:8000:8000"
  nginx:
    image: nginx:1.20.1
    restart: always
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.template
      - ./nginx/cert:/etc/nginx/cert
      - ./cache/nginx:/var/log/nginx
    environment:
      HTTP_SERVER: http://127.0.0.1:8000
      HTTPS_PORT: 19443
      SSL_CERTIFICATE: /etc/nginx/cert/fullchain.pem
      SSL_CERTIFICATE_KEY: /etc/nginx/cert/privkey.pem
    command: /bin/bash -c "envsubst < /etc/nginx/nginx.template > /etc/nginx/nginx.conf && nginx -g 'daemon off;'"
    depends_on:
      - frontend
    network_mode: host

Released under the MIT License.