dsu89430 2019-08-10 00:52
浏览 400
已采纳

通过Nginx请求到api服务器(Golang)失败并返回404错误

I'm setting up nginx in docker environment.
When I try to access to api server via nginx port, request returns 404 error.

Here is the stack.

・client: react/axios
・api: golang/gin
・web server: nginx
・db: mysql
・container: docker
・ci-tool: travis
・deploy: aws elastic beanstalk

Entire source code is here:
https://github.com/jpskgc/article

article
  ├ client
  │  └ nginx
  │      └ default.conf
  ├ api
  ├ nginx
  │   └ default.conf
  └ docker-compose.yml

Here is docker-compose.yml.

//docker-compose.yml
version: '3'
services:
  nginx:
    restart: always
    build:
      dockerfile: Dockerfile.dev
      context: ./nginx
    ports:
      - '3050:80'
    depends_on:
      - client
      - api
  api:
    build:
      dockerfile: Dockerfile.dev
      context: ./api
    volumes:
      - ./api:/app
    depends_on:
      - db
    tty: true
    environment:
      - AWS_ACCESS_KEY_ID
      - AWS_SECRET_ACCESS_KEY
      - MYSQL_USER
      - MYSQL_PASSWORD
      - MYSQL_HOST
  client:
    build:
      dockerfile: Dockerfile.dev
      context: ./client
    volumes:
      - /app/node_modules
      - ./client:/app

Here is default.conf.

//default.conf
upstream client {
  server client:3000;
}

upstream api {
  server api:2345;
}

server {
  listen 80;

location / {
  proxy_pass http://client;
}

location /sockjs-node {
  proxy_pass http://client;
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection "Upgrade";
}

location /api {
  rewrite /api/(.*) /$1 break;
  proxy_pass http://api;
  }
}

Also there is default.conf in client.

server {
  listen 3000;

  location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
    try_files $uri $uri/ /index.html;
  }
}

I expect nginx reverse proxy to port 2345 api server.
But the actual returns 404 response.

nginx_1   | 172.23.0.1 - - [09/Aug/2019:22:19:06 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36" "-"
nginx_1   | 172.23.0.1 - - [09/Aug/2019:22:19:06 +0000] "GET /static/js/bundle.js HTTP/1.1" 304 0 "http://localhost:3050/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36" "-"
nginx_1   | 172.23.0.1 - - [09/Aug/2019:22:19:07 +0000] "GET /static/js/0.chunk.js HTTP/1.1" 304 0 "http://localhost:3050/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36" "-"
nginx_1   | 172.23.0.1 - - [09/Aug/2019:22:19:07 +0000] "GET /static/js/main.chunk.js HTTP/1.1" 304 0 "http://localhost:3050/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36" "-"
nginx_1   | 172.23.0.1 - - [09/Aug/2019:22:19:07 +0000] "GET /static/js/bundle.js.map HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36" "-"
nginx_1   | 172.23.0.1 - - [09/Aug/2019:22:19:08 +0000] "GET /static/js/0.chunk.js.map HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36" "-"
nginx_1   | 172.23.0.1 - - [09/Aug/2019:22:19:08 +0000] "GET /static/js/main.chunk.js.map HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36" "-"
api_1     | [GIN] 2019/08/09 - 22:19:09 | 404 |      41.937µs |      172.23.0.5 | GET      /articles
nginx_1   | 172.23.0.1 - - [09/Aug/2019:22:19:09 +0000] "GET /api/articles HTTP/1.1" 404 18 "http://localhost:3050/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36" "-"
nginx_1   | 172.23.0.1 - - [09/Aug/2019:22:19:10 +0000] "GET /sockjs-node/info?t=1565389150444 HTTP/1.1" 200 90 "http://localhost:3050/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36" "-"
nginx_1   | 172.23.0.1 - - [09/Aug/2019:22:19:10 +0000] "GET /manifest.json HTTP/1.1" 304 0 "http://localhost:3050/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36" "-"

Also when accessing to actual deployed url, it returns 502 Bad Gateway now.
http://multidocker-env.vwnrixavuv.ap-northeast-1.elasticbeanstalk.com/api/articles

  • 写回答

1条回答 默认 最新

  • doushenyu8228 2019-08-10 05:50
    关注

    As Enix suggested on comment, after removing rewrite directive, issue is resolved.

    rewrite /api/(.*) /$1 break;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置