How to Configure Nginx for a Website is an important skill for beginners who want to host websites on a VPS, cloud server, or Linux-based server.
Nginx, pronounced “engine-x,” is a popular web server and reverse proxy used to serve websites and web applications. It can serve static files such as HTML, CSS, JavaScript, and images, while also forwarding requests to application servers such as Node.js, Python, PHP, or other backend technologies.
A basic website architecture might look like this:
Visitor
↓
Internet
↓
Nginx
↓
Website / Application
For a PHP application, the architecture may look like:
Browser
↓
Nginx
↓
PHP-FPM
↓
PHP Application
↓
Database
For a Node.js application:
Browser
↓
Nginx
↓
Node.js
↓
Database / APIs
Understanding How to Configure Nginx for a Website helps you create reliable server configurations, connect domains to websites, enable HTTPS, configure PHP, set up reverse proxies, manage logs, and apply basic security controls.
This beginner-friendly guide explains How to Configure Nginx for a Website from installation through testing and troubleshooting.
What Is Nginx?
Before learning How to Configure Nginx for a Website, you should understand what Nginx does.
Nginx is a web server and reverse proxy that can handle HTTP and HTTPS requests.
It is commonly used for:
- Hosting static websites.
- Serving HTML files.
- Serving CSS and JavaScript.
- Serving images and other assets.
- Reverse proxying applications.
- Load balancing.
- TLS termination.
- Caching.
- Routing requests.
- Serving PHP applications through PHP-FPM.
According to the official Nginx documentation, Nginx can function as an HTTP server, reverse proxy, mail proxy, and more.
For a beginner, the most important concept is that Nginx receives requests from visitors and determines how those requests should be handled.
Why Use Nginx for a Website?
Learning How to Configure Nginx for a Website is useful because Nginx is designed to handle large numbers of concurrent connections efficiently.
Common reasons to use Nginx include:
- Good performance for static files.
- Reverse-proxy capabilities.
- Flexible routing.
- HTTPS support.
- Virtual host support.
- Low resource consumption.
- Load-balancing capabilities.
- Strong ecosystem and documentation.
For example, one server can host several websites:
example.com
blog.example.com
shop.example.com
api.example.com
Nginx can use separate server blocks to determine which website should respond to each hostname.
How to Configure Nginx for a Website Step by Step
The basic process for How to Configure Nginx for a Website is:
- Prepare a Linux server.
- Install Nginx.
- Create the website directory.
- Add website files.
- Configure a server block.
- Configure the domain’s DNS.
- Test Nginx.
- Enable the configuration.
- Restart or reload Nginx.
- Configure HTTPS.
- Redirect HTTP to HTTPS.
- Add security settings.
- Review logs.
- Test the website.
Let’s go through each step.
Step 1: Prepare Your Server
Before learning How to Configure Nginx for a Website, you need a server.
You can use:
- VPS.
- Cloud virtual machine.
- Dedicated server.
- Local Linux machine.
- Development server.
Popular Linux distributions include:
- Ubuntu.
- Debian.
- AlmaLinux.
- Rocky Linux.
For this tutorial, examples use an Ubuntu/Debian-style environment.
You should have:
- Server access.
- A user with administrative privileges.
- A domain name.
- A public IP address.
- SSH access.
For production systems, avoid performing routine administration directly as the root user when a properly configured administrative account can be used.
Step 2: Install Nginx
On Ubuntu or Debian, update the package information:
sudo apt update
Then install Nginx:
sudo apt install nginx
After installation, check the service:
sudo systemctl status nginx
You should see that Nginx is running.
You can also check the installed version:
nginx -v
The exact version depends on your Linux distribution and repository.
For official installation instructions, consult the Nginx Beginner’s Guide.
Step 3: Check the Default Nginx Page
After installation, open the server’s IP address in a browser:
http://YOUR_SERVER_IP
If Nginx is running correctly, you should see the default Nginx page or another configured website.
This confirms that:
Browser
↓
Server IP
↓
Nginx
is working.
If the page does not load, check:
- Nginx status.
- Firewall.
- Cloud security rules.
- Port 80.
- Server networking.
Step 4: Create a Website Directory
The next step in How to Configure Nginx for a Website is creating a directory for your website.
For example:
sudo mkdir -p /var/www/example.com
Create a simple HTML file:
sudo nano /var/www/example.com/index.html
Add:
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
</head>
<body>
<h1>Welcome to my website</h1>
<p>Nginx is working correctly.</p>
</body>
</html>
Save the file.
Your website files are now located at:
/var/www/example.com
Step 5: Set Appropriate Permissions
Nginx needs permission to read the website files.
You can set ownership according to your server’s configuration.
For example:
sudo chown -R $USER:$USER /var/www/example.com
Then ensure directories and files have appropriate permissions.
Avoid using overly permissive permissions such as:
chmod -R 777 /var/www/example.com
This can create unnecessary security risks.
Use the minimum permissions required by your application.
Step 6: Configure the Domain DNS
One of the most important parts of How to Configure Nginx for a Website happens outside Nginx itself.
Your domain needs to point to the server.
Suppose your server IP is:
203.0.113.25
Create an A record:
Type: A
Name: @
Value: 203.0.113.25
For www, you might use:
Type: A
Name: www
Value: 203.0.113.25
or:
Type: CNAME
Name: www
Value: example.com
The exact configuration depends on your DNS provider.
You can test the DNS record with:
dig example.com A +short
The result should show your server’s public IP address.
If DNS is not configured correctly, Nginx cannot receive traffic through the intended domain.
For more information, see your related article How to Point a Subdomain to a Server.
Step 7: Create an Nginx Server Block
A server block tells Nginx how to handle requests for a specific domain.
Create a configuration file:
sudo nano /etc/nginx/sites-available/example.com
Add:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
The important settings are:
listen
listen 80;
This tells Nginx to listen for HTTP traffic on port 80.
server_name
server_name example.com www.example.com;
This tells Nginx which hostnames the configuration handles.
root
root /var/www/example.com;
This specifies where the website files are stored.
index
index index.html;
This tells Nginx which file to serve as the default document.
location
location / {
try_files $uri $uri/ =404;
}
This defines how requests should be handled.
Step 8: Enable the Nginx Configuration
On Debian and Ubuntu systems, you can create a symbolic link:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
Then test the configuration:
sudo nginx -t
If you see a successful syntax test, reload Nginx:
sudo systemctl reload nginx
This is an important part of How to Configure Nginx for a Website.
Never make large configuration changes and reload production Nginx without testing the configuration first.
Step 9: Test the Website
Open:
http://example.com
You should see:
Welcome to my website
You can also test using:
curl -I http://example.com
A successful response may include:
HTTP/1.1 200 OK
If you receive a 404, 403, or 502 error, investigate the Nginx configuration and logs.
How to Configure Nginx for a Website With HTTPS
Modern websites should generally use HTTPS.
After your HTTP website works, the next step in How to Configure Nginx for a Website is configuring TLS.
You need a certificate covering:
example.com
www.example.com
One common option is Let’s Encrypt, which provides automated TLS certificates.
A common Ubuntu workflow uses Certbot.
For example, depending on your distribution and installation method:
sudo apt install certbot python3-certbot-nginx
Then:
sudo certbot --nginx -d example.com -d www.example.com
The exact package and Certbot instructions can vary by operating system. Follow the current Certbot documentation.
Certbot can configure certificates and, where selected, HTTP-to-HTTPS redirects.
How to Configure Nginx for a Website With HTTP-to-HTTPS Redirects
After HTTPS is working, you generally want:
http://example.com
to redirect to:
https://example.com
A simple Nginx configuration is:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
Your HTTPS server block then handles port 443.
For example:
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html;
ssl_certificate /path/to/certificate.crt;
ssl_certificate_key /path/to/private.key;
location / {
try_files $uri $uri/ =404;
}
}
The exact TLS settings should follow current Nginx and certificate-provider recommendations.
How to Configure Nginx for a PHP Website
If your website uses PHP, Nginx does not execute PHP directly. It typically passes PHP requests to PHP-FPM.
Install PHP-FPM using the package appropriate for your distribution.
For example:
sudo apt install php-fpm
A simplified server configuration could look like:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php-fpm.sock;
}
}
The PHP-FPM socket path varies depending on the installed PHP version and operating system.
Check the actual socket on your server before using the configuration.
For PHP applications, make sure the PHP version is supported and properly secured.
How to Configure Nginx for a WordPress Website
WordPress is a common use case for Nginx.
A simplified configuration can look like:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
The important WordPress setting is:
try_files $uri $uri/ /index.php?$args;
This allows WordPress’s front controller to handle URLs that do not correspond to physical files.
Always adjust the PHP-FPM socket path to match your server.
For WordPress, also consider:
- HTTPS.
- Security headers.
- File permissions.
- Regular backups.
- Plugin updates.
- PHP updates.
- Database backups.
You can connect this article to your related guide How to Create Scheduled Website Backups.
How to Configure Nginx as a Reverse Proxy
One of the most useful features to understand when learning How to Configure Nginx for a Website is reverse proxying.
Suppose your Node.js application runs on:
127.0.0.1:3000
Visitors should access:
https://app.example.com
Nginx can sit between the browser and application.
The architecture becomes:
Browser
↓
HTTPS
↓
Nginx
↓
127.0.0.1:3000
↓
Node.js
A simplified configuration:
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://127.0.0.1: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;
}
}
This allows Nginx to receive public requests and forward them to the application.
How to Configure Nginx for Multiple Websites
Nginx can host multiple websites on the same server.
For example:
example.com
blog.example.com
shop.example.com
You can create separate server blocks:
/etc/nginx/sites-available/example.com
/etc/nginx/sites-available/blog.example.com
/etc/nginx/sites-available/shop.example.com
Each configuration can have its own:
server_nameroot- SSL certificate
- Logs
- Proxy configuration
- Application settings
For example:
server {
listen 80;
server_name example.com;
root /var/www/example.com;
}
and:
server {
listen 80;
server_name blog.example.com;
root /var/www/blog;
}
This is commonly called virtual hosting.
How to Configure Nginx for a Subdomain
If you want:
app.example.com
to point to an application, create a DNS record first.
For example:
Type: A
Name: app
Value: 203.0.113.25
Then create an Nginx server block:
server {
listen 80;
server_name app.example.com;
root /var/www/app;
index index.html;
}
After that:
sudo nginx -t
sudo systemctl reload nginx
You can then test:
http://app.example.com
For more detailed DNS instructions, see How to Point a Subdomain to a Server.
How to Configure Nginx Security Headers
Another important part of How to Configure Nginx for a Website is adding appropriate security headers.
For example:
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
You may also configure:
add_header Strict-Transport-Security "max-age=31536000" always;
However, HSTS should be enabled only after HTTPS is correctly configured and you understand the implications for your domain and subdomains.
Content Security Policy requires more careful application-specific configuration.
For example:
add_header Content-Security-Policy "default-src 'self';" always;
A restrictive CSP can break JavaScript, styles, fonts, images, analytics, and third-party integrations, so test it carefully.
You can connect this topic with your related article How to Add Security Headers to a Website.
How to Configure Nginx Logs
Nginx provides access and error logs that are extremely useful when troubleshooting.
Common locations include:
/var/log/nginx/access.log
/var/log/nginx/error.log
You can view the latest errors:
sudo tail -f /var/log/nginx/error.log
View access logs:
sudo tail -f /var/log/nginx/access.log
For a specific website, you can configure separate logs:
access_log /var/log/nginx/example-access.log;
error_log /var/log/nginx/example-error.log;
Logs can help identify:
- 404 errors.
- 403 errors.
- 502 errors.
- PHP-FPM problems.
- Permission problems.
- Upstream failures.
- Incorrect routes.
Common Nginx Errors and How to Fix Them
1. 404 Not Found
A 404 usually means the requested resource could not be found.
Check:
root /var/www/example.com;
Make sure the requested file actually exists.
For WordPress, check your try_files configuration.
2. 403 Forbidden
A 403 can be caused by:
- File permissions.
- Directory permissions.
- Nginx access rules.
- Missing index file.
- Application authorization.
Check the error log:
sudo tail -f /var/log/nginx/error.log
3. 502 Bad Gateway
A 502 commonly occurs when Nginx cannot communicate correctly with an upstream application.
For example:
Nginx
↓
Node.js
If Node.js is not running on port 3000, Nginx may return 502.
Check:
sudo systemctl status your-app
and verify the configured upstream address.
For PHP, check PHP-FPM status.
4. Nginx Configuration Test Failed
Run:
sudo nginx -t
The output normally identifies the configuration file and line containing the problem.
Fix the syntax error before reloading Nginx.
How to Check Whether Nginx Is Running
Use:
sudo systemctl status nginx
To restart:
sudo systemctl restart nginx
To reload configuration without a full restart:
sudo systemctl reload nginx
For normal configuration changes, a reload is generally preferable because it allows Nginx to apply the new configuration without unnecessarily stopping the service.
Always run:
sudo nginx -t
before reloading.
How to Configure a Firewall for Nginx
Your server firewall must allow the ports needed by your website.
For a standard HTTP/HTTPS website, these are generally:
Port 80 → HTTP
Port 443 → HTTPS
If using UFW, you might use an Nginx profile such as:
sudo ufw allow 'Nginx Full'
Then check:
sudo ufw status
The exact firewall configuration depends on your server and security requirements.
Do not open unnecessary ports to the public internet.
Nginx Performance Best Practices
When learning How to Configure Nginx for a Website, basic performance optimization can also be useful.
Consider:
- Browser caching.
- Gzip or Brotli compression where appropriate.
- Static file caching.
- HTTP/2 or HTTP/3 where supported and appropriately configured.
- Efficient images.
- Application caching.
- CDN integration.
- Connection handling.
- Proper upstream configuration.
For example, you can configure caching headers for static assets:
location ~* \.(css|js|jpg|jpeg|png|gif|svg|webp)$ {
expires 30d;
add_header Cache-Control "public";
}
Be careful with long cache durations if your deployment process does not use versioned asset filenames.
How to Configure Nginx for Website Maintenance
Regular maintenance is important after learning How to Configure Nginx for a Website.
Review:
- Nginx updates.
- Operating-system updates.
- TLS certificates.
- Firewall rules.
- Website files.
- Application dependencies.
- PHP versions.
- Logs.
- Backup status.
- DNS configuration.
You should also maintain backups before making significant server configuration changes.
See your related article How to Create Scheduled Website Backups for backup planning.
How to Test an Nginx Website
After completing How to Configure Nginx for a Website, perform several tests.
Test DNS
dig example.com A +short
Test HTTP
curl -I http://example.com
Test HTTPS
curl -I https://example.com
Test Nginx Configuration
sudo nginx -t
Check Nginx Status
sudo systemctl status nginx
Check Error Logs
sudo tail -f /var/log/nginx/error.log
Check Application
If using Node.js, PHP, Python, or another backend, make sure the application is running and accessible to Nginx.
Common Mistakes Beginners Make
When learning How to Configure Nginx for a Website, avoid these common mistakes.
1. Forgetting DNS
Creating an Nginx configuration does not automatically make your domain point to the server.
2. Using the Wrong server_name
Make sure the hostname exactly matches the domain or subdomain.
3. Wrong Document Root
Check that:
root /var/www/example.com;
points to the directory containing the website.
4. Not Running nginx -t
Always test configuration before reloading.
5. Using Overly Permissive File Permissions
Do not use 777 simply to solve permission problems.
6. Forgetting HTTPS
Do not stop after HTTP works.
7. Incorrect PHP-FPM Socket
The PHP-FPM socket varies by version and distribution.
8. Incorrect Reverse Proxy Port
If your Node.js application runs on port 3000, Nginx must proxy to the correct port.
9. Ignoring Logs
Nginx logs often provide the fastest clue when something fails.
10. Making Changes Without Backups
Always maintain a reliable backup strategy before major production changes.
Nginx Configuration Checklist
Use this checklist when following How to Configure Nginx for a Website:
- Prepare a Linux server.
- Install Nginx.
- Verify Nginx is running.
- Configure the domain DNS.
- Create the website directory.
- Upload website files.
- Create an Nginx server block.
- Configure
server_name. - Configure the document root.
- Enable the server block.
- Run
nginx -t. - Reload Nginx.
- Test the website.
- Configure HTTPS.
- Redirect HTTP to HTTPS.
- Configure PHP-FPM if needed.
- Configure reverse proxy if needed.
- Configure appropriate security headers.
- Check firewall rules.
- Review Nginx logs.
- Create scheduled backups.
- Test the final configuration.
Frequently Asked Questions
What is Nginx?
Nginx is a web server and reverse proxy commonly used to host websites and route requests to web applications.
Is Nginx free?
Yes. Nginx is available as open-source software, with commercial offerings also available.
Can Nginx host multiple websites?
Yes. Nginx can host multiple domains and subdomains using separate server blocks.
Can Nginx run PHP?
Nginx does not directly execute PHP. It commonly passes PHP requests to PHP-FPM.
Can Nginx run Node.js?
Nginx can reverse proxy requests to a Node.js application running on another port.
Do I need a VPS to use Nginx?
Not necessarily. Nginx may already be provided by a hosting company, cloud platform, container environment, or managed service. A VPS gives you more direct control over configuration.
How do I check my Nginx configuration?
Run:
sudo nginx -t
This checks the configuration syntax.
Where are Nginx logs stored?
On many Linux distributions, logs are located under:
/var/log/nginx/
The exact location can vary according to your configuration.
How do I restart Nginx?
Use:
sudo systemctl restart nginx
For configuration changes, you can often use:
sudo systemctl reload nginx
after validating the configuration.
Why does Nginx show 502 Bad Gateway?
A 502 often means Nginx cannot successfully communicate with the configured upstream application. Check the upstream service, port, socket, firewall, and Nginx error logs.
Related Internal Links
For your beginner-friendly website development and security content cluster, add internal links to:
- How to Enable HTTPS on a Website
- How to Point a Subdomain to a Server
- How to Set Up a Staging Subdomain
- How to Troubleshoot DNS Propagation Problems
- How to Create Scheduled Website Backups
- How to Add Security Headers to a Website
- How to Secure API Keys in Web Applications
- How to Prevent SQL Injection
- How to Prevent Cross-Site Scripting in a Web Application
- How to Protect Forms Against CSRF
- How to Secure File Uploads in PHP
- How to Implement Secure Password Storage
Replace these article titles with your actual internal URLs before publishing. Since your website domain was not provided, no internal URLs have been invented.
Best Practices for Nginx Website Configuration
The key principles of How to Configure Nginx for a Website are:
- Keep configurations organized.
- Use separate server blocks for separate websites.
- Use correct DNS records.
- Use the correct
server_name. - Keep website files in appropriate directories.
- Use proper file permissions.
- Test configuration with
nginx -t. - Enable HTTPS.
- Redirect HTTP to HTTPS.
- Protect sensitive applications.
- Keep Nginx and the operating system updated.
- Monitor access and error logs.
- Configure firewalls carefully.
- Use reverse proxy settings correctly for applications.
- Maintain regular backups.
- Test changes before applying them to production.
A good Nginx configuration should be simple, documented, secure, and easy to troubleshoot.
Conclusion
How to Configure Nginx for a Website is an important foundational skill for developers and website administrators.
The basic workflow can be summarized as:
Domain
↓
DNS
↓
Server IP
↓
Nginx
↓
Server Block
↓
Website / PHP / Node.js
↓
HTTPS
↓
Visitor
For a simple static website, you need a domain, DNS record, website directory, and Nginx server block.
For a PHP website, you also need PHP-FPM.
For a Node.js or other application server, Nginx can act as a reverse proxy.
The most important lesson from How to Configure Nginx for a Website is to configure each layer correctly. DNS must point to the correct server, Nginx must recognize the hostname, the document root or upstream application must be correct, HTTPS must be configured properly, and firewall rules must allow the required traffic.
Always test configuration changes with:
sudo nginx -t
before reloading Nginx.
Also use Nginx logs when troubleshooting errors such as 403, 404, and 502.
By following this guide, beginners can build a reliable Nginx setup for static websites, PHP applications, WordPress websites, Node.js applications, subdomains, and reverse-proxy architectures.
Recommended External Resources
- Nginx Official Documentation
- Nginx Beginner’s Guide
- Let’s Encrypt
- Certbot Documentation
- OWASP Web Security
- MDN HTTP Documentation
SEO implementation check: The focus keyword How to Configure Nginx for a Website is included in the SEO title, meta description, URL slug, opening paragraph, multiple H2/H3 subheadings, body content, checklist, FAQ, and conclusion. The focus keyword and natural keyword combinations appear more than 30 times, targeting a keyword density of at least 1%. The article exceeds 1,500 words, includes the requested image alt-text specification, external DoFollow resource opportunities, and internal-link opportunities.





Comments