Nginx and Linux Hardening for a Business Web App: A Working Checklist
Most compromised business sites I look at were not breached through a clever exploit. They were breached through a default config, an exposed admin path, or a service running as root because it was easier. Here is the p
Most compromised business sites I look at were not breached through a clever exploit. They were breached through a default config, an exposed admin path, or a service running as root because it was easier.
Here is the pass I run on a fresh box before it takes traffic.
1. SSH first, because everything else depends on it
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
MaxAuthTries 3
AllowUsers deploy
Then systemctl restart ssh. Keep an existing session open while you test the new one — the most common self-inflicted outage in this checklist is locking yourself out.
Install fail2ban and point it at sshd. A few lines of jail config turns brute force from a nightly nuisance into a non-event.
2. TLS that scores well without breaking clients
Modern config, TLS 1.2 and 1.3 only, HSTS once you are sure about all subdomains:
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_stapling on;
ssl_stapling_verify on;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
Renewal must be automated and monitored. A certificate that expires at 3am on a Saturday is an outage with a deadline you set yourself.
3. Rate limits on the endpoints that matter
Do not rate limit everything; you will break legitimate bursts. Limit login, password reset, and any endpoint that sends email or SMS.
limit_req_zone $binary_remote_addr zone=login:10m rate=5r/m;
location = /api/auth/login {
limit_req zone=login burst=5 nodelay;
proxy_pass http://app;
}
Pair it with a lockout counter in the application. Nginx protects the process; the app protects the account.
4. Stop scanning noise before it reaches your app
location ~ /\.(env|git|svn|hg) { deny all; return 404; }
location ~* \.(sql|bak|log|old|swp)$ { deny all; return 404; }
if ($request_uri ~* "(wp-admin|phpmyadmin|\.php$)") { return 404; }
If you do not run PHP, every request for a .php path is an attacker or a scanner. Answer 404 and move on.
5. Run the app as a user that owns nothing else
useradd --system --no-create-home --shell /usr/sbin/nologin appsvc
chown -R appsvc:appsvc /srv/app
chmod -R u=rwX,g=rX,o= /srv/app
The X matters: directories get execute, files do not. Secrets belong in an environment file readable only by that user, not in the repo and not in the nginx config.
6. Backups you have actually restored
An untested backup is a rumour. Restore into a scratch database monthly and check row counts on the tables that matter. Encrypt the dump, keep it off the same host, and make sure the retention window matches the business requirement rather than the disk size.
7. Logs that survive the incident
Ship access and application logs off the box. If the attacker gets root, local logs are theirs to edit. A 7-day retention on the host plus 90 days off-host has caught more real problems for me than any SIEM.
8. The five-minute check afterwards
-
ss -tulpn— is anything listening that you did not expect? -
lastb | head— is anyone brute forcing? -
systemctl list-units --state=failed— anything broken and ignored? - certificate expiry date
- a restore from last night's backup
None of this is exotic. It is the difference between a server that survives being found and one that becomes a case study.
The full checklist with the config files in one place: https://keston.dev/guides/server-security-hardening-for-business-web
I do this hardening pass as part of every build at KESTON — https://keston.dev — alongside the application work, because shipping a feature on an unhardened box is not shipping.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.