diff --git a/.dockerignore b/.dockerignore index f037083..db7b6f7 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,3 +1,5 @@ .git qlub -nginx.conf \ No newline at end of file +nginx.conf +conf.d +site \ No newline at end of file diff --git a/.gitignore b/.gitignore index b7f463e..cf096c8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ **nginx.conf -qlub \ No newline at end of file +qlub +conf.d \ No newline at end of file diff --git a/README.md b/README.md index 513b9af..c457cf9 100644 --- a/README.md +++ b/README.md @@ -23,10 +23,10 @@ Para instalar `qlub`, puedes descargar el binario desde el siguiente enlace: -- [Descargar qlub](https://github.com/njavilas2015/qlub/releases/download/v1.0.2/qlub) +- [Descargar qlub](https://github.com/njavilas2015/qlub/releases/download/v1.0.3/qlub) ```bash -wget https://github.com/njavilas2015/qlub/releases/download/v1.0.2/qlub +wget https://github.com/njavilas2015/qlub/releases/download/v1.0.3/qlub ``` Después de descargar, asegúrate de que el binario sea ejecutable y mueve el archivo a un directorio en tu `PATH`: @@ -98,6 +98,8 @@ services: volumes: - ./nginx.conf:/etc/nginx/nginx.conf:ro + - ./conf.d/:/etc/nginx/conf.d/ + - /mnt/md0/data/letsencrypt/:/etc/letsencrypt/ depends_on: @@ -119,6 +121,7 @@ services: volumes: - ./subdomains.json:/app/subdomains.json - ./nginx.conf:/app/nginx.conf + - ./conf.d/:/app/conf.d/ ``` ## Build diff --git a/docker-compose.yml b/docker-compose.yml index 1a149df..e8aed5d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,6 +7,8 @@ services: volumes: - ./nginx.conf:/etc/nginx/nginx.conf:ro + - ./conf.d/:/etc/nginx/conf.d/ + - /mnt/md0/data/letsencrypt/:/etc/letsencrypt/ depends_on: @@ -27,4 +29,5 @@ services: image: njavilas/qlub:server volumes: - ./subdomains.json:/app/subdomains.json - - ./nginx.conf:/app/nginx.conf \ No newline at end of file + - ./nginx.conf:/app/nginx.conf + - ./conf.d/:/app/conf.d/ \ No newline at end of file diff --git a/internal/config/generator.go b/internal/config/generator.go index 405efff..f365ec0 100644 --- a/internal/config/generator.go +++ b/internal/config/generator.go @@ -7,8 +7,9 @@ import ( "text/template" ) -func GenerateNginxConfig(subdomains []Subdomain) error { - file, err := os.Create("nginx.conf") +func write(fileName string, name string, text string, data any) { + + file, err := os.Create(fileName) if err != nil { log.Fatalf("Error creating nginx.conf: %v", err) @@ -16,17 +17,38 @@ func GenerateNginxConfig(subdomains []Subdomain) error { defer file.Close() - rawTemplate, err := template.New("nginx").Parse(NginxTemplate) + rawTemplate, err := template.New(name).Parse(text) if err != nil { log.Fatalf("Error parsing template: %v", err) } - if err := rawTemplate.Execute(file, subdomains); err != nil { + if err := rawTemplate.Execute(file, data); err != nil { log.Fatalf("Error generating config: %v", err) } fmt.Println("NGINX config generated successfully.") +} + +func GenerateNginxConfig(subdomains []Subdomain) error { + + write("nginx.conf", "nginx", DefaultNginxTemplate, "") + + os.RemoveAll("conf.d") + + err := os.MkdirAll("conf.d", os.ModePerm) + + if err != nil { + log.Fatalf("Error creating directory: %v", err) + } + + for index, subdomain := range subdomains { + + fileName := fmt.Sprintf("conf.d/%v.conf", index+1) + + write(fileName, "nginx", NginxTemplate, subdomain) + + } return nil } diff --git a/internal/config/templates.go b/internal/config/templates.go index 70d0170..1b8a0e9 100644 --- a/internal/config/templates.go +++ b/internal/config/templates.go @@ -1,48 +1,71 @@ package config -const NginxTemplate = `events { +const DefaultNginxTemplate = `worker_processes auto; +error_log /var/log/nginx/error.log warn; +pid /var/run/nginx.pid; +events { + worker_connections 1024; } http { - {{- range $subdomain := . }} + include /etc/nginx/mime.types; + default_type application/octet-stream; - {{- range $location := $subdomain.Location }} + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer" ' + '"$http_user_agent" "$http_x_forwarded_for"'; - upstream {{ $location.Alias }}_upstream { - {{- range $instances := $location.Instances }} - server {{ $instances }}:{{ $location.Port }}; - {{- end }} - } + access_log /var/log/nginx/access.log main; + + sendfile on; + keepalive_timeout 65; + + ssl_protocols TLSv1.2 TLSv1.3; + ssl_prefer_server_ciphers on; + + include /etc/nginx/conf.d/*.conf; +} +` + +const NginxTemplate = `# ------------------------------------------------------------ +# {{ .Domain }} +# ------------------------------------------------------------ + +{{- range $location := .Location }} + +upstream {{ $location.Alias }}_upstream { + {{- range $instances := $location.Instances }} + server {{ $instances }}:{{ $location.Port }}; {{- end }} +} +{{- end }} - server { - {{- if $subdomain.Ssl }} - listen 443 ssl; - ssl_certificate {{ $subdomain.SslCert }}; - ssl_certificate_key {{ $subdomain.SslCertKey }}; - {{- else }} - listen 80; - {{- end }} +server { + {{- if .Ssl }} + listen 443 ssl; + ssl_certificate {{ .SslCert }}; + ssl_certificate_key {{ .SslCertKey }}; + {{- else }} + listen 80; + {{- end }} + + server_name {{ .Domain }}; - server_name {{ $subdomain.Domain }}; - - {{- range $location := $subdomain.Location }} - - location {{ $location.Path }} { - {{- if $location.Ssl }} - proxy_pass https://{{ $location.Alias }}_upstream; - {{- else }} - proxy_pass http://{{ $location.Alias }}_upstream; - {{- end }} - proxy_set_header Host $host; - proxy_set_header X-Real-IP $remote_addr; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Proto $scheme; - proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection $http_connection; - } + {{- range $location := .Location }} + + location {{ $location.Path }} { + {{- if $location.Ssl }} + proxy_pass https://{{ $location.Alias }}_upstream; + {{- else }} + proxy_pass http://{{ $location.Alias }}_upstream; {{- end }} + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection $http_connection; } {{- end }} }