feat(config): 更新环境配置和Nginx设置以支持动态端口配置
- 修改 .env.example 文件中的客户端API基础路径配置 - 将Dockerfile中的nginx.conf复制到模板目录以支持环境变量 - 在nginx配置中使用环境变量替换硬编码端口 - 为API文档路径(/docs、/redoc、/openapi.json)添加代理配置 - 移除硬编码的服务器地址,改用相对路径配置 - 更新docker-compose.yml以传递内部端口环境变量 - 简化nginx反向代理配置,移除冗余的服务器块配置
This commit is contained in:
+2
-4
@@ -45,14 +45,12 @@ CLIENTS_NGINX_IMAGE=nginx:1.27-alpine
|
||||
CLIENTS_INTERNAL_PORT=80
|
||||
VITE_CLIENT_H5_API_BASE=/api/v1
|
||||
VITE_CLIENT_H5_SERVER_ORIGIN=
|
||||
VITE_CLIENT_NATIVE_API_BASE=http://10.0.10.11:8000/api/v1
|
||||
VITE_CLIENT_NATIVE_SERVER_ORIGIN=http://10.0.10.11:8000
|
||||
VITE_CLIENT_NATIVE_API_BASE=/api/v1
|
||||
VITE_CLIENT_NATIVE_SERVER_ORIGIN=
|
||||
|
||||
# Nginx host ports
|
||||
NGINX_IMAGE=nginx:1.27-alpine
|
||||
NGINX_CLIENT_INTERNAL_PORT=80
|
||||
NGINX_ADMIN_INTERNAL_PORT=81
|
||||
NGINX_SERVER_INTERNAL_PORT=82
|
||||
CLIENT_WEB_PORT=5173
|
||||
ADMIN_WEB_PORT=5174
|
||||
SERVER_WEB_PORT=8000
|
||||
|
||||
@@ -16,7 +16,7 @@ RUN npm run build
|
||||
|
||||
FROM ${ADMIN_WEB_NGINX_IMAGE}
|
||||
|
||||
COPY nginx.conf /etc/nginx/conf.d/default.conf
|
||||
COPY nginx.conf /etc/nginx/templates/default.conf.template
|
||||
COPY --from=build /app/dist /usr/share/nginx/html
|
||||
|
||||
EXPOSE 80
|
||||
|
||||
+30
-3
@@ -1,12 +1,12 @@
|
||||
server {
|
||||
listen 80;
|
||||
listen ${ADMIN_WEB_INTERNAL_PORT};
|
||||
server_name _;
|
||||
|
||||
root /usr/share/nginx/html;
|
||||
index index.html;
|
||||
|
||||
location /api/ {
|
||||
proxy_pass http://server:8000/api/;
|
||||
proxy_pass http://server:${SERVER_INTERNAL_PORT}/api/;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
@@ -15,11 +15,38 @@ server {
|
||||
}
|
||||
|
||||
location /uploads/ {
|
||||
proxy_pass http://server:8000/uploads/;
|
||||
proxy_pass http://server:${SERVER_INTERNAL_PORT}/uploads/;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
}
|
||||
|
||||
location = /docs {
|
||||
proxy_pass http://server:${SERVER_INTERNAL_PORT}/docs;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
|
||||
location = /redoc {
|
||||
proxy_pass http://server:${SERVER_INTERNAL_PORT}/redoc;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
|
||||
location = /openapi.json {
|
||||
proxy_pass http://server:${SERVER_INTERNAL_PORT}/openapi.json;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
|
||||
location / {
|
||||
try_files $uri $uri/ /index.html;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { clearAdminToken, getAdminToken, setAdminToken, setAdminUser } from './a
|
||||
|
||||
const API_BASE
|
||||
= import.meta.env.VITE_API_BASE
|
||||
|| (import.meta.env.DEV ? 'http://127.0.0.1:8000/api/v1' : '/api/v1')
|
||||
|| '/api/v1'
|
||||
|
||||
type RequestError = Error & { status?: number }
|
||||
|
||||
|
||||
+1
-1
@@ -24,7 +24,7 @@ RUN npm run build:h5
|
||||
|
||||
FROM ${CLIENTS_NGINX_IMAGE}
|
||||
|
||||
COPY nginx.conf /etc/nginx/conf.d/default.conf
|
||||
COPY nginx.conf /etc/nginx/templates/default.conf.template
|
||||
COPY --from=build /app/dist/build/h5 /usr/share/nginx/html
|
||||
|
||||
EXPOSE 80
|
||||
|
||||
+30
-3
@@ -1,12 +1,12 @@
|
||||
server {
|
||||
listen 80;
|
||||
listen ${CLIENTS_INTERNAL_PORT};
|
||||
server_name _;
|
||||
|
||||
root /usr/share/nginx/html;
|
||||
index index.html;
|
||||
|
||||
location /api/ {
|
||||
proxy_pass http://server:8000/api/;
|
||||
proxy_pass http://server:${SERVER_INTERNAL_PORT}/api/;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
@@ -15,11 +15,38 @@ server {
|
||||
}
|
||||
|
||||
location /uploads/ {
|
||||
proxy_pass http://server:8000/uploads/;
|
||||
proxy_pass http://server:${SERVER_INTERNAL_PORT}/uploads/;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
}
|
||||
|
||||
location = /docs {
|
||||
proxy_pass http://server:${SERVER_INTERNAL_PORT}/docs;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
|
||||
location = /redoc {
|
||||
proxy_pass http://server:${SERVER_INTERNAL_PORT}/redoc;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
|
||||
location = /openapi.json {
|
||||
proxy_pass http://server:${SERVER_INTERNAL_PORT}/openapi.json;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
|
||||
location / {
|
||||
try_files $uri $uri/ /index.html;
|
||||
}
|
||||
|
||||
@@ -1,14 +1,12 @@
|
||||
// #ifdef H5
|
||||
const API_BASE = import.meta.env.VITE_CLIENT_H5_API_BASE || "/api/v1";
|
||||
const SERVER_ORIGIN =
|
||||
import.meta.env.VITE_CLIENT_H5_SERVER_ORIGIN || window.location.origin;
|
||||
const SERVER_ORIGIN = import.meta.env.VITE_CLIENT_H5_SERVER_ORIGIN || "";
|
||||
// #endif
|
||||
|
||||
// #ifndef H5
|
||||
const API_BASE =
|
||||
import.meta.env.VITE_CLIENT_NATIVE_API_BASE || "http://10.0.10.11:8000/api/v1";
|
||||
const SERVER_ORIGIN =
|
||||
import.meta.env.VITE_CLIENT_NATIVE_SERVER_ORIGIN || "http://10.0.10.11:8000";
|
||||
import.meta.env.VITE_CLIENT_NATIVE_API_BASE || "/api/v1";
|
||||
const SERVER_ORIGIN = import.meta.env.VITE_CLIENT_NATIVE_SERVER_ORIGIN || "";
|
||||
// #endif
|
||||
|
||||
export { API_BASE, SERVER_ORIGIN };
|
||||
|
||||
+6
-3
@@ -118,6 +118,9 @@ services:
|
||||
ADMIN_WEB_NGINX_IMAGE: "${ADMIN_WEB_NGINX_IMAGE}"
|
||||
VITE_API_BASE: "${VITE_API_BASE}"
|
||||
container_name: ciyuan-admin-web
|
||||
environment:
|
||||
ADMIN_WEB_INTERNAL_PORT: "${ADMIN_WEB_INTERNAL_PORT}"
|
||||
SERVER_INTERNAL_PORT: "${SERVER_INTERNAL_PORT}"
|
||||
expose:
|
||||
- "${ADMIN_WEB_INTERNAL_PORT}"
|
||||
depends_on:
|
||||
@@ -138,6 +141,9 @@ services:
|
||||
VITE_CLIENT_NATIVE_API_BASE: "${VITE_CLIENT_NATIVE_API_BASE}"
|
||||
VITE_CLIENT_NATIVE_SERVER_ORIGIN: "${VITE_CLIENT_NATIVE_SERVER_ORIGIN}"
|
||||
container_name: ciyuan-clients
|
||||
environment:
|
||||
CLIENTS_INTERNAL_PORT: "${CLIENTS_INTERNAL_PORT}"
|
||||
SERVER_INTERNAL_PORT: "${SERVER_INTERNAL_PORT}"
|
||||
expose:
|
||||
- "${CLIENTS_INTERNAL_PORT}"
|
||||
depends_on:
|
||||
@@ -153,14 +159,11 @@ services:
|
||||
environment:
|
||||
NGINX_CLIENT_INTERNAL_PORT: "${NGINX_CLIENT_INTERNAL_PORT}"
|
||||
NGINX_ADMIN_INTERNAL_PORT: "${NGINX_ADMIN_INTERNAL_PORT}"
|
||||
NGINX_SERVER_INTERNAL_PORT: "${NGINX_SERVER_INTERNAL_PORT}"
|
||||
SERVER_INTERNAL_PORT: "${SERVER_INTERNAL_PORT}"
|
||||
CLIENTS_INTERNAL_PORT: "${CLIENTS_INTERNAL_PORT}"
|
||||
ADMIN_WEB_INTERNAL_PORT: "${ADMIN_WEB_INTERNAL_PORT}"
|
||||
ports:
|
||||
- "${CLIENT_WEB_PORT}:${NGINX_CLIENT_INTERNAL_PORT}"
|
||||
- "${ADMIN_WEB_PORT}:${NGINX_ADMIN_INTERNAL_PORT}"
|
||||
- "${SERVER_WEB_PORT}:${NGINX_SERVER_INTERNAL_PORT}"
|
||||
volumes:
|
||||
- ./docker/nginx/default.conf:/etc/nginx/templates/default.conf.template:ro
|
||||
depends_on:
|
||||
|
||||
@@ -2,48 +2,6 @@ server {
|
||||
listen ${NGINX_CLIENT_INTERNAL_PORT};
|
||||
server_name _;
|
||||
|
||||
location /api/ {
|
||||
proxy_pass http://server:${SERVER_INTERNAL_PORT}/api/;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
|
||||
location /uploads/ {
|
||||
proxy_pass http://server:${SERVER_INTERNAL_PORT}/uploads/;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
}
|
||||
|
||||
location = /docs {
|
||||
proxy_pass http://server:${SERVER_INTERNAL_PORT}/docs;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
|
||||
location = /redoc {
|
||||
proxy_pass http://server:${SERVER_INTERNAL_PORT}/redoc;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
|
||||
location = /openapi.json {
|
||||
proxy_pass http://server:${SERVER_INTERNAL_PORT}/openapi.json;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
|
||||
location / {
|
||||
proxy_pass http://clients:${CLIENTS_INTERNAL_PORT};
|
||||
proxy_http_version 1.1;
|
||||
@@ -58,48 +16,6 @@ server {
|
||||
listen ${NGINX_ADMIN_INTERNAL_PORT};
|
||||
server_name _;
|
||||
|
||||
location /api/ {
|
||||
proxy_pass http://server:${SERVER_INTERNAL_PORT}/api/;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
|
||||
location /uploads/ {
|
||||
proxy_pass http://server:${SERVER_INTERNAL_PORT}/uploads/;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
}
|
||||
|
||||
location = /docs {
|
||||
proxy_pass http://server:${SERVER_INTERNAL_PORT}/docs;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
|
||||
location = /redoc {
|
||||
proxy_pass http://server:${SERVER_INTERNAL_PORT}/redoc;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
|
||||
location = /openapi.json {
|
||||
proxy_pass http://server:${SERVER_INTERNAL_PORT}/openapi.json;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
|
||||
location / {
|
||||
proxy_pass http://admin-web:${ADMIN_WEB_INTERNAL_PORT};
|
||||
proxy_http_version 1.1;
|
||||
@@ -109,17 +25,3 @@ server {
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
}
|
||||
|
||||
server {
|
||||
listen ${NGINX_SERVER_INTERNAL_PORT};
|
||||
server_name _;
|
||||
|
||||
location / {
|
||||
proxy_pass http://server:${SERVER_INTERNAL_PORT};
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user