我有一個位于 docker 容器中的 React 專案。該應用程式已構建并在 Nginx 上運行。如以下 docker-compose 檔案所示,此 React 應用程式可以呼叫api容器
version: '3.9'
services:
# UI Container spec. note that 'ui' is the name of the container internally (also 'container_name')
ui:
container_name: ps-ui-prod
image: ps-ui-prod
build:
context: ./UI
dockerfile: DockerFile_UI.prod
ports:
- 1337:80
# Database Container spec.
sql:
container_name: ps-sql
image: ps-sql
environment:
ACCEPT_EULA: 'Y'
SA_PASSWORD: 'Pa55w0rd'
build:
context: ./DockerDB
dockerfile: DockerFile_SQL
ports:
- 1633:1433 # Map 1433 from inside the container to 1633 host to avoid port conflict with local install
# API container spec.
api:
container_name: ps-api
image: ps-api
build:
context: ./Api
dockerfile: DockerFile_API
environment:
ASPNETCORE_ENVIRONMENT: Development
ASPNETCORE_URLS: http:// :5555
ports:
- "5555:5555"
api容器的IP地址為:
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ps-api
172.21.0.3
React 應用程式的代理也設定為"proxy": "http://api:5555",
Nginx 組態檔如下:
server {
listen 80;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
我知道我應該添加配置來處理 POST 請求,但是當 POST 地址是容器時,我不知道該怎么做。此外,我應該說所有三個容器都可以相互ping通:

問題是當我呼叫api瀏覽器時回傳 405 錯誤:

uj5u.com熱心網友回復:
您需要將您的 graphql 請求代理到您的 API 后端,例如,將以下內容添加到您的 nginx 配置服務器塊(在您的情況下為 UI 服務):
location /api/graphql {
proxy_pass http://api:5555/api/graphql;
}
這可能會解決發送到/api/graphql端點的請求的問題。我想對/api端點的所有請求都存在同樣的問題。如果是這種情況,您可能希望通過從上述代碼段中/api洗掉部分來代理所有請求。/graphql
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/503819.html
