fix: nginx配置问题处理
This commit is contained in:
1 parent
df34fc30cc
commit
fa0dc7e7e1
4 files changed
+38
-4
No files matched your search
+2
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -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
|
||||
)
|
||||
@@ -7,10 +7,33 @@ import (
|
||||
// CORSMiddleware 跨域中间件
|
||||
func CORSMiddleware() gin.HandlerFunc {
|
||||
return gin.HandlerFunc(func(c *gin.Context) {
|
||||
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)
|
||||
|
||||
+11
-1
@@ -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
|
||||
|
||||
Reference in new issue
Block a user