-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnginx_http.go
89 lines (68 loc) · 2.38 KB
/
nginx_http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package khatru29
import (
"fmt"
"github.com/nodetec/rwz/pkg/network"
"github.com/nodetec/rwz/pkg/utils/directories"
"github.com/nodetec/rwz/pkg/utils/files"
"github.com/nodetec/rwz/pkg/utils/systemd"
"github.com/pterm/pterm"
)
// Function to configure Nginx for HTTP
func ConfigureNginxHttp(domainName string) {
spinner, _ := pterm.DefaultSpinner.Start("Configuring Nginx for HTTP...")
files.RemoveFile(NginxConfigFilePath)
directories.CreateDirectory(fmt.Sprintf("%s/%s/%s/", network.WWWDirPath, domainName, network.AcmeChallengeDirPath), 0755)
configContent := fmt.Sprintf(`map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream websocket_khatru29 {
server 0.0.0.0:5577;
}
server {
listen 80;
listen [::]:80;
server_name %s;
location /%s/ {
root %s/%s;
allow all;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying 404.
try_files $uri $uri/ =404;
proxy_pass http://websocket_khatru29;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
# Only return Nginx in server header
server_tokens off;
#### Security Headers ####
# Test configuration:
# https://securityheaders.com/
# https://observatory.mozilla.org/
add_header X-Frame-Options DENY;
# Avoid MIME type sniffing
add_header X-Content-Type-Options nosniff always;
add_header Referrer-Policy "no-referrer" always;
add_header X-XSS-Protection 0 always;
add_header Permissions-Policy "geolocation=(), midi=(), sync-xhr=(), microphone=(), camera=(), magnetometer=(), gyroscope=(), fullscreen=(self), payment=()" always;
#### Content-Security-Policy (CSP) ####
add_header Content-Security-Policy "base-uri 'self'; object-src 'none'; frame-ancestors 'none';" always;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name %s;
location / {
return 301 http://%s$request_uri;
}
}
`, domainName, network.AcmeChallengeDirPath, network.WWWDirPath, domainName, domainName, domainName)
files.WriteFile(NginxConfigFilePath, configContent, 0644)
systemd.RestartService("nginx")
spinner.Success("Nginx configured for HTTP")
}