1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-03-14 01:34:47 +08:00

feat: 创建静态、PHP 网站默认增加 404 页面 (#5498)

This commit is contained in:
zhengkunwang 2024-06-19 14:08:58 +08:00 committed by GitHub
parent 4c59090180
commit f59bea27e8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 35 additions and 14 deletions

View File

@ -346,7 +346,8 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) (err error)
} }
if len(create.FtpUser) != 0 && len(create.FtpPassword) != 0 { if len(create.FtpUser) != 0 && len(create.FtpPassword) != 0 {
itemID, err := NewIFtpService().Create(dto.FtpCreate{User: create.FtpUser, Password: create.FtpPassword, Path: path.Join(global.CONF.System.BaseDir, "1panel/apps/openresty/openresty/www/sites", website.Alias, "index")}) indexDir := path.Join(nginxInstall.GetPath(), "www", "sites", website.Alias, "index")
itemID, err := NewIFtpService().Create(dto.FtpCreate{User: create.FtpUser, Password: create.FtpPassword, Path: indexDir})
if err != nil { if err != nil {
global.LOG.Errorf("create ftp for website failed, err: %v", err) global.LOG.Errorf("create ftp for website failed, err: %v", err)
} }

View File

@ -80,18 +80,23 @@ func createIndexFile(website *model.Website, runtime *model.Runtime) error {
if err != nil { if err != nil {
return err return err
} }
var (
indexPath string
indexContent string
websiteService = NewIWebsiteService()
indexFolder = path.Join(nginxInstall.GetPath(), "www", "sites", website.Alias, "index")
)
indexFolder := path.Join(constant.AppInstallDir, constant.AppOpenresty, nginxInstall.Name, "www", "sites", website.Alias, "index")
indexPath := ""
indexContent := ""
switch website.Type { switch website.Type {
case constant.Static: case constant.Static:
indexPath = path.Join(indexFolder, "index.html") indexPath = path.Join(indexFolder, "index.html")
indexContent = string(nginx_conf.Index) indexHtml, _ := websiteService.GetDefaultHtml("index")
indexContent = indexHtml.Content
case constant.Runtime: case constant.Runtime:
if runtime.Type == constant.RuntimePHP { if runtime.Type == constant.RuntimePHP {
indexPath = path.Join(indexFolder, "index.php") indexPath = path.Join(indexFolder, "index.php")
indexContent = string(nginx_conf.IndexPHP) indexPhp, _ := websiteService.GetDefaultHtml("php")
indexContent = indexPhp.Content
} }
} }
@ -114,6 +119,13 @@ func createIndexFile(website *model.Website, runtime *model.Runtime) error {
if err := fileOp.WriteFile(indexPath, strings.NewReader(indexContent), 0755); err != nil { if err := fileOp.WriteFile(indexPath, strings.NewReader(indexContent), 0755); err != nil {
return err return err
} }
html404, _ := websiteService.GetDefaultHtml("404")
path404 := path.Join(indexFolder, "404.html")
if err := fileOp.WriteFile(path404, strings.NewReader(html404.Content), 0755); err != nil {
return err
}
return nil return nil
} }
@ -245,12 +257,14 @@ 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.UpdateDirective("error_page", []string{"404", "/404.html"})
case constant.Proxy: case constant.Proxy:
nginxInclude := fmt.Sprintf("/www/sites/%s/proxy/*.conf", website.Alias) nginxInclude := fmt.Sprintf("/www/sites/%s/proxy/*.conf", website.Alias)
server.UpdateDirective("include", []string{nginxInclude}) server.UpdateDirective("include", []string{nginxInclude})
case constant.Runtime: case constant.Runtime:
switch runtime.Type { switch runtime.Type {
case constant.RuntimePHP: case constant.RuntimePHP:
server.UpdateDirective("error_page", []string{"404", "/404.html"})
if runtime.Resource == constant.ResourceLocal { if runtime.Resource == constant.ResourceLocal {
switch runtime.Type { switch runtime.Type {
case constant.RuntimePHP: case constant.RuntimePHP:

View File

@ -2039,8 +2039,9 @@ const message = {
domain404: 'Website page does not exist', domain404: 'Website page does not exist',
indexHtml: 'Static website default page', indexHtml: 'Static website default page',
stopHtml: 'Website stop page', stopHtml: 'Website stop page',
indePhp: 'PHP website default page', indexPHP: 'PHP website default page',
sslExpireDate: 'Certificate expiration date', sslExpireDate: 'Certificate expiration date',
website404Helper: 'Website 404 error page only supports PHP runtime environment websites and static websites',
}, },
php: { php: {
short_open_tag: 'Short tag support', short_open_tag: 'Short tag support',

View File

@ -1898,8 +1898,9 @@ const message = {
domain404: '網站不存在頁面', domain404: '網站不存在頁面',
indexHtml: '靜態網站預設頁', indexHtml: '靜態網站預設頁',
stopHtml: '網站停用頁', stopHtml: '網站停用頁',
indePhp: 'PHP 網站預設頁', indexPHP: 'PHP 網站預設頁',
sslExpireDate: '憑證過期時間', sslExpireDate: '憑證過期時間',
website404Helper: '網站 404 錯誤頁僅支援 PHP 運行環境網站和靜態網站',
}, },
php: { php: {
short_open_tag: '短標簽支持', short_open_tag: '短標簽支持',

View File

@ -1900,8 +1900,9 @@ const message = {
domain404: '网站不存在页', domain404: '网站不存在页',
indexHtml: '静态网站默认页', indexHtml: '静态网站默认页',
stopHtml: '网站停用页', stopHtml: '网站停用页',
indePhp: 'PHP 网站默认页', indexPHP: 'PHP 网站默认页',
sslExpireDate: '证书过期时间', sslExpireDate: '证书过期时间',
website404Helper: '网站 404 错误页仅支持 PHP 运行环境网站和静态网站',
}, },
php: { php: {
short_open_tag: '短标签支持', short_open_tag: '短标签支持',

View File

@ -9,18 +9,22 @@
<el-option :value="'404'" :label="$t('website.website404')"></el-option> <el-option :value="'404'" :label="$t('website.website404')"></el-option>
<el-option :value="'domain404'" :label="$t('website.domain404')"></el-option> <el-option :value="'domain404'" :label="$t('website.domain404')"></el-option>
<el-option :value="'index'" :label="$t('website.indexHtml')"></el-option> <el-option :value="'index'" :label="$t('website.indexHtml')"></el-option>
<el-option :value="'php'" :label="$t('website.indePhp')"></el-option> <el-option :value="'php'" :label="$t('website.indexPHP')"></el-option>
<el-option :value="'stop'" :label="$t('website.stopHtml')"></el-option> <el-option :value="'stop'" :label="$t('website.stopHtml')"></el-option>
</el-select> </el-select>
<div class="mt-1.5">
<el-text v-if="type == '404'" type="info">
{{ $t('website.website404Helper') }}
</el-text>
</div>
<div ref="htmlRef" class="default-html"></div> <div ref="htmlRef" class="default-html"></div>
</el-col> </el-col>
</el-row> </el-row>
<template #footer> <template #footer>
<span class="dialog-footer"> <span class="dialog-footer">
<el-button @click="handleClose" :disabled="loading">{{ $t('commons.button.cancel') }}</el-button> <el-button @click="handleClose" :disabled="loading">{{ $t('commons.button.cancel') }}</el-button>
<el-button type="primary" @click="submit()" :disabled="loading"> <el-button type="primary" @click="submit()" :disabled="loading">
{{ $t('commons.button.confirm') }} {{ $t('commons.button.save') }}
</el-button> </el-button>
</span> </span>
</template> </template>
@ -89,7 +93,6 @@ const submit = async () => {
const content = view.value.state.doc.toString(); const content = view.value.state.doc.toString();
await UpdateDefaultHtml({ type: type.value, content: content }); await UpdateDefaultHtml({ type: type.value, content: content });
MsgSuccess(i18n.global.t('commons.msg.updateSuccess')); MsgSuccess(i18n.global.t('commons.msg.updateSuccess'));
handleClose();
} catch (error) { } catch (error) {
} finally { } finally {
loading.value = false; loading.value = false;
@ -102,6 +105,6 @@ defineExpose({ acceptParams });
.default-html { .default-html {
width: 100%; width: 100%;
min-height: 300px; min-height: 300px;
margin-top: 20px; margin-top: 10px;
} }
</style> </style>