繁体   English   中英

nginx:nginx 反向代理在服务器上工作,但在本地主机上不工作

[英]nginx: nginx reverse proxy works in server but not working on localhost

我有一个使用 prisma/graphql 的后端服务。 并且后端在端口 4001 上运行。这是我的 Nginx 配置文件proxy_pass:http://$host:4001的代理通行证

当我从服务器运行它时,它同时适用于http://server:4001http://server但是,当我从本地主机运行它时,我收到以下错误:

no resolver defined to resolve localhost, client: 172.30.0.1, server: _, request: "GET / HTTP/1.1", host: "localhost"

是什么导致了这个错误以及如何解决它?

docker-compose:

backend:
    image: base
    command: >
      sh -c "npm run deploy && npm run start"
    env_file: ${ENV_FILE}
    ports:
      - '4001:4001'
    depends_on:
      - prisma
    restart: always
    volumes:
      - ./deploy/certs:/deploy/certs

  nginx:
    image: nginx:latest
    volumes:
      - ./deploy/certs:/deploy/certs
      - ./deploy/nginx/conf.d:/etc/nginx/conf.d
    ports:
      - 80:80/tcp
      - 443:443/tcp
    depends_on:
      - backend
    restart: always
    env_file: ${ENV_FILE}

nginx 配置:

server {
    listen 80 default_server;
    server_name _;

location / {
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_pass http://$host:4001;

  }
}

我想您需要连接到nginx.conf文件中docker-compose.yml中指定的backend服务。

例如

upstream localhost {
  server backend:4001;
}

server {
  listen 80;
  server_name localhost;
}

你能试试这个吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM