nginx反向代理节点js

时间:2019-10-13 19:29:44

标签: node.js docker-compose nginx-reverse-proxy

我想要: (1)https://example.com使用SSL提供静态内容 (2)https://sub.example.com/myapp用于使用SSL从NodeJS提供动态内容。

我有一个域example.com和一个子域sub.example.com。我有一个其中包含nginx和nodejs应用程序的Docker容器。在Digital Ocean上使用Ubuntu。

我收到502 Bad Gateway错误。不确定是否可以正常工作,如何解决。

  • nodejs应用在8080上侦听(未指定主机,没有SSL)
  • Nginx侦听example.com的80和443,但将所有http(80)请求重定向到https(443)
  • Nginx在443上监听sub.example.com。

docker-compose.yml:

version: '3'

services:
  nginx:
    image: nginx:1.15-alpine
    restart: unless-stopped
    volumes:
      - ./data/nginx:/etc/nginx/conf.d
      - ./data/certbot/conf:/etc/letsencrypt
      - ./data/certbot/www:/var/www/certbot
      - ./data/nginx/html:/etc/nginx/html
    ports:
      - "80:80"
      - "443:443"
    command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
  certbot:
    image: certbot/certbot
    restart: unless-stopped
    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;'"
  node:
    image: node:11
    volumes:
      - ./data/node:/usr/local/src/node
    ports:
      - "8080:8080"
    working_dir: /usr/local/src/node/myapp
    command: node server.js

nginx.conf:

server {
    listen 80;
    server_name example.com;
    server_tokens off;

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

    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl;
    server_name example.com;
    server_tokens off;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    location /myapp {
        proxy_pass  http://localhost:8080;
    }
}

0 个答案:

没有答案