magic/cmd/nginx/template.go
2026-03-28 19:25:12 +08:00

98 lines
2.7 KiB
Go

package nginx
import (
"context"
"fmt"
"strings"
"gitea.micah.wiki/pandora/magic/config"
)
var (
domainKey = "${Domain}"
logPathKey = "${LogPath}"
sslPathKey = "${SSLPath}"
certPathKey = "${CertPath}"
rootPathKey = "${RootPath}"
proxyKey = "${Proxy}"
httpTemplate = `
server {
listen 80;
server_name ${Domain};
rewrite ^(.*)$ https://${server_name}$1 permanent;
}
`
httpsTemplate = `
server {
listen 443 ssl;
server_name ${Domain};
error_log ${LogPath}/${Domain}.error.log;
access_log ${LogPath}/${Domain}.access.log main;
ssl_certificate ${SSLPath}/${CertPath}cert.pem;
ssl_certificate_key ${SSLPath}/${CertPath}key.pem;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
root ${RootPath};
location / {
proxy_pass ${Proxy};
#开启websocket
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_set_header X-Forwarded-Proto $scheme;
# proxy_set_header X-Forwarded-Ssl on;
proxy_set_header Accept-Encoding "";
proxy_connect_timeout 86400;
proxy_send_timeout 86400;
proxy_read_timeout 86400;
send_timeout 86400;
client_max_body_size 100g;
client_body_buffer_size 128k;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location = /robots.txt {}
}
`
)
func GetInfo(ctx context.Context, nginx *config.Nginx) string {
nginxConf := GetHTTPsInfo(ctx, nginx.Domain, nginx.RootPath, nginx.LogPath, nginx.SSLPath, nginx.CertPrefix, nginx.Proxy)
if nginx.HTTPDirect {
nginxConf = fmt.Sprintf("%s\n%s", nginxConf, GetHTTPInfo(ctx, nginx.Domain))
}
return nginxConf
}
func GetHTTPInfo(_ context.Context, domain string) string {
return strings.ReplaceAll(httpTemplate, domainKey, domain)
}
func GetHTTPsInfo(_ context.Context, domain, rootPath, logPath, sslPath, certPrefix, proxy string) string {
info := strings.ReplaceAll(httpsTemplate, domainKey, domain)
info = strings.ReplaceAll(info, rootPathKey, rootPath)
info = strings.ReplaceAll(info, logPathKey, logPath)
info = strings.ReplaceAll(info, sslPathKey, sslPath)
info = strings.ReplaceAll(info, certPathKey, certPrefix)
info = strings.ReplaceAll(info, proxyKey, proxy)
return info
}