4.0 KiB
4.0 KiB
Environment Configuration Guide
🌍 Overview
This guide explains how to configure the Customer Portal for different environments (development, staging, production).
📁 Environment Files
File Structure
apps/portal/
├── .env.local # Local development (ignored by git)
├── .env.example # Template for local setup
└── .env.production # Production environment template
Priority Order
.env.local(highest priority, for local overrides).env.production(when NODE_ENV=production)- Fallback to
http://localhost:4000(development default)
🔧 API Base Configuration
Simple Configuration (Recommended)
# Set this to your actual API URL
NEXT_PUBLIC_API_BASE="https://api.yourdomain.com"
Common Examples:
# Same domain with /api path
NEXT_PUBLIC_API_BASE="https://yourdomain.com/api"
# Separate API subdomain
NEXT_PUBLIC_API_BASE="https://api.yourdomain.com"
# Different port
NEXT_PUBLIC_API_BASE="https://yourdomain.com:4000"
# Development (default fallback)
NEXT_PUBLIC_API_BASE="http://localhost:4000"
🚀 Deployment Scenarios
Scenario 1: Frontend + Backend on Same Server
# Frontend: https://yourdomain.com
# Backend: https://yourdomain.com/api
NEXT_PUBLIC_API_BASE="/api"
Scenario 2: Separate API Server
# Frontend: https://portal.yourdomain.com
# Backend: https://api.yourdomain.com
NEXT_PUBLIC_API_BASE="https://api.yourdomain.com"
Scenario 3: Docker/Kubernetes Deployment
# Using service discovery
NEXT_PUBLIC_API_BASE="https://backend-service/api"
🛠️ Setup Instructions
Development Setup
-
Copy the example file:
cp apps/portal/.env.example apps/portal/.env.local -
Update values as needed:
NEXT_PUBLIC_API_BASE="http://localhost:4000"
Production Setup
-
Set environment variables in your deployment platform:
- Vercel: Add to project settings
- Docker: Use environment files or compose
- Kubernetes: Use ConfigMaps/Secrets
-
Choose appropriate API base configuration
Environment Variables Reference
| Variable | Description | Example |
|---|---|---|
NEXT_PUBLIC_API_BASE |
Backend API base URL | https://api.example.com |
NEXT_PUBLIC_APP_NAME |
Application display name | Customer Portal |
NEXT_PUBLIC_APP_VERSION |
Version displayed in UI | 1.0.0 |
NODE_ENV |
Environment mode | production |
HOSTNAME |
Server bind address | 0.0.0.0 |
PORT |
Server port | 3000 |
🔍 Troubleshooting
Common Issues
-
CORS Errors in Production
- Ensure backend allows frontend domain
- Check API base URL is correct
-
404 Errors for API Calls
- Verify
NEXT_PUBLIC_API_BASEis set correctly - Check network tab in browser dev tools
- Verify
-
Auto-detection Not Working
- Set explicit
NEXT_PUBLIC_API_BASE - Check console for API base URL being used
- Set explicit
Debug API Configuration
Add this to any component to see current API base:
console.log('API Base:', process.env.NEXT_PUBLIC_API_BASE || 'auto-detected');
🔒 Security Considerations
- HTTPS in Production: Always use HTTPS for production APIs
- Environment Secrets: Use proper secret management for sensitive data
- CORS Configuration: Restrict backend CORS to known frontend domains
- API Keys: Use environment variables, never hardcode
📦 Docker Deployment Example
# Production build with environment
FROM node:18-alpine
WORKDIR /app
COPY . .
RUN pnpm build
# Set production environment
ENV NODE_ENV=production
ENV NEXT_PUBLIC_API_BASE="https://api.example.com"
EXPOSE 3000
CMD ["pnpm", "start"]
🎯 Best Practices
- Use auto-detection for simple same-domain deployments
- Set explicit URLs for complex multi-service architectures
- Test environment configurations before deployment
- Document your specific deployment setup
- Use staging environments to verify configuration