gpt4 book ai didi

reactjs - 通过nginx请求api server(Golang)失败返回404错误

转载 作者:数据小太阳 更新时间:2023-10-29 03:36:08 26 4
gpt4 key购买 nike

我在docker环境下搭建nginx。
当我尝试通过nginx端口访问api服务器时,请求返回404错误。

这是堆栈。

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

完整的源代码在这里:
https://github.com/jpskgc/article

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

这里是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

这是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;
}
}

客户端也有default.conf

server {
listen 3000;

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

我希望 nginx 反向代理到端口 2345 api 服务器。
但实际返回404响应。

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" "-"

另外,当访问实际部署的 url 时,它现在返回 502 Bad Gateway
http://multidocker-env.vwnrixavuv.ap-northeast-1.elasticbeanstalk.com/api/articles

最佳答案

正如 Enix 在评论中建议的那样,在删除 rewrite 指令后,问题得到解决。

重写/api/(.*)/$1 中断;

关于reactjs - 通过nginx请求api server(Golang)失败返回404错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57438522/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com