mirror of
https://github.com/AlistGo/alist.git
synced 2025-06-04 17:04:42 +08:00
fix!: reverse proxy to sub-directory (#3483)
from this commit, if you want reverse proxy to sub-directory like `alist` with `nginx`, you need config: ```nginx location /alist/ { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Range $http_range; proxy_set_header If-Range $http_if_range; proxy_redirect off; proxy_pass http://127.0.0.1:5244/alist/; # the max size of file to upload client_max_body_size 20000m; } ```
This commit is contained in:
@ -4,6 +4,7 @@ import (
|
||||
"github.com/alist-org/alist/v3/cmd/flags"
|
||||
"github.com/alist-org/alist/v3/internal/conf"
|
||||
"github.com/alist-org/alist/v3/internal/message"
|
||||
"github.com/alist-org/alist/v3/pkg/utils"
|
||||
"github.com/alist-org/alist/v3/server/common"
|
||||
"github.com/alist-org/alist/v3/server/handles"
|
||||
"github.com/alist-org/alist/v3/server/middlewares"
|
||||
@ -12,21 +13,27 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Init(r *gin.Engine) {
|
||||
common.SecretKey = []byte(conf.Conf.JwtSecret)
|
||||
Cors(r)
|
||||
r.Use(middlewares.StoragesLoaded)
|
||||
if conf.Conf.MaxConnections > 0 {
|
||||
r.Use(middlewares.MaxAllowed(conf.Conf.MaxConnections))
|
||||
func Init(e *gin.Engine) {
|
||||
if !utils.SliceContains([]string{"", "/"}, conf.URL.Path) {
|
||||
e.GET("/", func(c *gin.Context) {
|
||||
c.Redirect(302, conf.URL.Path)
|
||||
})
|
||||
}
|
||||
WebDav(r.Group("/dav"))
|
||||
g := e.Group(conf.URL.Path)
|
||||
common.SecretKey = []byte(conf.Conf.JwtSecret)
|
||||
Cors(g)
|
||||
g.Use(middlewares.StoragesLoaded)
|
||||
if conf.Conf.MaxConnections > 0 {
|
||||
g.Use(middlewares.MaxAllowed(conf.Conf.MaxConnections))
|
||||
}
|
||||
WebDav(g.Group("/dav"))
|
||||
|
||||
r.GET("/favicon.ico", handles.Favicon)
|
||||
r.GET("/i/:link_name", handles.Plist)
|
||||
r.GET("/d/*path", middlewares.Down, handles.Down)
|
||||
r.GET("/p/*path", middlewares.Down, handles.Proxy)
|
||||
g.GET("/favicon.ico", handles.Favicon)
|
||||
g.GET("/i/:link_name", handles.Plist)
|
||||
g.GET("/d/*path", middlewares.Down, handles.Down)
|
||||
g.GET("/p/*path", middlewares.Down, handles.Proxy)
|
||||
|
||||
api := r.Group("/api")
|
||||
api := g.Group("/api")
|
||||
auth := api.Group("", middlewares.Auth)
|
||||
|
||||
api.POST("/auth/login", handles.Login)
|
||||
@ -46,9 +53,11 @@ func Init(r *gin.Engine) {
|
||||
_fs(auth.Group("/fs"))
|
||||
admin(auth.Group("/admin", middlewares.AuthAdmin))
|
||||
if flags.Dev {
|
||||
dev(r.Group("/dev"))
|
||||
dev(g.Group("/dev"))
|
||||
}
|
||||
static.Static(r)
|
||||
static.Static(g, func(handlers ...gin.HandlerFunc) {
|
||||
e.NoRoute(handlers...)
|
||||
})
|
||||
}
|
||||
|
||||
func admin(g *gin.RouterGroup) {
|
||||
@ -124,10 +133,9 @@ func _fs(g *gin.RouterGroup) {
|
||||
g.POST("/add_qbit", handles.AddQbittorrent)
|
||||
}
|
||||
|
||||
func Cors(r *gin.Engine) {
|
||||
func Cors(r *gin.RouterGroup) {
|
||||
config := cors.DefaultConfig()
|
||||
config.AllowAllOrigins = true
|
||||
//config.AllowHeaders = append(config.AllowHeaders, "Authorization", "range", "File-Path", "As-Task", "Password")
|
||||
config.AllowHeaders = []string{"*"}
|
||||
config.AllowMethods = []string{"*"}
|
||||
r.Use(cors.New(config))
|
||||
|
@ -1,7 +1,6 @@
|
||||
package static
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/alist-org/alist/v3/internal/conf"
|
||||
@ -15,20 +14,16 @@ type SiteConfig struct {
|
||||
}
|
||||
|
||||
func getSiteConfig() SiteConfig {
|
||||
u, err := url.Parse(conf.Conf.SiteURL)
|
||||
if err != nil {
|
||||
utils.Log.Fatalf("can't parse site_url: %+v", err)
|
||||
}
|
||||
siteConfig := SiteConfig{
|
||||
ApiURL: conf.Conf.SiteURL,
|
||||
BasePath: u.Path,
|
||||
BasePath: conf.URL.Path,
|
||||
Cdn: strings.ReplaceAll(strings.TrimSuffix(conf.Conf.Cdn, "/"), "$version", conf.WebVersion),
|
||||
}
|
||||
if siteConfig.BasePath != "" {
|
||||
siteConfig.BasePath = utils.FixAndCleanPath(siteConfig.BasePath)
|
||||
}
|
||||
if siteConfig.Cdn == "" {
|
||||
siteConfig.Cdn = siteConfig.BasePath
|
||||
siteConfig.Cdn = strings.TrimSuffix(siteConfig.BasePath, "/")
|
||||
}
|
||||
return siteConfig
|
||||
}
|
||||
|
@ -62,7 +62,7 @@ func UpdateIndex() {
|
||||
}
|
||||
}
|
||||
|
||||
func Static(r *gin.Engine) {
|
||||
func Static(r *gin.RouterGroup, noRoute func(handlers ...gin.HandlerFunc)) {
|
||||
InitIndex()
|
||||
folders := []string{"assets", "images", "streamer", "static"}
|
||||
r.Use(func(c *gin.Context) {
|
||||
@ -81,7 +81,7 @@ func Static(r *gin.Engine) {
|
||||
r.StaticFS(fmt.Sprintf("/%s/", folders[i]), http.FS(sub))
|
||||
}
|
||||
|
||||
r.NoRoute(func(c *gin.Context) {
|
||||
noRoute(func(c *gin.Context) {
|
||||
c.Header("Content-Type", "text/html")
|
||||
c.Status(200)
|
||||
if strings.HasPrefix(c.Request.URL.Path, "/@manage") {
|
||||
|
@ -3,7 +3,9 @@ package server
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"path"
|
||||
|
||||
"github.com/alist-org/alist/v3/internal/conf"
|
||||
"github.com/alist-org/alist/v3/internal/model"
|
||||
"github.com/alist-org/alist/v3/internal/op"
|
||||
"github.com/alist-org/alist/v3/pkg/utils"
|
||||
@ -14,17 +16,14 @@ import (
|
||||
|
||||
var handler *webdav.Handler
|
||||
|
||||
func init() {
|
||||
func WebDav(dav *gin.RouterGroup) {
|
||||
handler = &webdav.Handler{
|
||||
Prefix: "/dav",
|
||||
Prefix: path.Join(conf.URL.Path, "/dav"),
|
||||
LockSystem: webdav.NewMemLS(),
|
||||
Logger: func(request *http.Request, err error) {
|
||||
log.Errorf("%s %s %+v", request.Method, request.URL.Path, err)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func WebDav(dav *gin.RouterGroup) {
|
||||
dav.Use(WebDAVAuth)
|
||||
dav.Any("/*path", ServeWebDAV)
|
||||
dav.Any("", ServeWebDAV)
|
||||
|
Reference in New Issue
Block a user