【问题标题】:How to set reverse proxy using jwilder/nginx-proxy?如何使用 jwilder/nginx-proxy 设置反向代理?
【发布时间】:2021-12-09 17:42:29
【问题描述】:

我正在尝试使用 jwilder/nginx-proxy 容器设置反向代理。

我有一个小型 Express 应用:

var express = require("express");
var app = express();

app.get("/api", (req, res, next) => {
  res.json({ hello: "world" });
});

app.listen(3000, () => {
  console.log("Server running on port 3000");
});

还有一个docker-compose.yml

version: "3.4"
services:
  reverse-proxy:
    image: jwilder/nginx-proxy
    ports:
      - 80:80
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
    networks:
      - testt

  testt:
    container_name: testt
    image: node:lts
    working_dir: /var/www/html/app/
    entrypoint: /bin/bash
    environment:
      - HOST=0.0.0.0
      - VIRTUAL_HOST=testt.dev # for reverse proxy
      - VIRTUAL_PORT=3000 # for reverse proxy
    ports:
      - 3000:3000
    volumes:
      - ./testt/:/var/www/html/app
    tty: true
    networks:
      - testt

networks:
  testt:
    external: true

这就是我设置的所有内容。没有别的了。

当我运行localhost:3000/api 时,我得到了预期的结果{"hello":"world"}。当我在浏览器中运行 http://testt.dev/api 时,我希望得到相同的结果,但它不起作用(DNS_PROBE_FINISHED_NXDOMAIN)。

我还需要设置什么以及在哪里设置? 我必须为 HOSTS 文件设置一些内容吗?

谢谢

编辑: 码头工人日志:

Setting up DH Parameters..

forego      | starting dockergen.1 on port 5000

forego      | starting nginx.1 on port 5100

nginx.1     | 2021/12/09 09:39:36 [notice] 22#22: using the "epoll" event method

nginx.1     | 2021/12/09 09:39:36 [notice] 22#22: nginx/1.21.3

nginx.1     | 2021/12/09 09:39:36 [notice] 22#22: built by gcc 8.3.0 (Debian 8.3.0-6) 

nginx.1     | 2021/12/09 09:39:36 [notice] 22#22: OS: Linux 5.10.47-linuxkit

nginx.1     | 2021/12/09 09:39:36 [notice] 22#22: getrlimit(RLIMIT_NOFILE): 1048576:1048576

nginx.1     | 2021/12/09 09:39:36 [notice] 22#22: start worker processes

nginx.1     | 2021/12/09 09:39:36 [notice] 22#22: start worker process 27

nginx.1     | 2021/12/09 09:39:36 [notice] 22#22: start worker process 28

nginx.1     | 2021/12/09 09:39:36 [notice] 22#22: start worker process 29

nginx.1     | 2021/12/09 09:39:36 [notice] 22#22: start worker process 30

dockergen.1 | 2021/12/09 09:39:36 Template error: open /etc/nginx/certs: no such file or directory

dockergen.1 | 2021/12/09 09:39:36 Generated '/etc/nginx/conf.d/default.conf' from 2 containers

dockergen.1 | 2021/12/09 09:39:36 Running 'nginx -s reload'

nginx.1     | 2021/12/09 09:39:36 [notice] 22#22: signal 1 (SIGHUP) received from 32, reconfiguring

nginx.1     | 2021/12/09 09:39:36 [notice] 22#22: reconfiguring

dockergen.1 | 2021/12/09 09:39:36 Watching docker events

nginx.1     | 2021/12/09 09:39:36 [notice] 22#22: using the "epoll" event method

nginx.1     | 2021/12/09 09:39:36 [notice] 22#22: start worker processes

nginx.1     | 2021/12/09 09:39:36 [notice] 22#22: start worker process 37

nginx.1     | 2021/12/09 09:39:36 [notice] 22#22: start worker process 38

nginx.1     | 2021/12/09 09:39:36 [notice] 22#22: start worker process 39

nginx.1     | 2021/12/09 09:39:36 [notice] 22#22: start worker process 40

dockergen.1 | 2021/12/09 09:39:36 Template error: open /etc/nginx/certs: no such file or directory

dockergen.1 | 2021/12/09 09:39:36 Contents of /etc/nginx/conf.d/default.conf did not change. Skipping notification 'nginx -s reload'

nginx.1     | 2021/12/09 09:39:36 [notice] 27#27: gracefully shutting down

nginx.1     | 2021/12/09 09:39:36 [notice] 28#28: gracefully shutting down

nginx.1     | 2021/12/09 09:39:36 [notice] 30#30: gracefully shutting down

nginx.1     | 2021/12/09 09:39:36 [notice] 29#29: gracefully shutting down

nginx.1     | 2021/12/09 09:39:36 [notice] 27#27: exiting

nginx.1     | 2021/12/09 09:39:36 [notice] 30#30: exiting

nginx.1     | 2021/12/09 09:39:36 [notice] 29#29: exiting

nginx.1     | 2021/12/09 09:39:36 [notice] 28#28: exiting

nginx.1     | 2021/12/09 09:39:36 [notice] 27#27: exit

nginx.1     | 2021/12/09 09:39:36 [notice] 29#29: exit

nginx.1     | 2021/12/09 09:39:36 [notice] 30#30: exit

nginx.1     | 2021/12/09 09:39:36 [notice] 28#28: exit

nginx.1     | 2021/12/09 09:39:36 [notice] 22#22: signal 17 (SIGCHLD) received from 28

nginx.1     | 2021/12/09 09:39:36 [notice] 22#22: worker process 27 exited with code 0

nginx.1     | 2021/12/09 09:39:36 [notice] 22#22: worker process 28 exited with code 0

nginx.1     | 2021/12/09 09:39:36 [notice] 22#22: signal 29 (SIGIO) received

nginx.1     | 2021/12/09 09:39:36 [notice] 22#22: signal 17 (SIGCHLD) received from 27

nginx.1     | 2021/12/09 09:39:36 [notice] 22#22: worker process 30 exited with code 0

nginx.1     | 2021/12/09 09:39:36 [notice] 22#22: signal 29 (SIGIO) received

nginx.1     | 2021/12/09 09:39:36 [notice] 22#22: signal 17 (SIGCHLD) received from 29

nginx.1     | 2021/12/09 09:39:36 [notice] 22#22: worker process 29 exited with code 0

nginx.1     | 2021/12/09 09:39:36 [notice] 22#22: signal 29 (SIGIO) received

【问题讨论】:

  • 您是否设置了 DNS 以便 testt.dev 解析到您的计算机?
  • 无论如何。我必须为主机文件设置一些东西吗?
  • 是的,这将在开发过程中起作用。将127.0.0.1 testt.dev 添加到/etc/hosts。
  • 好吧,不工作。我希望我需要设置类似的东西......但它没有帮助。奇怪

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


【解决方案1】:

如果您想要证书,我认为您需要 url,然后是 letesencrypt_hostletsencrypt_email。试试:

version: '3.7'
services:

    ghost01:
        image: "ghost"
        user: "1000"
        environment:
            - url=https://test01.zathras.io
            - VIRTUAL_HOST=test01.zathras.io
            - VIRTUAL_HOST_ALIAS=test01.zathras.io
            - LETSENCRYPT_HOST=test01.zathras.io
            - LETSENCRYPT_EMAIL=qdzlug@gmail.com
        networks:
            - webproxy
networks:
    webproxy:
        external: true

【讨论】:

  • 好吧,我被重定向到 https,但仍然得到 ERR_CONNECTION_REFUSED。我也在我的问题中添加了一个 docker 日志。谢谢
  • 嗯,好吧。那么问题就更深了。据您所知,您尝试向其发送流量的容器没有运行任何东西。端口 80 我在一个需要 root 访问权限才能绑定的区域上进行操作,因此这也可能会失败。如果您使用official nginx image 使事情正常进行,您可以将其放入,它应该可以正常工作。查看您的应用程序,猜测它很可能没有运行。我会检查 docker ps -a 以查看它是否正在运行,并检查 docker logs <containerid> 以查看它正在记录什么。
猜你喜欢
  • 2022-11-02
  • 1970-01-01
  • 2017-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-09
  • 2020-05-03
  • 2016-07-05
相关资源
最近更新 更多