drw85135 2019-05-31 10:01
浏览 325
已采纳

Docker Nginx 502 GET / Post请求的网关不正确,但Websocket无效

There have been various similar cases here on stackoverflow but they're pretty much all refer to incorrect ports or using localhost as the IP instead of the docker-machine ip.

The vue.js app connects perfectly fine to the websocket and works. However, the GET requests to 192.168.99.100:8080/meows and other endpoints all hit the nginx 502 bad gateway. Manually accessing the endpoints (instead of vue.js) also hit 502 bad gateway.

The ip's are properly set. Ports are the same everywhere :8080. endpoints have the correct http verband all have an nginx upstream pointing at the server location /..{}. Yet no problems connecting to the network, all data passing through no problem.

EDIT: Im running windows 7 with the docker-toolbox because my windows version doesnt have the whole virtualization thing. No further configuration after installation has been done.

The architecture is the following:

enter image description here

docker-compose

version: "3.6"

services:
  meow:
    build: "."
    command: "meow-service"
    depends_on:
      - "postgres"
      - "nats"
    environment:
      POSTGRES_DB: "meower"
      POSTGRES_USER: "meower"
      POSTGRES_PASSWORD: "123456"
      NATS_ADDRESS: "nats:4222"
  query:
    build: "."
    command: "query-service"
    depends_on:
      - "postgres"
      - "nats"
    environment:
      POSTGRES_DB: "meower"
      POSTGRES_USER: "meower"
      POSTGRES_PASSWORD: "123456"
      NATS_ADDRESS: "nats:4222"
      ELASTICSEARCH_ADDRESS: "elasticsearch:9200"
  pusher:
    build: "."
    command: "pusher-service"
    depends_on:
      - "nats"
    environment:
      NATS_ADDRESS: "nats:4222"
  postgres:
    build: "./postgres"
    restart: "always"
    environment:
      POSTGRES_DB: "meower"
      POSTGRES_USER: "meower"
      POSTGRES_PASSWORD: "123456"
  nats:
    image: "nats-streaming:0.9.2"
    restart: "always"
  elasticsearch:
    image: 'docker.elastic.co/elasticsearch/elasticsearch:6.2.3'
  nginx:
    build: "./nginx"
    ports:
      - "8080:80"
    depends_on:
      - "meow"
      - "query"
      - "pusher"

nginx.conf:

user nginx;
worker_processes 1;

events {
  worker_connections 1024;
}

http {
  upstream meows_POST {
    server meow:8080;
  }

  upstream meows_GET {
    server query:8080;
  }

  upstream search_GET {
    server query:8080;
  }

  upstream pusher {
    server pusher:8080;
  }

  server {
    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;
    add_header Access-Control-Allow-Origin *;

    location /meows {
      limit_except GET POST OPTIONS {
        deny all;
      }
      proxy_pass http://meows_$request_method;
    }

    location /search {
      limit_except GET OPTIONS {
        deny all;
      }
      proxy_pass http://search_GET;
    }

    location /pusher {
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
      proxy_pass http://pusher;
    }
  }
}

And to show that the go application also uses the correct ports, the following is the port listening for

func newRouter() (router *mux.Router) {
    router = mux.NewRouter()
    router.HandleFunc("/meows", listMeowsHandler).
        Methods("GET")
    router.HandleFunc("/search", searchMeowsHandler).
        Methods("GET")
    return
}

router := newRouter()
if err := http.ListenAndServe(":8080", router); err != nil {
    log.Fatal(err)
}
  • 写回答

2条回答 默认 最新

  • dongxian4531 2019-05-31 13:45
    关注

    Removed all

      ports:
      - "8080"
    

    properties from all services in the ngix.conf as was proposed.

    The underlying problem however was the elasticsearch docker image. Using docker ps -a i could see that after a minute it would exit 78. Using docker-compose logs elasticsearch the error was max virtual memory areas vm.max_map_count px[ is too low, increase to at least [x]. Using belows command the host machine allocated more memory for the containers and everything was solved.

    docker-machine ssh
    sudo sysctl -w vm.max_map_count=262144
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog