-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnginx.conf
More file actions
405 lines (348 loc) · 13.1 KB
/
nginx.conf
File metadata and controls
405 lines (348 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# Copyright (c) 2026 Mike Odnis
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Nginx Reverse Proxy Configuration for Next.js 16 Portfolio
# Place this in /etc/nginx/sites-available/portfolio
# Create symlink: ln -s /etc/nginx/sites-available/portfolio /etc/nginx/sites-enabled/
# Test config: nginx -t
# Reload: systemctl reload nginx
# Rate limiting zones (must be in http context, before server blocks)
# Add these to /etc/nginx/nginx.conf http block:
# limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;
# limit_req_zone $binary_remote_addr zone=general:10m rate=30r/s;
# limit_conn_zone $binary_remote_addr zone=conn_limit:10m;
upstream portfolio_backend {
# Docker container or local Next.js server
server localhost:3000;
# For multiple instances (load balancing)
# server localhost:3001;
# server localhost:3002;
# Keep connections alive for better performance
keepalive 64;
keepalive_requests 1000;
keepalive_timeout 60s;
}
# Redirect HTTP to HTTPS
server {
listen 80;
listen [::]:80;
server_name mikeodnis.dev www.mikeodnis.dev;
# ACME challenge for Let's Encrypt
location /.well-known/acme-challenge/ {
root /var/www/certbot;
allow all;
}
# Redirect all other traffic to HTTPS
location / {
return 301 https://$host$request_uri;
}
}
# HTTPS server
server {
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
server_name mikeodnis.dev www.mikeodnis.dev;
# SSL certificates (Let's Encrypt)
ssl_certificate /etc/letsencrypt/live/mikeodnis.dev/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mikeodnis.dev/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/mikeodnis.dev/chain.pem;
# SSL configuration (Mozilla Modern - TLS 1.3 preferred)
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';
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_session_tickets off;
ssl_buffer_size 4k;
# OCSP stapling
ssl_stapling on;
ssl_stapling_verify on;
resolver 1.1.1.1 1.0.0.1 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
# Security headers
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=(), interest-cohort=()" always;
add_header Cross-Origin-Opener-Policy "same-origin" always;
add_header Cross-Origin-Embedder-Policy "credentialless" always;
# Remove server version
server_tokens off;
# Logging
access_log /var/log/nginx/portfolio_access.log combined buffer=512k flush=1m;
error_log /var/log/nginx/portfolio_error.log warn;
# Client settings
client_max_body_size 10M;
client_body_buffer_size 128k;
client_header_buffer_size 1k;
large_client_header_buffers 4 16k;
# Connection limits
# limit_conn conn_limit 20;
# Gzip compression
gzip on;
gzip_vary on;
gzip_min_length 1000;
gzip_proxied any;
gzip_types
text/plain
text/css
text/js
text/xml
text/javascript
application/javascript
application/json
application/xml
application/rss+xml
application/atom+xml
image/svg+xml
font/woff
font/woff2;
gzip_comp_level 6;
gzip_buffers 16 8k;
# Brotli compression (if ngx_brotli module installed)
# brotli on;
# brotli_comp_level 6;
# brotli_types text/plain text/css text/xml text/javascript application/json application/javascript application/xml application/rss+xml image/svg+xml font/woff font/woff2;
# Proxy cache (optional - Next.js handles its own caching)
# proxy_cache_path /var/cache/nginx/portfolio levels=1:2 keys_zone=portfolio_cache:10m max_size=1g inactive=60m use_temp_path=off;
# Static files - images with long cache
location ~* \.(jpg|jpeg|png|gif|ico|svg|webp|avif|heic)$ {
proxy_pass http://portfolio_backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
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;
expires 1y;
add_header Cache-Control "public, immutable, max-age=31536000";
add_header X-Content-Type-Options "nosniff" always;
access_log off;
}
# Static files - fonts and CSS/JS with long cache
location ~* \.(css|js|woff|woff2|ttf|eot|otf)$ {
proxy_pass http://portfolio_backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
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;
expires 1y;
add_header Cache-Control "public, immutable, max-age=31536000";
add_header X-Content-Type-Options "nosniff" always;
access_log off;
}
# API endpoints with rate limiting
location /api/ {
# Rate limiting
limit_req zone=api burst=20 nodelay;
limit_req_status 429;
proxy_pass http://portfolio_backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
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;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
proxy_cache_bypass $http_upgrade;
# Disable buffering for streaming responses
proxy_buffering off;
proxy_request_buffering off;
# Timeouts
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
# Error handling
proxy_intercept_errors off;
}
# Health check endpoint (no rate limiting)
location = /api/health {
proxy_pass http://portfolio_backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
access_log off;
# Quick timeout for health checks
proxy_connect_timeout 5s;
proxy_read_timeout 5s;
}
# Next.js static assets (immutable)
location /_next/static/ {
proxy_pass http://portfolio_backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
expires 1y;
add_header Cache-Control "public, immutable, max-age=31536000";
access_log off;
}
# Next.js data routes
location /_next/data/ {
proxy_pass http://portfolio_backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
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;
# Short cache for data
add_header Cache-Control "public, max-age=60, stale-while-revalidate=300";
}
# Next.js image optimization
location /_next/image {
proxy_pass http://portfolio_backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
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;
# Image optimization can take time
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
# Don't buffer images
proxy_buffering off;
proxy_request_buffering off;
# Cache optimized images
add_header Cache-Control "public, max-age=86400, stale-while-revalidate=604800";
}
# PWA Service Worker (no cache)
location = /sw.js {
proxy_pass http://portfolio_backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
add_header Cache-Control "no-cache, no-store, must-revalidate";
add_header Service-Worker-Allowed "/";
expires -1;
}
# PWA Workbox files
location ~* ^/workbox-.+\.js$ {
proxy_pass http://portfolio_backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
add_header Cache-Control "no-cache, no-store, must-revalidate";
expires -1;
}
# PWA manifest
location = /manifest.webmanifest {
proxy_pass http://portfolio_backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
add_header Cache-Control "public, max-age=3600";
default_type application/manifest+json;
}
# Robots and sitemap
location = /robots.txt {
proxy_pass http://portfolio_backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
add_header Cache-Control "public, max-age=86400";
access_log off;
}
location = /sitemap.xml {
proxy_pass http://portfolio_backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
add_header Cache-Control "public, max-age=3600";
}
# RSS feed
location ~* ^/feed\.xml$ {
proxy_pass http://portfolio_backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
add_header Cache-Control "public, max-age=3600";
}
# Block common vulnerability scans
location ~* (\.php|\.asp|\.aspx|\.jsp|\.cgi|\.env|\.git|\.svn|wp-admin|wp-login|xmlrpc) {
return 444;
}
# Main application
location / {
# General rate limiting
limit_req zone=general burst=50 nodelay;
proxy_pass http://portfolio_backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
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;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
proxy_cache_bypass $http_upgrade;
# Timeouts
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
# Buffer settings for HTML
proxy_buffering on;
proxy_buffer_size 8k;
proxy_buffers 16 8k;
proxy_busy_buffers_size 16k;
# Error pages
error_page 502 503 504 /50x.html;
}
# Custom error page
location = /50x.html {
root /usr/share/nginx/html;
internal;
}
}
# Redirect www to non-www (optional - uncomment if needed)
# server {
# listen 443 ssl;
# listen [::]:443 ssl;
# http2 on;
# server_name www.mikeodnis.dev;
#
# ssl_certificate /etc/letsencrypt/live/mikeodnis.dev/fullchain.pem;
# ssl_certificate_key /etc/letsencrypt/live/mikeodnis.dev/privkey.pem;
#
# return 301 https://mikeodnis.dev$request_uri;
# }
# Jaeger UI (development/staging only - protect with auth in production)
# server {
# listen 80;
# server_name jaeger.mikeodnis.dev;
#
# # Basic auth for protection
# # auth_basic "Restricted Access";
# # auth_basic_user_file /etc/nginx/.htpasswd;
#
# location / {
# proxy_pass http://localhost:16686;
# proxy_http_version 1.1;
# proxy_set_header Upgrade $http_upgrade;
# proxy_set_header Connection 'upgrade';
# proxy_set_header Host $host;
# proxy_cache_bypass $http_upgrade;
# }
# }