【问题标题】:nginx: nginx reverse proxy works in server but not working on localhostnginx:nginx反向代理在服务器上工作,但在本地主机上不工作
【发布时间】:2020-08-27 03:40:33
【问题描述】:

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

当我从服务器运行它时,它可以在 http://server:4001http://server 上运行 但是,当我从 localhost 运行它时,我收到以下错误:

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

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

码头工人撰写:

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;

  }
}

【问题讨论】:

    标签: javascript nginx graphql prisma prisma-graphql


    【解决方案1】:

    我猜你需要连接到docker-compose.ymlnginx.conf 文件中指定的backend 服务。

    例如

    upstream localhost {
      server backend:4001;
    }
    
    server {
      listen 80;
      server_name localhost;
    }
    

    你能试试这个吗?

    【讨论】:

    • 我试过这个。并得到同样的错误。 nginx_1 | 2020/05/12 09:19:43 [error] 6#6: *2 no resolver defined to resolve localhost, client: 172.30.0.1, server: _, request: "GET / HTTP/1.1", host: "localhost"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    相关资源
    最近更新 更多