Nginx Configuration
Relevant source files
The following files were used as context for generating this wiki page:
While the repository does not contain a specific nginx.conf file, this document outlines a recommended Nginx configuration designed to act as a reverse proxy for the Analisis-Sentiment-Data_Mining project. This configuration is derived from the project's architecture, deployment documentation, and application code.
The primary role of Nginx in this context is to receive all incoming HTTP/HTTPS traffic and forward it to the appropriate backend service, which is the Next.js application. It also handles SSL termination, caching, compression, and security headers, providing a robust and performant production environment.
Sources: dashboard-next/DEPLOYMENT_CHECKLIST.md, README.md, dashboard-next/app/api/live-comments/route.ts
System Architecture and Request Flow
The project consists of two main services: a Next.js frontend application running on port 3000 and a Python (FastAPI/Gemini) backend API running on port 8000. The Next.js application contains its own API routes (e.g., /api/live-comments) which, in turn, make server-to-server requests to the Python backend.
Nginx sits in front of the Next.js application, proxying all client requests to it. The Next.js server then handles the logic of communicating with the Python backend.
Sources: README.md:39, README.md:43, dashboard-next/app/api/live-comments/route.ts:4
Request Flow Diagram
The following diagram illustrates how user requests are processed through Nginx to the application services.
This diagram shows that Nginx forwards all incoming traffic to the Next.js application on port 3000. The Next.js application's API routes then communicate with the Python backend on port 8000.
Sources: README.md:39-43, dashboard-next/app/api/live-comments/route.ts:4, dashboard-next/app/api/gemini-sentiment/route.ts:3
Recommended Nginx Configuration
Below is a sample nginx.conf file tailored for this project. It includes server blocks for handling HTTP to HTTPS redirection, proxying to the Next.js application, and implementing performance and security best practices.
# Recommended nginx.conf for Analisis-Sentiment-Data_Mining
# Upstream for the Next.js application
upstream nextjs_app {
server 127.0.0.1:3000;
}
# Redirect HTTP to HTTPS
server {
listen 80;
server_name your-domain.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name your-domain.com;
# SSL Configuration
ssl_certificate /path/to/your/fullchain.pem;
ssl_certificate_key /path/to/your/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384';
ssl_prefer_server_ciphers off;
# Performance: Compression
gzip on;
gzip_proxied any;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
gzip_comp_level 6;
gzip_min_length 1000;
gzip_vary on;
# Security Headers (as per DEPLOYMENT_CHECKLIST.md)
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self'; font-src 'self'; object-src 'none'; frame-ancestors 'self';" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
# Proxy all requests to the Next.js application
location / {
proxy_pass http://nextjs_app;
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_cache_bypass $http_upgrade;
}
# Caching for static assets generated by Next.js
location /_next/static {
proxy_cache_valid 200 30d;
proxy_pass http://nextjs_app;
add_header Cache-Control "public, max-age=31536000, immutable";
}
}
Sources: dashboard-next/DEPLOYMENT_CHECKLIST.md:29, dashboard-next/DEPLOYMENT_CHECKLIST.md:92, next.config.js:5, PERFORMANCE_OPTIMIZATION_REPORT.md:20
Key Configuration Directives
| Location / Directive | Purpose | Justification |
|---|---|---|
upstream nextjs_app | Defines the Next.js application server address. | The Next.js app runs on port 3000. Sources: README.md:41, dashboard-next/DEPLOYMENT_CHECKLIST.md:41 |
listen 80 | Listens for HTTP traffic. | Standard practice to redirect all insecure traffic to HTTPS. |
return 301 https... | Redirects HTTP to HTTPS. | Enforces secure connections, a key item in the security checklist. Sources: dashboard-next/DEPLOYMENT_CHECKLIST.md:92 |
listen 443 ssl | Listens for HTTPS traffic and enables SSL. | Required for secure communication. |
gzip on; | Enables on-the-fly compression. | Complements the compress: true setting in next.config.js for better performance. Sources: next.config.js:5, PERFORMANCE_OPTIMIZATION_REPORT.md:58 |
add_header ... | Sets various security headers. | Fulfills the security checklist requirements for hardening the application. Sources: dashboard-next/DEPLOYMENT_CHECKLIST.md:93 |
location / | Matches all incoming requests. | The primary block to proxy traffic to the Next.js application. |
proxy_pass ... | Forwards the request to the nextjs_app upstream. | This is the core function of the reverse proxy. |
location /_next/static | Caches static assets. | Next.js serves its static assets from this path. Aggressive caching improves performance for repeat visits. |
Performance and Caching
Nginx plays a crucial role in the application's performance strategy, complementing the optimizations within the Next.js application.
Compression
The next.config.js file enables compression (compress: true). While Next.js can handle this, offloading compression to Nginx is often more efficient, especially under high load. The provided Nginx configuration enables gzip for common text-based assets.
Sources: next.config.js:5, PERFORMANCE_OPTIMIZATION_REPORT.md:58
Caching
The application uses multiple layers of caching.
- Application-level Caching: The API route
/api/statsimplements an in-memory cache and sets aCache-Controlheader. - Nginx Caching: Nginx can be configured to respect these headers. The sample configuration adds an aggressive cache policy for immutable static assets served from
/_next/static, instructing browsers to cache them for a long duration.
The Cache-Control header set by the /api/stats route (public, s-maxage=300, stale-while-revalidate=600) will be passed through by Nginx to the client, allowing browsers and intermediate caches to store the response for 5 minutes.
Sources: PERFORMANCE_OPTIMIZATION_REPORT.md:19, dashboard-next/app/api/stats/route.ts:10, dashboard-next/app/api/stats/route.ts:133
Security
Nginx is the first line of defense and is configured to enhance application security as recommended in the deployment checklist.
SSL/TLS Termination
Nginx terminates the SSL/TLS connection, decrypting incoming requests and forwarding them to the backend Next.js application over plain HTTP. This centralizes SSL certificate management.
Security Headers
The configuration adds several important HTTP security headers to mitigate common web vulnerabilities like clickjacking and cross-site scripting (XSS). This aligns with the security checklist.