Developer and Text Tools

How to Configure Nginx for a Static Site, SPA, Proxy or PHP App

Set up Nginx correctly: HTTPS with redirects, SPA routing, reverse proxy headers, PHP-FPM, security headers, gzip and caching, then test and reload safely.

Saizul Amin
Saizul Amin30 Oct 2026 · 3 min read
How to Configure Nginx for a Static Site, SPA, Proxy or PHP App

How to Configure Nginx for a Static Site, SPA, Proxy or PHP App

Nginx is fast and reliable, but its configuration files are strict and the snippets people copy are often out of date. A typical first configuration forgets the HTTP to HTTPS redirect, breaks deep links in a React app, loses the visitor's IP address behind the proxy or ships without security headers. The free Nginx Config Generator writes a complete server configuration for four common setups. This guide explains what each part does.

The four site types

Static files. Nginx serves files from a web root. try_files $uri $uri/ =404 returns a file if it exists, a folder index if not, and a 404 otherwise.

Single-page apps. React, Vue and Angular handle routing in the browser, so a visitor who refreshes /account/settings asks the server for a file that does not exist. The fix is to fall back to /index.html for unknown paths. index.html itself is served with no-cache so a new release is picked up at once, while the fingerprinted JavaScript and CSS files are cached for a year.

Reverse proxy. Nginx sits in front of an app server such as Node, Python or Java. It passes the original Host, the client IP in X-Real-IP and X-Forwarded-For, and the scheme in X-Forwarded-Proto, so the app knows who is calling and whether the request was HTTPS. WebSocket support adds the Upgrade and Connection headers.

PHP-FPM. WordPress and Laravel route every request through index.php. PHP files are handed to PHP-FPM through its socket, and hidden files such as .env are blocked.

HTTPS done properly

The configuration has a port 80 server that answers Let's Encrypt challenges and redirects everything else to HTTPS, and a port 443 server with TLS 1.2 and 1.3, session caching and certbot's certificate paths. Get the first certificate with certbot using the webroot method, then reload Nginx. HSTS tells browsers to always use HTTPS for a year; only add preload when every subdomain works over HTTPS, because removal from browser preload lists takes months.

www or not

Pick one address and redirect the other to it, so search engines see one site and cookies behave consistently. The generator writes the redirect block for you.

Security headers

X-Content-Type-Options stops browsers guessing file types, X-Frame-Options blocks other sites framing yours, Referrer-Policy limits what your URLs reveal to other sites and Permissions-Policy turns off camera, microphone and location access you do not use. They are added with always so they appear on error pages too. A Content-Security-Policy is the strongest header but must be written for your site's scripts, so add it once you know what you load.

Performance

Gzip compresses text files, typically shrinking them by 70 percent; images and video are already compressed and are left alone. HTTP/2 lets the browser fetch many files over one connection. client_max_body_size sets the largest upload, which defaults to only 1 MB.

Rate limiting

A per-IP limit, such as ten requests per second with a short burst, slows down password guessing and scrapers without affecting normal visitors. Apply stricter limits to login endpoints if you can.

Testing and reloading safely

Save the file in sites-available and link it into sites-enabled. Always run sudo nginx -t before reloading: it checks syntax and reports the line of any error. sudo systemctl reload nginx then applies the change without dropping connections. Keep the previous file so you can roll back.

Everything is generated in your browser. Review the result and test it on a staging server before production.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages