mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-10-10 15:36:45 +08:00
feat: 修改反向代理网站创建逻辑 (#793)
This commit is contained in:
parent
9f12a91f4a
commit
8a32d8032f
5 changed files with 84 additions and 43 deletions
|
@ -1351,7 +1351,7 @@ func (w WebsiteService) UpdateAuthBasic(req request.NginxAuthUpdate) (err error)
|
||||||
absoluteAuthPath := path.Join(nginxInstall.GetPath(), authPath)
|
absoluteAuthPath := path.Join(nginxInstall.GetPath(), authPath)
|
||||||
fileOp := files.NewFileOp()
|
fileOp := files.NewFileOp()
|
||||||
if !fileOp.Stat(path.Dir(absoluteAuthPath)) {
|
if !fileOp.Stat(path.Dir(absoluteAuthPath)) {
|
||||||
_ = fileOp.CreateDir(path.Dir(absoluteAuthPath), 755)
|
_ = fileOp.CreateDir(path.Dir(absoluteAuthPath), 0755)
|
||||||
}
|
}
|
||||||
if !fileOp.Stat(absoluteAuthPath) {
|
if !fileOp.Stat(absoluteAuthPath) {
|
||||||
_ = fileOp.CreateFile(absoluteAuthPath)
|
_ = fileOp.CreateFile(absoluteAuthPath)
|
||||||
|
|
|
@ -2,7 +2,9 @@ package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
||||||
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
||||||
|
"github.com/1Panel-dev/1Panel/backend/utils/nginx/components"
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -84,6 +86,40 @@ func createIndexFile(website *model.Website, runtime *model.Runtime) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func createProxyFile(website *model.Website, runtime *model.Runtime) error {
|
||||||
|
nginxInstall, err := getAppInstallByKey(constant.AppOpenresty)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
proxyFolder := path.Join(constant.AppInstallDir, constant.AppOpenresty, nginxInstall.Name, "www", "sites", website.Alias, "proxy")
|
||||||
|
filePath := path.Join(proxyFolder, "root.conf")
|
||||||
|
fileOp := files.NewFileOp()
|
||||||
|
if !fileOp.Stat(proxyFolder) {
|
||||||
|
if err := fileOp.CreateDir(proxyFolder, 0755); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !fileOp.Stat(filePath) {
|
||||||
|
if err := fileOp.CreateFile(filePath); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
config := parser.NewStringParser(string(nginx_conf.Proxy)).Parse()
|
||||||
|
config.FilePath = filePath
|
||||||
|
directives := config.Directives
|
||||||
|
location, ok := directives[0].(*components.Location)
|
||||||
|
if !ok {
|
||||||
|
return errors.New("error")
|
||||||
|
}
|
||||||
|
location.ChangePath("^~", "/")
|
||||||
|
location.UpdateDirective("proxy_pass", []string{website.Proxy})
|
||||||
|
location.UpdateDirective("proxy_set_header", []string{"Host", "$host"})
|
||||||
|
if err := nginx.WriteConfig(config, nginx.IndentedStyle); err != nil {
|
||||||
|
return buserr.WithErr(constant.ErrUpdateBuWebsite, err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func createWebsiteFolder(nginxInstall model.AppInstall, website *model.Website, runtime *model.Runtime) error {
|
func createWebsiteFolder(nginxInstall model.AppInstall, website *model.Website, runtime *model.Runtime) error {
|
||||||
nginxFolder := path.Join(constant.AppInstallDir, constant.AppOpenresty, nginxInstall.Name)
|
nginxFolder := path.Join(constant.AppInstallDir, constant.AppOpenresty, nginxInstall.Name)
|
||||||
siteFolder := path.Join(nginxFolder, "www", "sites", website.Alias)
|
siteFolder := path.Join(nginxFolder, "www", "sites", website.Alias)
|
||||||
|
@ -123,6 +159,11 @@ func createWebsiteFolder(nginxInstall model.AppInstall, website *model.Website,
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if website.Type == constant.Proxy {
|
||||||
|
if err := createProxyFile(website, runtime); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return fileOp.CopyDir(path.Join(nginxFolder, "www", "common", "waf", "rules"), path.Join(siteFolder, "waf"))
|
return fileOp.CopyDir(path.Join(nginxFolder, "www", "common", "waf", "rules"), path.Join(siteFolder, "waf"))
|
||||||
}
|
}
|
||||||
|
@ -167,9 +208,9 @@ func configDefaultNginx(website *model.Website, domains []model.WebsiteDomain, a
|
||||||
server.UpdateRootProxy([]string{proxy})
|
server.UpdateRootProxy([]string{proxy})
|
||||||
case constant.Static:
|
case constant.Static:
|
||||||
server.UpdateRoot(rootIndex)
|
server.UpdateRoot(rootIndex)
|
||||||
//server.UpdateRootLocation()
|
|
||||||
case constant.Proxy:
|
case constant.Proxy:
|
||||||
server.UpdateRootProxy([]string{website.Proxy})
|
nginxInclude := fmt.Sprintf("/www/sites/%s/proxy/*.conf", website.Alias)
|
||||||
|
server.UpdateDirective("include", []string{nginxInclude})
|
||||||
case constant.Runtime:
|
case constant.Runtime:
|
||||||
if runtime.Resource == constant.ResourceLocal {
|
if runtime.Resource == constant.ResourceLocal {
|
||||||
switch runtime.Type {
|
switch runtime.Type {
|
||||||
|
@ -192,7 +233,6 @@ func configDefaultNginx(website *model.Website, domains []model.WebsiteDomain, a
|
||||||
if err := nginx.WriteConfig(config, nginx.IndentedStyle); err != nil {
|
if err := nginx.WriteConfig(config, nginx.IndentedStyle); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := opNginx(nginxInstall.ContainerName, constant.NginxCheck); err != nil {
|
if err := opNginx(nginxInstall.ContainerName, constant.NginxCheck); err != nil {
|
||||||
_ = deleteWebsiteFolder(nginxInstall, website)
|
_ = deleteWebsiteFolder(nginxInstall, website)
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -1213,23 +1213,18 @@ const message = {
|
||||||
startProxy: 'Start Reverse proxy',
|
startProxy: 'Start Reverse proxy',
|
||||||
stopProxy: 'Stop the Reverse proxy',
|
stopProxy: 'Stop the Reverse proxy',
|
||||||
proxyFile: 'Source',
|
proxyFile: 'Source',
|
||||||
proxyHelper1:
|
proxyHelper1: 'When accessing this directory, the content of the target URL will be returned and displayed',
|
||||||
'Proxy directory: when accessing this directory, the content of the target URL will be returned and displayed',
|
proxyPassHelper: 'The proxy site must be a URL that can be accessed normally',
|
||||||
proxyPassHelper:
|
proxyHostHelper: 'Add the domain name to the request header and pass it to the proxy server',
|
||||||
'Target URL: You can fill in the site you need to proxy, the target URL must be a URL that can be accessed normally, otherwise an error will be returned',
|
replacementHelper: 'Up to 5 replacements can be added, please leave blank if no replacement is required',
|
||||||
proxyHostHelper:
|
modifier: 'Matching rules',
|
||||||
'Send domain name: Add the domain name to the request header and pass it to the proxy server. The default is the target URL domain name. If it is not set properly, the proxy may not work properly',
|
modifierHelper: 'Example: = exact match, ~ regular match, ^~ match the beginning of the path, etc',
|
||||||
replacementHelper:
|
replace: 'Text Replacement',
|
||||||
'Content replacement: you can add up to 3 replacement content, if you do not need to replace, please leave blank',
|
addReplace: 'Add text replacement',
|
||||||
modifier: 'Path Match',
|
replaced: 'Search string (cannot be empty)',
|
||||||
modifierHelper:
|
replaceText: 'Replace with string',
|
||||||
'Path matching: Example: = exact match, ~ regular match, ^~ match the beginning of the path, etc',
|
replacedErr: 'The search string cannot be empty',
|
||||||
replace: 'Content Replacement',
|
replacedErr2: 'The search string cannot be repeated',
|
||||||
addReplace: 'Add content to replace',
|
|
||||||
replaced: 'The replaced text cannot be empty',
|
|
||||||
replaceText: 'Replacement text, can be empty',
|
|
||||||
replacedErr: 'The replaced text cannot be empty',
|
|
||||||
replacedErr2: 'The replaced text cannot be repeated',
|
|
||||||
basicAuth: 'Password Access',
|
basicAuth: 'Password Access',
|
||||||
editBasicAuthHelper:
|
editBasicAuthHelper:
|
||||||
'The password is asymmetrically encrypted and cannot be echoed. Editing needs to reset the password',
|
'The password is asymmetrically encrypted and cannot be echoed. Editing needs to reset the password',
|
||||||
|
|
|
@ -1203,32 +1203,31 @@ const message = {
|
||||||
userGroup: '运行用户/组',
|
userGroup: '运行用户/组',
|
||||||
user: '用户',
|
user: '用户',
|
||||||
uGroup: '用户组',
|
uGroup: '用户组',
|
||||||
proxyPath: '代理目录',
|
proxyPath: '前端请求路径',
|
||||||
proxyPass: '目标URL',
|
proxyPass: '后端代理地址',
|
||||||
cache: '缓存',
|
cache: '缓存',
|
||||||
status: '状态',
|
status: '状态',
|
||||||
createProxy: '创建反向代理',
|
createProxy: '创建反向代理',
|
||||||
editProxy: '编辑反向代理',
|
editProxy: '编辑反向代理',
|
||||||
cacheTime: '缓存时间',
|
cacheTime: '缓存时间',
|
||||||
enableCache: '开启缓存',
|
enableCache: '开启缓存',
|
||||||
proxyHost: '发送域名',
|
proxyHost: '后端域名',
|
||||||
disabled: '已停止',
|
disabled: '已停止',
|
||||||
startProxy: '开启反向代理',
|
startProxy: '开启反向代理',
|
||||||
stopProxy: '关闭反向代理',
|
stopProxy: '关闭反向代理',
|
||||||
proxyFile: '源文',
|
proxyFile: '源文',
|
||||||
proxyHelper1: '代理目录:访问这个目录时将会把目标URL的内容返回并显示',
|
proxyHelper1: '访问这个目录时将会把目标URL的内容返回并显示',
|
||||||
proxyPassHelper: '目标URL:可以填写你需要代理的站点,目标URL必须为可正常访问的URL,否则将返回错误',
|
proxyPassHelper: '代理的站点,必须为可正常访问的URL',
|
||||||
proxyHostHelper:
|
proxyHostHelper: '将域名添加到请求头传递到代理服务器',
|
||||||
'发送域名:将域名添加到请求头传递到代理服务器,默认为目标URL域名,若设置不当可能导致代理无法正常运行',
|
replacementHelper: '最多可以添加5条替换内容,如果不需要替换请留空',
|
||||||
replacementHelper: '内容替换:最多可以添加3条替换内容,如果不需要替换请留空',
|
modifier: '匹配规则',
|
||||||
modifier: '路径匹配',
|
modifierHelper: '例:= 精确匹配,~ 正则匹配,^~ 匹配路径开头 等',
|
||||||
modifierHelper: '路径匹配:例:= 精确匹配,~ 正则匹配,^~ 匹配路径开头 等',
|
replace: '文本替换',
|
||||||
replace: '内容替换',
|
addReplace: '添加文本替换',
|
||||||
addReplace: '添加内容替换',
|
replaced: '搜索字符串(不能为空)',
|
||||||
replaced: '被替换的文本,不能为空',
|
replaceText: '替换为字符串',
|
||||||
replaceText: '替换的文本,可为空',
|
replacedErr: '搜索字符串不能为空',
|
||||||
replacedErr: '被替换的文本不能为空',
|
replacedErr2: '搜索字符串不能重复',
|
||||||
replacedErr2: '被替换的文本不能重复',
|
|
||||||
basicAuth: '密码访问',
|
basicAuth: '密码访问',
|
||||||
editBasicAuthHelper: '密码为非对称加密,无法回显,编辑需要重新设置密码',
|
editBasicAuthHelper: '密码为非对称加密,无法回显,编辑需要重新设置密码',
|
||||||
createPassword: '生成密码',
|
createPassword: '生成密码',
|
||||||
|
|
|
@ -11,6 +11,9 @@
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
<el-form-item :label="$t('website.modifier')" prop="modifier">
|
<el-form-item :label="$t('website.modifier')" prop="modifier">
|
||||||
<el-input v-model.trim="proxy.modifier"></el-input>
|
<el-input v-model.trim="proxy.modifier"></el-input>
|
||||||
|
<div>
|
||||||
|
<span class="input-help">{{ $t('website.modifierHelper') }}</span>
|
||||||
|
</div>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
<el-form-item :label="$t('website.proxyPath')" prop="match">
|
<el-form-item :label="$t('website.proxyPath')" prop="match">
|
||||||
<el-input v-model.trim="proxy.match"></el-input>
|
<el-input v-model.trim="proxy.match"></el-input>
|
||||||
|
@ -36,11 +39,17 @@
|
||||||
<el-col :span="12">
|
<el-col :span="12">
|
||||||
<el-form-item :label="$t('website.proxyPass')" prop="proxyPass">
|
<el-form-item :label="$t('website.proxyPass')" prop="proxyPass">
|
||||||
<el-input v-model.trim="proxy.proxyPass"></el-input>
|
<el-input v-model.trim="proxy.proxyPass"></el-input>
|
||||||
|
<div>
|
||||||
|
<span class="input-help">{{ $t('website.proxyPassHelper') }}</span>
|
||||||
|
</div>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
</el-col>
|
</el-col>
|
||||||
<el-col :span="12">
|
<el-col :span="12">
|
||||||
<el-form-item :label="$t('website.proxyHost')" prop="proxyHost">
|
<el-form-item :label="$t('website.proxyHost')" prop="proxyHost">
|
||||||
<el-input v-model.trim="proxy.proxyHost"></el-input>
|
<el-input v-model.trim="proxy.proxyHost"></el-input>
|
||||||
|
<div>
|
||||||
|
<span class="input-help">{{ $t('website.proxyHostHelper') }}</span>
|
||||||
|
</div>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
</el-col>
|
</el-col>
|
||||||
</el-row>
|
</el-row>
|
||||||
|
@ -68,16 +77,14 @@
|
||||||
</div>
|
</div>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
<el-form-item>
|
<el-form-item>
|
||||||
<el-button type="primary" @click="addReplaces" :disabled="replaces.length >= 3">
|
<el-button type="primary" @click="addReplaces" :disabled="replaces.length >= 5">
|
||||||
{{ $t('website.addReplace') }}
|
{{ $t('website.addReplace') }}
|
||||||
</el-button>
|
</el-button>
|
||||||
|
<div>
|
||||||
|
<span class="input-help">{{ $t('website.replacementHelper') }}</span>
|
||||||
|
</div>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
</el-form>
|
</el-form>
|
||||||
<el-alert :title="$t('website.modifierHelper')" type="info" :closable="false" />
|
|
||||||
<el-alert :title="$t('website.proxyHelper1')" type="info" :closable="false" />
|
|
||||||
<el-alert :title="$t('website.proxyPassHelper')" type="info" :closable="false" />
|
|
||||||
<el-alert :title="$t('website.proxyHostHelper')" type="info" :closable="false" />
|
|
||||||
<el-alert :title="$t('website.replacementHelper')" type="info" :closable="false" />
|
|
||||||
</el-col>
|
</el-col>
|
||||||
</el-row>
|
</el-row>
|
||||||
<template #footer>
|
<template #footer>
|
||||||
|
|
Loading…
Add table
Reference in a new issue