testing local subdomain with nginx and docker
For reference: we were able to run this remotely using AWS ligthsail, using the following settings
worker_processes 1;events { worker_connections 1024;}http { include /etc/nginx/mime.types; gzip on; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript; upstream landingpage { server landingpage:5000; } upstream frontend { server frontend:5000; } server { listen 80; if ($http_x_forwarded_proto != 'https') { return 301 https://$host$request_uri; } server_name domain.com www.domain.com; location / { proxy_pass http://landingpage; } } server { listen 80; if ($http_x_forwarded_proto != 'https') { return 301 https://$host$request_uri; } server_name demo.domain.com www.demo.domain.com; location / { add_header X-Robots-Tag "noindex, nofollow, nosnippet, noarchive, notranslate, noimageindex"; proxy_pass http://frontend; } }}
with the following dockerfile for both react apps (basically exposing port 5000 for both services)
FROM node:latest RUN mkdir -p /usr/src/appWORKDIR /usr/src/appCOPY package.json /usr/src/app/RUN npm install --verboseCOPY . /usr/src/appRUN npm run build --productionRUN npm install -g serveEXPOSE 5000CMD serve -s build
Unfortunately I cannot provide more details on doing this on a local machine
This is working for me. The difference might be that I'm using a fake domain name, but I can't say for sure. I'm also using ssl, because I couldn't get Firefox to access the fake domain via http. I'm routing the subdomain to Couchdb. The webclient service is the parcel-bundler
development server.
/etc/hosts
127.0.0.1 example.local127.0.0.1 www.example.local127.0.0.1 db.example.local
develop/docker-compose.yaml
version: '3.5'services: nginx: build: context: ../ dockerfile: develop/nginx/Dockerfile ports: - 443:443 couchdb: image: couchdb:3 volumes: - ./couchdb/etc:/opt/couchdb/etc/local.d environment: - COUCHDB_USER=admin - COUCHDB_PASSWORD=password webclient: build: context: ../ dockerfile: develop/web-client/Dockerfile volumes: - ../clients/web/src:/app/src environment: - CLIENT=web - COUCHDB_URL=https://db.example.local
develop/nginx/Dockerfile
FROM nginxCOPY develop/nginx/conf.d/* /etc/nginx/conf.d/COPY develop/nginx/ssl/certs/* /etc/ssl/example.local/
develop/nginx/conf.d/default.conf
server { listen 443 ssl; ssl_certificate /etc/ssl/example.local/server.crt; ssl_certificate_key /etc/ssl/example.local/server.key.pem; server_name example.local www.example.local; location / { proxy_pass http://webclient:1234; }}server { listen 443 ssl; ssl_certificate /etc/ssl/example.local/server.crt; ssl_certificate_key /etc/ssl/example.local/server.key.pem; server_name db.example.local; location / { proxy_pass http://couchdb:5984/; }}
develop/web-client/Dockerfile
FROM node:12-alpineWORKDIR /appCOPY clients/web/*.config.js ./COPY clients/web/package*.json ./RUN npm installCMD ["npm", "start"]
Here is the blog that shows how to generate the self-signed certs.