gpt4 book ai didi

docker - 关于 Docker 上的 nginx 配置

转载 作者:行者123 更新时间:2023-12-02 19:52:47 25 4
gpt4 key购买 nike

我是Docker新手,我有一个Debian 9系统的服务器,它的IP是104.167.123.123,我通过Docker在上面安装了一个Web应用程序,端口是3001,我可以通过http://127.0.0.1:3001访问该应用程序和 http://104.167.123.123:3001 ,我还在这台服务器上通过 Docker 安装了 Nginx,default.conf正在关注

server {
listen 80;
return 500;
}
server {
listen 80;
server_name domain.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:3001;
proxy_redirect off;
proxy_set_header X-Real-PORT $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

我的问题是
  • 如何使 IP 访问和 IP:port 访问都返回 500,现在 IP 访问返回 500,但 ip:port 不是,例如104.167.123.123:3001 ?
  • default.conf , 如果 proxy_pass http://127.0.0.1:3001;应用无法通过域名访问,但如果改为proxy_pass http://104.167.123.123:3001; ,可以通过域名访问,为什么呢?

  • 请帮帮我,谢谢。

    最佳答案

    How to make both IP access and IP:port access return 500, now IP access return 500, but ip:port is not, e.g. 104.167.123.123:3001?



    当您点击 IP只有发送到 Nginx 的请求,所以 Nginx 返回 500,正如您在配置中提到的,但是当您尝试访问 104.167.123.123:3001此请求不是从 Nginx 路由,而是直接访问您的后端服务器。

    In default.conf, if proxy_pass http://127.0.0.1:3001; the application cannot be accessed through the domain name, but if change to proxy_pass http://104.167.123.123:3001;, it can be accessed through the domain name, Why?



    你在 docker 中运行 Nginx,所以 127.0.0.1:3000意思是 Nginx 容器的 localhost 不是后端容器。在使用 http://104.167.123.123:3001; 时这样,nginx 能够将此请求路由到后端服务器。

    您可以通过执行以下操作来解决问题
  • 使用 docker-compose 并且不要从后端应用程序公开端口,这样您的后端服务将无法被 IP:PORT 机器人访问.
  • 在 nginx 配置中使用后端服务名称而不是容器的公共(public) ip,因此您不需要在配置中添加公共(public) ip 或 localhost:3000

  • 例如
        listen 80;
    server_name domain.com;
    location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header HOST $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_pass http://web:3000;
    proxy_redirect off;
    proxy_set_header X-Real-PORT $remote_port;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    docker 撰写
    web:
    image:nodejs
    command: node app.js
    nginx:
    image:nginx
    ports:
    - "80:80"
    depends_on:
    - web

    关于docker - 关于 Docker 上的 nginx 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59456776/

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