No Login Data Private Local Save

Nginx Location Block Builder - Online Config Snippet

7
0
0
0

Nginx Location Block Builder

Visually construct Nginx location blocks with proxy, static, FastCGI, redirects, CORS, caching & more. Get production-ready config snippets instantly.

Quick Presets
API Reverse Proxy SPA (try_files) Static Cache CORS Open Maintenance PHP-FPM
Basic Settings
Use /api/ for prefix, \.php$ for regex
Proxy Configuration
Advanced Options
Generated Snippet ~0 lines
location /api/ {
    # Proxy configuration
    proxy_pass http://localhost:3000/;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_read_timeout 60s;
    proxy_connect_timeout 10s;
    proxy_send_timeout 60s;
    proxy_buffering on;
}

Frequently Asked Questions

What is the difference between root and alias in Nginx?

root appends the location path to the root directory. For location /images/ with root /var/www, a request for /images/photo.jpg looks in /var/www/images/photo.jpg.

alias replaces the matched location portion entirely. For location /images/ with alias /var/www/photos/, the same request looks in /var/www/photos/photo.jpg. Note: alias requires a trailing slash if the location path has one.

How does Nginx location matching priority work?
  1. = (exact match) — Highest priority. Matches the URI exactly.
  2. ^~ (preferential prefix) — If the longest matching prefix location uses ^~, Nginx skips regex checks.
  3. ~ and ~* (regex) — Checked in order of appearance in the config file. First match wins.
  4. Plain prefix — Lowest priority. Nginx picks the longest matching prefix if no regex matches.
When should I use try_files in a location block?

try_files is essential for Single Page Applications (SPAs) built with React, Vue, or Angular. It tells Nginx to check for a file, then a directory, and finally fall back to index.html so client-side routing works correctly: try_files $uri $uri/ /index.html;.

It's also useful for serving cached files first, then falling back to a backend: try_files $uri @backend;

Why does the trailing slash in proxy_pass matter?

With location /api/ and proxy_pass http://backend/ (both with trailing slashes), a request to /api/users becomes http://backend/users — the /api/ part is stripped.

With proxy_pass http://backend (no trailing slash), the same request becomes http://backend/api/users — the full path is preserved. This is one of the most common Nginx configuration mistakes.

How do I configure CORS headers correctly in Nginx?

Add the following inside your location block:

add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type' always;
if ($request_method = 'OPTIONS') { return 204; }

Use always to ensure headers are added even for non-2xx responses. Our tool's CORS toggle handles this automatically.

What's the best caching strategy for static assets?

For versioned/hashed assets (e.g. app.a1b2c3.js), use long-term caching with immutable: add_header Cache-Control "public, max-age=31536000, immutable";.

For unversioned assets, use shorter TTLs with revalidation: add_header Cache-Control "public, max-age=3600, must-revalidate";. Our tool's cache presets make this easy.

How do I test my Nginx configuration before applying it?

Always run nginx -t to test your configuration syntax before reloading. This catches typos and invalid directives. Then apply with nginx -s reload for a graceful reload without dropping connections.

Can I combine multiple handler types in one location block?

Generally, a single location block should have one primary handler (proxy_pass, root, try_files, or return). However, you can combine them with advanced techniques like try_files with a named location fallback (@backend). Our tool focuses on clean, single-purpose location blocks that follow Nginx best practices.