Skip to content

Commit

Permalink
nginx: add nginx-base role like module
Browse files Browse the repository at this point in the history
  • Loading branch information
mkg20001 committed Mar 24, 2024
1 parent f28c16d commit fdbd4e9
Show file tree
Hide file tree
Showing 18 changed files with 147 additions and 21 deletions.
2 changes: 1 addition & 1 deletion modules/defaults/base/sshd.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ with lib;
HostbasedAuthentication = false;
PermitEmptyPasswords = false;
UseDns = false;
UsePAM = mkDefault true;
UsePAM = mkDefault false;
};

# https://gitlab.com/gitlab-org/gitlab-foss/-/blob/master/doc/user/gitlab_com/index.md#ssh-host-keys-fingerprints
Expand Down
2 changes: 1 addition & 1 deletion modules/defaults/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
imports = [
./base
./misc.nix
./nginx.nix
./nginx
./zsh
];
}
19 changes: 0 additions & 19 deletions modules/defaults/nginx.nix

This file was deleted.

6 changes: 6 additions & 0 deletions modules/defaults/nginx/conf.d/connection-upgrade.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# used for websockets
# set http_connection to either upgrade or close (as normal)
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
1 change: 1 addition & 0 deletions modules/defaults/nginx/conf.d/hash-bucket-size.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
server_names_hash_bucket_size 64;
17 changes: 17 additions & 0 deletions modules/defaults/nginx/conf.d/mgit-json-logging.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
log_format logger-mgit-json escape=json '{'
'"http_host": "$http_host", '
'"remote_addr": "$remote_addr", '
'"time_msec": $msec, '
'"time_local": "$time_local", '
'"status": $status, '
'"body_bytes_sent": $body_bytes_sent, '
'"http_referrer": "$http_referrer", '
'"request_time": $request_time, '
'"upstream_response_time": "$upstream_response_time", '
'"request_length": $request_length, '
'"request_method": "$request_method", '
'"request_uri": "$request_uri", '
'"upstream_addr": "$upstream_addr", '
'"http_user_agent": "$http_user_agent", '
'"request": "$request"'
'}';
1 change: 1 addition & 0 deletions modules/defaults/nginx/conf.d/server-tokens.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
server_tokens off;
39 changes: 39 additions & 0 deletions modules/defaults/nginx/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{ options, config, lib, ... }@args:
import ../_with_unify.nix args config.services.nginx.enable
{
services.nginx = {
enableReload = true;
recommendedBrotliSettings = true;
recommendedGzipSettings = true;
recommendedOptimisation = true;
recommendedProxySettings = true;
recommendedTlsSettings = true;
recommendedZstdSettings = true;

# Default block returns null for SSL requests with the wrong hostname
# This is to prevent SNI info leak. This configuration only works for nginx 1.19.4 and later.
virtualHosts."default" = {
default = true;
listen = [
{ port = 443; ssl = true; addr = "[::]"; }
{ port = 80; addr = "[::]"; }
];
extraConfig = ''
ssl_reject_handshake on;
'';
locations."/".extraConfig = ''
return 404 "This domain is not configured on this server. Please contact your administrator if this seems wrong.";
'';
};

};

environment.etc."nginx/conf.d".source = ./conf.d;
environment.etc."nginx/snippets".source = ./snippets;
}
{
nix-unify = {
modules.shareSystemd.units = [ "nginx.service" ];
files.etc."nginx" = {};
};
}
5 changes: 5 additions & 0 deletions modules/defaults/nginx/snippets/acmetool.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# snippet from ansible by mgit
location /.well-known/acme-challenge/ {
auth_basic off;
alias /run/acme/acme-challenge/;
}
3 changes: 3 additions & 0 deletions modules/defaults/nginx/snippets/cf-origin-pull.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# cf authenticated only
ssl_client_certificate /etc/ssl/cloudflare/origin_pull.pem;
ssl_verify_client on;
1 change: 1 addition & 0 deletions modules/defaults/nginx/snippets/hsts.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_header Strict-Transport-Security max-age=15768000 always;
5 changes: 5 additions & 0 deletions modules/defaults/nginx/snippets/proxy-forward-headers.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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 X-Forwarded-Ssl on;
proxy_set_header X-Forwarded-Port $server_port;
4 changes: 4 additions & 0 deletions modules/defaults/nginx/snippets/proxy-nobuff.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
proxy_buffering off;
proxy_ignore_headers "X-Accel-Buffering";
proxy_request_buffering off;
proxy_http_version 1.1;
6 changes: 6 additions & 0 deletions modules/defaults/nginx/snippets/security-headers.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
# TODO: Test Secruity headers
# This still needs to be tested to see if it breaks anything
# add_header Content-Security-Policy "default-src 'none'; connect-src 'self'; img-src 'self'; script-src 'self'; style-src 'self'";
9 changes: 9 additions & 0 deletions modules/defaults/nginx/snippets/tls-legacy.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:RSA+AES:!ADH:!AECDH:!MD5;
ssl_prefer_server_ciphers on;

ssl_dhparam /etc/ssl/dhparams.pem;

ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_session_tickets off;
14 changes: 14 additions & 0 deletions modules/defaults/nginx/snippets/tls.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# ssl security config for tls1.3 by mgit
# 20240219 - intermediate security nginx config https://mozilla.github.io/server-side-tls/ssl-config-generator/
# generated 2024-02-19, Mozilla Guideline v5.7, nginx 1.14.2, OpenSSL 1.1.1n, intermediate configuration
# https://ssl-config.mozilla.org/#server=nginx&version=1.14.2&config=intermediate&openssl=1.1.1n&guideline=5.7
# This version is based on the nginx version and openssl version of Debian 10 Buster, because this the oldest OS
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off;

ssl_dhparam /etc/ssl/dhparams.pem;

ssl_protocols TLSv1.2 TLSv1.3;
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;
ssl_prefer_server_ciphers off;
11 changes: 11 additions & 0 deletions modules/defaults/nginx/snippets/tls1_2.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# ssl security config for tls1.2 by mgit
# 20200710 - intermediate security nginx config https://mozilla.github.io/server-side-tls/ssl-config-generator/
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets off;

ssl_dhparam /etc/ssl/dhparams.pem;

ssl_protocols TLSv1.2;
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;
ssl_prefer_server_ciphers on;
23 changes: 23 additions & 0 deletions tests/nginx.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
inputs: mod: { pkgs, lib, ... }:
{
name = "nginx";

node.specialArgs.inputs = inputs;

nodes = {
server = { lib, pkgs, ... }: {
imports = mod.default;
services.nginx.enable = true;

environment.systemPackages = with pkgs; [
curl
];
};
};

testScript = ''
start_all()
server.wait_for_unit("nginx")
server.execute("curl localhost | grep 'This domain is not configured on this server. Please contact your administrator if this seems wrong.'")
'';
}

0 comments on commit fdbd4e9

Please sign in to comment.