Databáze řešení

How to Deploy a React App with Nginx

After building a React app you get static files that any web server can host. This guide serves them with Nginx.

1. Build the production bundle

npm run build

This creates a dist/ (Vite) or build/ (CRA) folder of static files.

2. Copy files to the web root

sudo mkdir -p /var/www/myapp
sudo cp -r dist/* /var/www/myapp/

3. Configure Nginx (with SPA fallback)

server {
    listen 80;
    server_name myapp.example.com;
    root /var/www/myapp;
    index index.html;

    location / {
        try_files $uri $uri/ /index.html;
    }
}

The try_files ... /index.html line ensures client-side routing works on refresh.

4. Enable and reload

sudo nginx -t && sudo systemctl reload nginx

Add HTTPS with sudo certbot --nginx and your React app is live.

Byla tato odpověď nápomocná?

0 Uživatelům pomohlo

Powered by WHMCompleteSolution