如何在两个容器之间通信:nginx和nodejs

agyaoht7  于 2023-11-17  发布在  Nginx
关注(0)|答案(1)|浏览(160)

我很难弄清楚如何将proxypassnginx容器转换为nodejs容器。
在我看来,http://localhost:3000将落在nginx容器中......所以我认为这个设置是有意义的:

nginx容器:

  1. podman run -d \
  2. --name nginx.main \
  3. -p 0.0.0.0:8081:8080 \
  4. -p 0.0.0.0:4431:4430 \
  5. -p 0.0.0.0:3001:3000 \
  6. -u root \
  7. -v /home/_secrets/certbot/_certs:/etc/nginx/_cert \
  8. -v /home/mee/_volumes/nginx_main:/etc/nginx \
  9. nginx

字符串

nodejs容器:

  1. podman run -d \
  2. -v /home/mee/dev/abd/:/usr/src/app -w /usr/src/app \
  3. -p 3000:3000 \
  4. --name next.dev node:latest \
  5. npm run dev

firewalld,从3001路由到3000

  1. sudo firewall-cmd --add-port=3000/tcp --permanent
  2. sudo firewall-cmd --add-port=3001/tcp --permanent
  3. sudo firewall-cmd --permanent \
  4. --zone=mee_fd \
  5. --add-forward-port=port=3001:proto=tcp:toport=3000
  6. sudo firewall-cmd --reload

nginx配置:

  1. location / {
  2. proxy_pass http://localhost:3000;
  3. add_header X-Frame-Options "SAMEORIGIN" always;
  4. add_header X-XSS-Protection "1; mode=block" always;
  5. add_header X-Content-Type-Options "nosniff" always;
  6. add_header Referrer-Policy "no-referrer-when-downgrade" always;
  7. add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;
  8. # add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
  9. # enable strict transport security only if you understand the implications
  10. }


真的不知道这应该如何通信.我已经尝试使用ipaddress而不是'localhost',但我得到相同的响应.
谢谢

vsdwdz23

vsdwdz231#

为了允许容器之间的通信,您需要设置一个共享网络,例如在.yaml中(这可以在ci上完成,仅出于代码目的在.yaml中报告):

  1. version: '2'
  2. services:
  3. proxy:
  4. build: ./
  5. networks:
  6. - example1
  7. - example2
  8. ports:
  9. - 80:80
  10. - 443:443
  11. networks:
  12. example1:
  13. external:
  14. name: example1_default
  15. example2:
  16. external:
  17. name: example2_default

字符串
在nginx配置中:

  1. location / {
  2. proxy_pass http://myServiceName:3000; <-- note is not localhost but the name of node service
  3. add_header X-Frame-Options "SAMEORIGIN" always;
  4. add_header X-XSS-Protection "1; mode=block" always;
  5. add_header X-Content-Type-Options "nosniff" always;
  6. add_header Referrer-Policy "no-referrer-when-downgrade" always;
  7. add_header Content-Security-Policy "default-src * data: 'unsafe-eval' 'unsafe-inline'" always;
  8. # add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
  9. # enable strict transport security only if you understand the implications
  10. }


让我知道

展开查看全部

相关问题