简体   繁体   中英

Dockerized NGINX Reverse Proxy SSL

So I have a tech stack that amounts to:

  • NodeJS API Backend
  • ReactJS Frontend
  • NGINX Reverse Proxy
  • MongoDB (hosted through Mongo Atlas)

The API backend, ReactJS app, and NGINX reverse proxy are all configured using docker-compose. So they're all in their separate containers.

I'm having difficulties configuring the NGINX web server for SSL. I've tried various tutorials to no avail. This one getting me the closest. But it would fail at the acme-challenge with a "connection refused" error.

This is what my nginx config looks like (nginx.dev.conf):

worker_processes  1;

events {
    worker_connections  1024;
}

http {

    sendfile             on;
    keepalive_timeout    65;
    client_max_body_size 5M;

    gzip on;
    gzip_disable "msie6";

    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    # Block alihack
    deny 23.27.103.106/32;

    upstream api {
        least_conn;
        server api:8080 max_fails=3 fail_timeout=30s;
    }

    upstream app {
        least_conn;
        server app:3000 max_fails=3 fail_timeout=30s;
    }

    server {
        listen 80;

        location / {
            return 301 https://app;
        }

        location /.well-known/acme-challenge/ {
            root /var/www/certbot;
        }
    }

    server {
        listen 443 ssl;

        if ($request_method = 'OPTIONS') {
            return 200;
        }

        ssl_certificate /etc/letsencrypt/live/*censoredurl*/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/*censoredurl*/privkey.pem;

        # To allow POST on static pages
        error_page  405     =200 $uri;

        location / {
            proxy_pass http://app;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
            break;
        }

        location ~* \.(eot|otf|ttf|woff|woff2)$ {
            add_header Access-Control-Allow-Origin *;
        }

        location ~ /api/(?<url>.*) {
            proxy_pass http://api/$url;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }

        location /health-check {
            return 200;
            access_log off;
        }
    }

}

And my docker compose:

version: '3.7'

services:
  server:
    build:
      context: ./server
      dockerfile: Dockerfile
    image: myapp-server
    ports:
      - '80:80'
    links:
      - api:api
      - app:app
    volumes:
      - ./server/certbot/conf:/etc/letsencrypt
      - ./server/certbot/www:/var/www/certbot
    command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
  certbot:
    image: certbot/certbot
    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;'"
  app:
    container_name: app
    build:
      context: ./frontend
      dockerfile: Dockerfile
    volumes:
      - './frontend:/usr/app/frontend/'
      - '/usr/app/frontend/node_modules'
    ports:
      - '3000:3000'
    environment:
      - NODE_ENV=development
  api:
    container_name: api
    build:
      context: ./backend
      dockerfile: Dockerfile
    volumes:
      - './backend:/usr/app/backend/'
      - '/usr/app/backend/node_modules'
    ports:
      - '8080'
    environment:
      - NODE_ENV=development

EDIT The error I get

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator webroot, Installer None
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for *domain*.com
http-01 challenge for www.*domain*.com
Using the webroot path /var/www/certbot for all unmatched domains.
Waiting for verification...
Challenge failed for domain *domain*.com
Challenge failed for domain www.*domain*.com
http-01 challenge for *domain*.com
http-01 challenge for www.*domain*.com
Cleaning up challenges
Some challenges have failed.

IMPORTANT NOTES:
 - The following errors were reported by the server:

   Domain: *domain*.com
   Type:   connection
   Detail: Fetching
   http://*domain*.com/.well-known/acme-challenge/76vCqW16lDgy00XfjLoSGg9WnX5a757Xbdj2lzO2lnY:
   Connection refused

   Domain: www.*domain*.com
   Type:   connection
   Detail: Fetching
   http://www.*domain*.com/.well-known/acme-challenge/0Sfl0yltK75pcp_NVWcUaJhGNKgaR2thfdRcUxuF7zA:
   Connection refused

   To fix these errors, please make sure that your domain name was
   entered correctly and the DNS A/AAAA record(s) for that domain
   contain(s) the right IP address. Additionally, please check that
   your computer has a publicly routable IP address and that no
   firewalls are preventing the server from communicating with the
   client. If you're using the webroot plugin, you should also verify
   that you are serving files from the webroot path you provided.

My server works correctly when I don't have the SSL configuration put in place. So I know my DNS settings work as intended.

nginx handles your location routes topdown , so you want your acme config before anything else

this is my config;

server { .... location ~ /\\.well-known/acme-challenge { allow all; root /yourroot; try_files $uri $uri/ /$uri =418; } location / { return 301 https://$host$request_uri; } .... }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM