Nginx Configuration Review and Improvements
You've set up two Nginx server blocks:
- BLOCK 1: Redirects HTTP requests on port 8181 to HTTPS on the same port.
- BLOCK 2: Serves secure content over HTTPS on port 8181, acting as a reverse proxy to
http://localhost:8181.
Your configuration is mostly correct, but there are a few issues and potential improvements. Let's break them down:
Issues in BLOCK 1:
- You're specifying SSL certificates (
ssl_certificate and ssl_certificate_key) in the HTTP-only server block (port 8181 without ssl). This is incorrect because SSL/TLS isn't enabled for this block. Nginx will throw a warning or error because ssl_certificate directives are ignored when not in SSL mode.
- The
return 301 https://$host:8181$request_uri; line is correct for redirecting to HTTPS, but since SSL isn't enabled here, the configuration is inconsistent.
Corrected BLOCK 1 (HTTP to HTTPS Redirect):
# ############################################################# #
# ## BLOCK 1: REDIRECT HTTP on 8181 to HTTPS on 8181 ## #
# ############################################################# #
server {
listen 8181;
server_name audit.office.intelserv.com;
# Simple HTTP to HTTPS redirect (no SSL config needed here)
return 301 https://$host:8181$request_uri;
}
Improvements and Notes for BLOCK 2:
- Your SSL configuration is fine, but it's good practice to harden your SSL settings for better security. Uncomment or add the following to enforce modern TLS protocols and strong ciphers:
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:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off; # Requires TLSv1.3 for 0-RTT compatibility
- OCSP Stapling can improve SSL handshake performance and security:
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s; # Google DNS for resolving OCSP responders
resolver_timeout 5s;
- Your
proxy_pass settings look good for a reverse proxy. If your backend (http://localhost:8181) handles WebSocket connections, you're correctly supporting it with:
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
- Consider adding timeout settings to avoid hanging connections:
proxy_read_timeout 300s;
proxy_send_timeout 300s;
proxy_connect_timeout 75s;
- Optionally, add security headers to protect against common web vulnerabilities:
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
Updated BLOCK 2 (Full Secure Configuration):
# ############################################################# #
# ## BLOCK 2: SERVE SECURE CONTENT on 8181 ## #
# ############################################################# #
server {
listen 8181 ssl;
server_name audit.office.intelserv.com;
ssl_certificate C:/nginx/Cert/audit.pem;
ssl_certificate_key C:/nginx/Cert/audit.key;
ssl_session_timeout 5m;
# SSL Hardening
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:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off;
# OCSP Stapling (optional but recommended)
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
location / {
proxy_pass http://localhost:8181;
proxy_set_header Host $host;
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;
# WebSocket support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# Proxy timeouts
proxy_read_timeout 300s;
proxy_send_timeout 300s;
proxy_connect_timeout 75s;
}
# Security headers
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
}
Final Notes:
-
Test your Nginx config after changes:
nginx -t
If everything is OK, reload Nginx:
nginx -s reload
-
Verify HTTPS redirection:
- Go to
http://audit.office.intelserv.com:8181 in your browser. It should redirect to https://audit.office.intelserv.com:8181.
- Check for the SSL padlock icon in the browser bar (ensure no certificate warnings).
-
Test SSL strength:
-
Monitor logs (C:/nginx/logs/error.log and access.log) for any issues.
Your setup should now be:
- Secure: Enforces HTTPS with modern TLS settings.
- Functional: Properly proxies to
http://localhost:8181.
- Hardened: Adds security headers and OCSP stapling.
Happy Nginx-ing! Let me know if you hit any snags.