gpt4 book ai didi

node.js - Nginx 与 dockerized NodeJS

转载 作者:搜寻专家 更新时间:2023-10-31 23:27:16 25 4
gpt4 key购买 nike

我有一个 NodeJS 应用程序,它是 dockerized 的,我有一个 NGINX docker 容器,它在 docker 容器中的 NodeJS 进程之间进行负载平衡。我能够成功地 curl 两个 NodeJs 服务器,但 NGINX 找不到上游服务器。

这是 nginx 配置文件:

upstream app {
least_conn; # Use Least Connections strategy
server 127.0.0.1:3000; # NodeJS Server 1
server 127.0.0.1:3001; # NodeJS Server 2
}
server {
listen 80;
server_name example.com;

access_log /var/log/nginx/example.com-access.log;
error_log /var/log/nginx/example.com-error.log error;

# Browser and robot always look for these
# Turn off logging for them
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { log_not_found off; access_log off; }

# Handle static files so they are not proxied to NodeJS
# You may want to also hand these requests to other upstream
# servers, as you can define more than one!
location ~* (images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico) {
root /usr/share/nginx/html;
}

# pass the request to the node.js server
# with some correct headers for proxy-awareness
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;

proxy_pass http://app/;
proxy_redirect off;

# Handle Web Socket connections
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}

# Proxy requests to the mobile api to the mobile api servers (old version of Android App uses m.goodrx.com URLs)
location /mobile-api {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;

proxy_pass http://www.goodrx.com/mobile-api;
proxy_redirect off;

# Handle Web Socket connections
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}

然后我启动绑定(bind)端口 3000 和 3001 的 Node 容器以及绑定(bind)端口 80 的 nginx 容器。当 curl 127.0.0.1:80 时,我收到一个错误请求,但 curl 127.0.0.1:3000 和 127.0.0.1:3001 有效。知道 NGINX 代理可能出了什么问题吗?

最佳答案

127.0.0.1,或者localhost指向nginx容器。你可以查看我的回答here但基本上你需要用 --add-host docker:<IP ADDRESS> 运行 nginx 容器,然后更改您的 nginx 配置:

upstream app {
least_conn; # Use Least Connections strategy
server docker:3000; # NodeJS Server 1
server docker:3001; # NodeJS Server 2
}

关于node.js - Nginx 与 dockerized NodeJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28619356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com