如何使用jwilder/nginx-proxy设置反向代理?
How to set reverse proxy using jwilder/nginx-proxy?
我正在尝试使用 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 文件进行设置吗?
谢谢
编辑:
Docker日志:
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
我想你需要 url
然后 letesencrypt_host
和 letsencrypt_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
我正在尝试使用 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 文件进行设置吗?
谢谢
编辑: Docker日志:
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
我想你需要 url
然后 letesencrypt_host
和 letsencrypt_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