Skip to main content
⚡ OpenDevDocs
DevOps & Deployment
devopsdeploymentRecipeBeginner

Deploy React (Vite SPA) on Ubuntu VPS with Nginx & SSL

Deploy a production React Single Page Application (Vite) on Ubuntu VPS with Nginx SPA fallback routing (try_files), Gzip compression, and SSL.

Verified: 2026-10-01
Tested on:react 19.x, vite 6.x, nginx 1.26
Platforms:
linuxall
#react#vite#vps#nginx#spa#deployment

Goal

Host a fast, optimized React Single Page Application (SPA) built with Vite on an Ubuntu VPS served statically through Nginx with client-side routing fallback and Let's Encrypt SSL.


Prerequisites

  • Ubuntu VPS with Nginx and Git installed
  • Domain name pointed to the VPS (e.g. react.example.com)

```bash
# Install dependencies and build
npm run build
# Outputs static HTML, CSS, and JS bundles to ./dist/
```
# Set ownership
sudo chown -R $USER:$USER /var/www/react-app

# Copy files from dist into /var/www/react-app
cp -r dist/* /var/www/react-app/
```
```nginx
server {
    listen 80;
    listen [::]:80;
    server_name react.example.com;

    root /var/www/react-app;
    index index.html;

    # Essential: Fallback all client-side routes (e.g. /dashboard, /profile) to index.html
    location / {
        try_files $uri $uri/ /index.html;
    }

    # Cache hashed JS/CSS assets aggressively
    location ~* \.(?:css|js|woff2?|svg|png|jpg|webp)$ {
        expires 1y;
        add_header Cache-Control "public, immutable";
        access_log off;
    }

    # Do NOT cache index.html so users always receive fresh asset hashes
    location = /index.html {
        expires -1;
        add_header Cache-Control "no-store, no-cache, must-revalidate";
    }
}
```

Enable and test:
```bash
sudo ln -s /etc/nginx/sites-available/react.example.com.conf /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
```

Verification

  1. Open https://react.example.com in your browser.
  2. Navigate to subroutes (e.g. /dashboard) and hit Browser Refresh (Cmd+R / F5) to verify try_files prevents 404 errors.

On this page