Skip to content

Commit 4fe0175

Browse files
committed
feat(nix): rate limit with nginx in nixos module
1 parent 96d6e81 commit 4fe0175

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

flake.nix

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
];
1515

1616
CC = "clang";
17-
QEMU_NET_OPTS = "hostfwd=tcp:127.0.0.1:8080-:8080";
17+
QEMU_NET_OPTS = "hostfwd=tcp::8080-:80";
1818
};
1919

2020
packages.x86_64-linux.display =
@@ -31,17 +31,34 @@
3131

3232
nixosModules.everything =
3333
{ config, ... }: {
34-
networking.firewall.allowedTCPPorts = [ 8080 ];
34+
networking.firewall.allowedTCPPorts = [ 80 ];
3535
systemd.services.ttds-runner = {
36-
wantedBy = [ "multi-user.target" ];
37-
after = [ "network.target" ];
38-
description = "Run the ttds web server, wrapping the display server.";
39-
36+
wantedBy = [ "multi-user.target" ];
37+
after = [ "network.target" ];
38+
description = "Run the ttds web server, wrapping the display server.";
4039
serviceConfig = {
41-
ExecStart = "${self.packages.x86_64-linux.web}/bin/ttds-web ${self.packages.x86_64-linux.display}/bin/ttds";
42-
WorkingDirectory = "/etc";
43-
};
44-
};
40+
ExecStart = "${self.packages.x86_64-linux.web}/bin/ttds-web ${self.packages.x86_64-linux.display}/bin/ttds";
41+
WorkingDirectory = "/etc";
42+
};
43+
};
44+
45+
services.nginx.enable = true;
46+
services.nginx.config = ''
47+
events {}
48+
49+
http {
50+
limit_req_zone $binary_remote_addr zone=api:10m rate=20r/s;
51+
52+
server {
53+
listen 80;
54+
55+
location / {
56+
limit_req zone=api burst=50 nodelay;
57+
proxy_pass http://localhost:8080/;
58+
}
59+
}
60+
}
61+
'';
4562
};
4663

4764
nixosConfigurations.test-vm = nixpkgs.lib.nixosSystem {

0 commit comments

Comments
 (0)