This project demonstrates how to build a multi-environment React application served by NGINX. It includes a React frontend that fetches environment information from an NGINX server, which dynamically provides the environment based on system variables. This setup allows you to deploy the same React application in different environments (e.g., development, staging, production) with configuration handled by NGINX.
- Node.js and npm installed on your local machine
- Docker installed on your local machine
-
Clone this repository to your local machine:
git clone https://github.com/AAber/MultiEnvNginxReact.git
-
Navigate to the project directory:
cd MultiEnvNginxReact
-
Install dependencies:
npm install
-
Start the development server:
npm start
-
Open your web browser and navigate to http://localhost:3000 to view the React application.
- Build static js code
npm run build
- Build the Docker image:
docker build -t multienv-nginx-react .
-
Run the Docker container:
docker run --name staging -e ENVIRONMENT=staging -d -p 8080:8080 multienv-nginx-react
-
Open your web browser and navigate to http://localhost:8080 to view the React application served by NGINX.
-
Deploy into K8s production:
kubectl apply -f k8s/deploy.yml
The NGINX server dynamically provides environment information to the React application via the /api/environment
endpoint. The NGINX configuration reads the $ENVIRONMENT
variable from the operating system and includes it in the response header when accessing the endpoint.