Skip to content

Commit ce80449

Browse files
authored
Merge pull request #7 from nodetec/harden-nginx-https
Harden nginx https
2 parents fc1f424 + f81f508 commit ce80449

File tree

1 file changed

+61
-14
lines changed

1 file changed

+61
-14
lines changed

pkg/network/nginx_https.go

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,17 @@ upstream websocket {
3030
}
3131
3232
server {
33-
listen 443 ssl;
34-
listen [::]:443 ssl;
33+
listen 443 ssl http2;
34+
listen [::]:443 ssl http2;
3535
server_name %s;
3636
37+
root /var/www/%s;
38+
3739
location / {
38-
proxy_pass http://websocket;
40+
# First attempt to serve request as file, then
41+
# as directory, then fall back to displaying 404.
42+
try_files $uri $uri/ =404;
43+
proxy_pass http://websocket;
3944
proxy_http_version 1.1;
4045
proxy_set_header Upgrade $http_upgrade;
4146
proxy_set_header Connection $connection_upgrade;
@@ -44,24 +49,66 @@ server {
4449
}
4550
4651
#### SSL Configuration ####
52+
# Test configuration:
53+
# https://www.ssllabs.com/ssltest/analyze.html
54+
# https://cryptcheck.fr/
4755
ssl_certificate /etc/letsencrypt/live/%s/fullchain.pem;
4856
ssl_certificate_key /etc/letsencrypt/live/%s/privkey.pem;
57+
# Verify chain of trust of OCSP response using Root CA and Intermediate certs
58+
ssl_trusted_certificate /etc/letsencrypt/live/%s/chain.pem;
4959
50-
ssl_session_cache shared:SSL:10m;
51-
ssl_session_timeout 10m;
52-
ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
60+
# Only return Nginx in server header
61+
server_tokens off;
62+
63+
# TODO
64+
# Add support to generate the file in the script
65+
#ssl_dhparam /etc/ssl/certs/dhparam.pem;
66+
67+
ssl_protocols TLSv1.2 TLSv1.3;
68+
69+
# For more information on the security of different cipher suites, you can refer to the following link:
70+
# https://ciphersuite.info/
71+
# Compilation of the top cipher suites 2024:
72+
# https://ssl-config.mozilla.org/#server=nginx
73+
ssl_ciphers "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-CHACHA20-POLY1305";
74+
75+
# Perfect Forward Secrecy (PFS) is frequently compromised without this
5376
ssl_prefer_server_ciphers on;
54-
ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA RC4 !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS";
77+
78+
ssl_session_tickets off;
79+
80+
# Enable SSL session caching for improved performance
81+
# Try setting ssl_session_timeout to 1d if performance is bad
82+
ssl_session_timeout 10m;
83+
ssl_session_cache shared:SSL:10m;
84+
85+
# By default, the buffer size is 16k, which corresponds to minimal overhead when sending big responses.
86+
# To minimize Time To First Byte it may be beneficial to use smaller values
87+
ssl_buffer_size 8k;
88+
89+
# OCSP stapling
5590
ssl_stapling on;
5691
ssl_stapling_verify on;
57-
ssl_ecdh_curve secp384r1;
5892
59-
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains";
93+
# Security headers
94+
# Test configuration:
95+
# https://securityheaders.com/
96+
# https://observatory.mozilla.org/
97+
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; preload";
98+
6099
add_header X-Frame-Options DENY;
61-
add_header X-Content-Type-Options nosniff;
62-
add_header X-XSS-Protection "1; mode=block";
63-
add_header Referrer-Policy same-origin;
64-
add_header Feature-Policy "geolocation none;midi none;notifications none;push none;sync-xhr none;microphone none;camera none;magnetometer none;gyroscope none;speaker self;vibrate none;fullscreen self;payment none;";
100+
101+
# Avoid MIME type sniffing
102+
add_header X-Content-Type-Options nosniff always;
103+
104+
add_header Referrer-Policy "no-referrer" always;
105+
106+
add_header X-XSS-Protection 0 always;
107+
108+
add_header Permissions-Policy "geolocation=(), midi=(), sync-xhr=(), microphone=(), camera=(), magnetometer=(), gyroscope=(), fullscreen=(self), payment=()" always;
109+
110+
# Content-Security-Policy (CSP)
111+
add_header Content-Security-Policy "base-uri 'self'; object-src 'none'; frame-ancestors 'none'; upgrade-insecure-requests;" always;
65112
}
66113
67114
server {
@@ -78,7 +125,7 @@ server {
78125
return 301 https://%s$request_uri;
79126
}
80127
}
81-
`, domainName, domainName, domainName, domainName, dirName, domainName)
128+
`, domainName, dirName, domainName, domainName, domainName, domainName, dirName, domainName)
82129

83130
err = os.WriteFile("/etc/nginx/conf.d/nostr_relay.conf", []byte(configContent), 0644)
84131
if err != nil {

0 commit comments

Comments
 (0)