fix: nginx配置问题处理

This commit is contained in:
wood911010 committed 2025-08-08 20:33:16 +08:00
1 parent df34fc30cc
commit fa0dc7e7e1
4 files changed
+42 -8

No files matched your search

+4 -3
View File
@@ -8,8 +8,8 @@ import (
"license-manager/internal/api/routes"
"license-manager/internal/config"
"license-manager/internal/database"
"license-manager/pkg/logger"
"license-manager/pkg/i18n"
"license-manager/pkg/logger"
"github.com/gin-gonic/gin"
)
@@ -28,6 +28,7 @@ import (
// @host localhost:18888
// @BasePath /
// @schemes http https
// @securityDefinitions.apikey BearerAuth
// @in header
@@ -40,7 +41,7 @@ func main() {
if _, err := os.Stat(configPath); os.IsNotExist(err) {
configPath = "../configs/config.yaml" // 本地开发环境
}
if err := config.Load(configPath); err != nil {
log.Fatalf("Failed to load config: %v", err)
}
@@ -69,7 +70,7 @@ func main() {
if err := i18n.InitGlobalManager(cfg.I18n.ConfigPath, cfg.I18n.DefaultLang); err != nil {
log.Fatalf("多语言初始化失败: %v", err)
}
// 预加载支持的语言
for _, lang := range cfg.I18n.SupportLangs {
if err := i18n.LoadLanguage(lang); err != nil {
+1 -1
View File
@@ -11,6 +11,7 @@ require (
github.com/swaggo/gin-swagger v1.6.0
github.com/swaggo/swag v1.16.3
golang.org/x/crypto v0.28.0
gopkg.in/yaml.v2 v2.4.0
gorm.io/driver/mysql v1.6.0
gorm.io/gorm v1.30.1
)
@@ -69,6 +70,5 @@ require (
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
+26 -3
View File
@@ -7,10 +7,33 @@ import (
// CORSMiddleware 跨域中间件
func CORSMiddleware() gin.HandlerFunc {
return gin.HandlerFunc(func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
origin := c.Request.Header.Get("Origin")
// 允许的域名列表
allowedOrigins := map[string]bool{
"http://localhost:3000": true, // 前端开发服务器
"http://localhost:8080": true, // 前端开发服务器 (Vite)
"http://localhost:18888": true, // Swagger UI
"http://127.0.0.1:18888": true, // Swagger UI (IPv4)
"": true, // 同源请求
}
// 如果是允许的域名或者没有Origin头(同源请求),则允许
if allowedOrigins[origin] || origin == "" {
if origin != "" {
c.Header("Access-Control-Allow-Origin", origin)
} else {
c.Header("Access-Control-Allow-Origin", "*")
}
} else {
// 对于其他域名,也允许(开发环境)
c.Header("Access-Control-Allow-Origin", "*")
}
c.Header("Access-Control-Allow-Credentials", "true")
c.Header("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Header("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT, DELETE")
c.Header("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT, DELETE, PATCH")
c.Header("Access-Control-Max-Age", "86400") // 24小时
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
@@ -19,4 +42,4 @@ func CORSMiddleware() gin.HandlerFunc {
c.Next()
})
}
}
+11 -1
View File
@@ -41,7 +41,7 @@ server {
# API proxy to backend (if backend is on the same server)
location /api/ {
proxy_pass http://host.docker.internal:18888/;
proxy_pass http://host.docker.internal:18888/api/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
@@ -49,6 +49,16 @@ server {
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
# 添加 CORS 头支持
proxy_pass_header Access-Control-Allow-Origin;
proxy_pass_header Access-Control-Allow-Methods;
proxy_pass_header Access-Control-Allow-Headers;
proxy_pass_header Access-Control-Allow-Credentials;
# 添加日志用于调试
access_log /var/log/nginx/api_proxy.log;
error_log /var/log/nginx/api_proxy_error.log;
}
# Disable access to hidden files