Nginx Config Generator
Generate common Nginx server block examples for websites and reverse proxies.
Generated Nginx Config
Common Uses
- Basic PHP site with index.php fallback
- Reverse proxy to an app on another port
- HTTP to HTTPS redirect block
- Simple static HTML site
Intro
Generate common Nginx server block examples for websites, reverse proxies, redirects, and static sites.
This tool helps you create a starting Nginx configuration for common hosting scenarios without writing every directive manually. It can be useful when setting up a basic PHP website, configuring a reverse proxy for an application, creating an HTTP-to-HTTPS redirect, or serving a static HTML site.
A generated Nginx config should always be reviewed before production use. File paths, upstream ports, PHP handling, SSL settings, redirects, and security rules may need to be adjusted for your server, operating system, and application stack.
What Is an Nginx Server Block?
An Nginx server block defines how a domain or hostname should be handled by the Nginx web server.
A server block can control things like:
- which domain names the config responds to
- which port it listens on
- where the website files are stored
- how PHP requests are processed
- whether traffic is proxied to another application
- whether HTTP requests are redirected to HTTPS
In practice, a server block is the main unit of configuration for serving websites and applications with Nginx.
Common Nginx Configuration Types
This generator already supports several common setup types on the live page. Those include: (tech.kelsey-mcguire.com)
- Basic PHP Site
- Reverse Proxy
- HTTP to HTTPS Redirect
- Static Site
Each one solves a different problem.
Basic PHP Site
Use this when hosting a standard PHP-based website such as:
- WordPress
- Laravel
- custom PHP apps
- admin panels
- small business websites
This type of config usually includes:
- a document root
index.phphandlingtry_filesfallback- PHP-FPM integration
Reverse Proxy
Use this when Nginx should sit in front of another application that runs on a different port.
Common reverse proxy targets include:
- Node.js apps
- Docker containers
- internal dashboards
- self-hosted tools
- application servers running on
localhost
This is useful when you want Nginx to handle:
- public HTTP or HTTPS access
- host-based routing
- proxy headers
- buffering and performance tuning
- TLS termination
HTTP to HTTPS Redirect
Use this when you want all plain HTTP traffic redirected to HTTPS.
This is commonly used when:
- a certificate is already installed
- you want one canonical secure version of the site
- you are cleaning up mixed access paths
- you want a standard redirect for browsers and crawlers
Static Site
Use this for simple websites that serve HTML, CSS, JavaScript, and media files directly from disk.
This is often the right choice for:
- landing pages
- documentation sites
- simple business websites
- exported frontend builds
- maintenance pages
When to Use Nginx
Nginx is commonly used for:
- high-performance website hosting
- reverse proxy setups
- load balancing
- static file delivery
- TLS termination
- application routing
It is especially popular when you need efficient handling of many requests, clean virtual host management, or a front-end proxy for backend services.
Key Nginx Directives Explained
Understanding a few core directives makes generated configs much easier to review.
server_name
This tells Nginx which domain or hostname the server block should respond to.
Examples:
example.comwww.example.comapp.example.com
If server_name is wrong, Nginx may serve the wrong site or fall back to a default config.
listen
This defines which port the server block listens on.
Common values include:
80for HTTP443for HTTPS
If the wrong port is used, requests may never reach the correct server block.
root
This sets the document root, or the directory where site files are stored.
Example:
/var/www/example.com/public
If the root path is wrong, you may see:
- 404 errors
- forbidden responses
- the wrong site content
index
This defines which default files Nginx should serve when a directory is requested.
Common values include:
index.htmlindex.php
try_files
This tells Nginx which files or paths to try before returning an error or passing control elsewhere.
In PHP apps, try_files is often used to support:
- front-controller routing
- clean URLs
- framework entry points
A common example is routing unmatched requests to index.php.
location
A location block defines how Nginx should handle certain URL paths or file types.
This is where you often configure:
- reverse proxy rules
- PHP processing
- static file behavior
- access restrictions
proxy_pass
This sends traffic from Nginx to another application server.
It is commonly used for:
- Node.js apps
- internal dashboards
- Docker containers
- backend APIs
return 301
This is commonly used for permanent redirects, including HTTP-to-HTTPS redirection.
Common Nginx Mistakes
Many Nginx problems come from a few recurring issues.
Wrong Document Root
If the root path does not match the actual site directory, the site may return:
- 404 errors
- 403 forbidden errors
- the wrong website content
Bad PHP Handling
A PHP site config needs the correct PHP-FPM socket or upstream definition. If that is wrong, PHP files may download instead of execute, or the site may return 502 errors.
Incorrect Reverse Proxy Target
If proxy_pass points to the wrong port or upstream service, the site may fail to load or return a bad gateway error.
Missing HTTPS Configuration
A redirect to HTTPS should only be used when HTTPS is actually configured and working. Otherwise users may be redirected to a broken endpoint.
Syntax Errors
A missing semicolon, bad bracket, or typo can prevent Nginx from reloading successfully.
Conflicting Server Blocks
If multiple server blocks respond to the same hostname, Nginx may not serve the site you expect.
How to Test an Nginx Config Safely
Before reloading Nginx, always test the configuration.
A safe workflow is:
- generate the config
- review paths, ports, and hostnames
- save it in the correct site config location
- run a config test
- reload Nginx only after the test passes
Common commands are:
If the config test fails:
- read the exact line number in the error
- check for missing semicolons
- confirm closing braces are balanced
- verify file paths and upstream values
- check whether referenced sockets or services exist
Example Use Cases
Hosting a Basic PHP Website
Use a PHP site config when deploying:
- WordPress
- a small custom PHP app
- an internal admin portal
Proxying an App on Port 3000
Use a reverse proxy when a backend app runs on:
127.0.0.1:3000localhost:8080- a Docker container port
This is common for:
- Node.js
- React or Next.js apps behind a server
- internal dashboards
- self-hosted applications
Redirecting All HTTP Traffic to HTTPS
Use a redirect block when:
- the certificate is installed
- HTTPS is working correctly
- you want a single secure version of the site
Serving a Static HTML Site
Use a static config when the site does not need PHP or an application server.
This is ideal for:
- simple pages
- documentation
- exported frontend builds
- maintenance sites
Best Practices for Nginx Configs
When using a generated Nginx config:
- review every hostname and path
- verify the document root exists
- confirm the upstream port is correct
- test config syntax before reload
- keep one clear config per site
- use HTTPS where appropriate
- back up working configs before major changes
- document any custom edits you make later
For production systems, it is also smart to keep Nginx configs under version control.
Frequently Asked Questions
What does this generator create?
It creates a starter Nginx server block for common use cases like PHP sites, reverse proxies, redirects, and static websites.
Is the generated config production-ready?
It should be treated as a starting point. You still need to review file paths, SSL settings, upstream services, PHP handling, and security requirements.
When should I use a reverse proxy?
Use a reverse proxy when your application runs behind Nginx on another port or internal service.
What is the difference between a static site and a PHP site config?
A static site serves files directly. A PHP site also needs PHP request handling, often through PHP-FPM.
Why does Nginx return 502 Bad Gateway?
This usually means Nginx cannot connect to the configured upstream service or PHP handler.
Why should I run nginx -t first?
It checks the configuration for syntax and some structural problems before you reload the service.
Related Tools
You may also find these tools useful:
- htaccess Redirect Generator
- SSL Certificate Checker
- HTTP Header Checker
- DNS Lookup Tool
- Port Lookup Tool
Final Note
This Nginx Config Generator is useful for creating a quick starting point for common website and reverse proxy setups.
Use it to save time, but always review the generated config carefully before deploying it. A few minutes of validation can prevent broken sites, bad redirects, PHP errors, and failed reloads.
