dsu89430 2019-08-09 16:52
浏览 403
已采纳

通过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-09 21:50
    关注

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

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

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

报告相同问题?

悬赏问题

  • ¥15 springboot中的路径问题
  • ¥80 App Store Connect 中设置了订阅项目,Xcode 中预览可以正确显示价格,真机测试却无法显示
  • ¥15 MATLAB的PIV算法问题
  • ¥15 RflySim例程学习:matlab编译报错
  • ¥20 谁来给我解答一下疑惑
  • ¥80 有偿!!请懂注册表的翻译解析一下一小段注册表的调整内容
  • ¥15 基于52单片机 交通灯系统
  • ¥15 根据访客ip显示城市名称
  • ¥20 对文档进行操作,有偿 有意向的可以加我v
  • ¥15 brainstorm进行致痫指数分析
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部