| 1 | { |
| 2 | config, |
| 3 | lib, |
| 4 | pkgs, |
| 5 | ... |
| 6 | }: |
| 7 | |
| 8 | let |
| 9 | bannedUserAgentPatterns = [ |
| 10 | "Trident/" |
| 11 | "Android\\s[123456789]\\." |
| 12 | "iPod" |
| 13 | "iPad\\sOS\\s" |
| 14 | "iPhone\\sOS\\s[23456789]" |
| 15 | "Opera/[89]" |
| 16 | "(Chrome|CriOS)/(\\d\\d?\\.|1[01]|12[4])" |
| 17 | "(Firefox|FxiOS)/(\\d\\d?\\.|1[01]|12[012345679]\\.)" |
| 18 | "PPC\\sMac\\sOS" |
| 19 | "Windows\\sCE" |
| 20 | "Windows\\s95" |
| 21 | "Windows\\s98" |
| 22 | "Windows\\sNT\\s[12345]\\." |
| 23 | ]; |
| 24 | in |
| 25 | { |
| 26 | networking.firewall.allowedTCPPorts = [ |
| 27 | 80 |
| 28 | 443 |
| 29 | ]; |
| 30 | |
| 31 | services.nginx = { |
| 32 | enable = true; |
| 33 | enableReload = true; |
| 34 | |
| 35 | recommendedBrotliSettings = true; |
| 36 | recommendedGzipSettings = true; |
| 37 | recommendedOptimisation = true; |
| 38 | recommendedProxySettings = true; |
| 39 | recommendedTlsSettings = true; |
| 40 | |
| 41 | proxyTimeout = "900s"; |
| 42 | |
| 43 | appendConfig = '' |
| 44 | worker_processes auto; |
| 45 | ''; |
| 46 | |
| 47 | eventsConfig = '' |
| 48 | worker_connections 1024; |
| 49 | ''; |
| 50 | |
| 51 | appendHttpConfig = '' |
| 52 | map $http_user_agent $badagent { |
| 53 | default 0; |
| 54 | ${lib.concatMapStringsSep "\n" (pattern: '' |
| 55 | ~${pattern} 1; |
| 56 | '') bannedUserAgentPatterns} |
| 57 | } |
| 58 | ''; |
| 59 | |
| 60 | # Plain HTTP access via lysator hostname (no ACME since we don't control the domain) |
| 61 | virtualHosts."nixos.lysator.liu.se" = { |
| 62 | locations."/" = { |
| 63 | proxyPass = "http://127.0.0.1:3000"; |
| 64 | }; |
| 65 | }; |
| 66 | |
| 67 | virtualHosts."staging-hydra.nixos.org" = { |
| 68 | forceSSL = true; |
| 69 | enableACME = true; |
| 70 | |
| 71 | extraConfig = '' |
| 72 | error_page 502 /502.html; |
| 73 | error_page 503 /503.html; |
| 74 | location ~ /(502|503).html { |
| 75 | root ${../../../build/nginx-error-pages}; |
| 76 | internal; |
| 77 | } |
| 78 | ''; |
| 79 | |
| 80 | # Ask robots not to scrape hydra, it has various expensive endpoints |
| 81 | locations."=/robots.txt".alias = pkgs.writeText "hydra.nixos.org-robots.txt" '' |
| 82 | User-agent: * |
| 83 | Disallow: / |
| 84 | Allow: /$ |
| 85 | ''; |
| 86 | |
| 87 | locations."/" = { |
| 88 | proxyPass = "http://127.0.0.1:3000"; |
| 89 | extraConfig = '' |
| 90 | if ($badagent) { |
| 91 | access_log /var/log/nginx/abuse.log; |
| 92 | return 403; |
| 93 | } |
| 94 | ''; |
| 95 | }; |
| 96 | |
| 97 | locations."/static/" = { |
| 98 | alias = "${config.services.hydra-dev.package}/libexec/hydra/root/static/"; |
| 99 | }; |
| 100 | }; |
| 101 | }; |
| 102 | |
| 103 | } |