Repository navigation
How to serve the vite Application on nginx #20669
|
Can anybody say how to serve the vite application properly on nginx. |
Replies: 2 comments
|
For a client-rendered SPA hosted at server {
listen 80;
server_name example.com;
root /var/www/my-app/dist;
index index.html;
location /assets/ {
try_files $uri =404;
}
location / {
try_files $uri $uri/ /index.html;
}
}The fallback allows refreshing client-side routes. Replace the hostname/path and configure HTTPS for production. If you host under References: https://vite.dev/guide/static-deploy.html and https://nginx.org/en/docs/http/ngx_http_core_module.html#try_files Prepared with AI assistance and checked against the linked documentation. I have not reproduced your original project locally. |
|
Build it first ( server {
listen 80;
root /var/www/myapp/dist;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}If the app lives under a subpath (example.com/app/), set — |
For a client-rendered SPA hosted at
/, build it withnpm run build, then serve the contents ofdistas static files:The fallback allows refreshing client-side routes. Replace the hostname/path and configure HTTPS for production. If you host under
/app/, align Vite'sbase, the router basename and nginx's path mapping. An SSR application needs its server runtime instead of this SPA-only configuration.vite previewis for local preview, not a production server.R…