我使用docker-compose和nginx作為反向代理部署了一個FASTAPI應用程式,并將其作為一個前端。
當我試圖訪問該網站時,我得到了一個空白頁面,但其他服務(后臺)作業正常,導航欄中的favicon和網站名稱也在加載。
我查看了控制臺,似乎 react 無法定位其他靜態檔案。
server {
listen 443 ssl http2 default_server;
listen [::]: 443 ssl http2;
server_name pbl.asia www.pbl.asia;
server_tokens關閉。
location = /favicon.ico {root /usr/share/nginx/html;}.
root /usr/share/nginx/html;
index.html index.htm。
location = / {
try_files $uri /index.html。
}
位置 / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr。
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for。
proxy_pass "http://backend:8000"。
}
ssl_certificate /etc/letsencrypt/live/pbl.asia/fullchain.pem。
ssl_certificate_key /etc/letsencrypt/live/pbl.asia/privkey.pem。
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem。
}
這是我的ngnix組態檔。
# Frontend
frontend。
build:
context: frontend
container_name: frontend
depends_on:
- 后臺
volumes:
- react_build:/frontend/build
Nginx服務
nginx:
image: nginx:1.21-alpine
ports:
- 80:80.
- 443:443.
volumes:
- ./nginx:/etc/nginx/conf.d.
- ./data/certbot/conf: /etc/letsencrypt
- ./data/certbot/www: /var/www/certbot
- react_build:/usr/share/nginx/html
command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g "daemon off;"' "
depends_on。
- 后臺
- 前端
重新啟動: always
docker-compose.yaml
FROM node:16.8.0-slim
WORKDIR /frontend
COPY package.json ./
RUN npm install
COPY ./
RUN npm run build
這是我的Docker檔案
uj5u.com熱心網友回復:
在位置塊中指定索引,為我解決了這個問題
。root /usr/share/nginx/html;
location = /home {
index.html index.htm。
try_files $uri /index.html。
}
location ~ "^/([0-9a-zA-Z =-?/-_]{7,})$"/span> {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr。
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for。
proxy_pass 'http://backend:8000'。
}
還為后端部分指定了單獨的regex,否則NGINX會將所有的請求路由到后端,導致內部服務器錯誤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/316524.html
標籤:
