Nginx Configuration

Nginx Configuration

Introduction

This guide will walk you through the process of configuring Nginx on Ubuntu 22.04.

What is Nginx?

Nginx is a web server that can also be used as a reverse proxy, load balancer, mail proxy, and HTTP cache.

Prerequisites

  • A server running Nginx on Ubuntu 22.04.
  • A non-root user with sudo privileges

Serve Static Files

Step 1 - Create a Directory Structure

This will be the root directory of your website.

sudo mkdir -p /var/www/example.com/html

Step 2 - Grant Permissions

This will allow Nginx to read and serve the files correctly.

sudo chown -R www-data:www-data /var/www/example.com/html

Step 3 - Create a Sample Index Page

nano /var/www/example.com/html/index.html
<html>
  <head>
    <title>Welcome to example.com!</title>
  </head>
  <body>
    <h1>Success! The example.com server block is working!</h1>
  </body>
</html>

Step 4 - Create a Server Block

This will tell Nginx how to respond to requests for your website.

sudo nano /etc/nginx/sites-available/example.com.conf
server {
  listen 80;
  listen [::]:80;
 
  root /var/www/example.com/html;
  index index.html index.htm index.nginx-debian.html;
 
  server_name example.com www.example.com;
 
  location / {
    try_files $uri $uri/ =404;
  }
}

Reverse Proxy

Prerequisites

  • An application running on any unused port (e.g. 3000)

Step 1 - Configure Nginx

This will tell Nginx how to respond to requests for your website using a reverse proxy.

sudo nano /etc/nginx/sites-available/example.com.conf
server {
  listen 80;
  listen [::]:80;
 
  server_name example.com www.example.com;
 
  # This will tell Nginx to proxy requests to the application running on port 3000
  location / {
    proxy_pass http://localhost:3000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }
}

Running PHP

Prerequisites

  • PHP installed on your server
  • An application written in PHP

Step 1 - Install PHP FPM

In this example, we will be installing PHP 8.2.

sudo apt install php8.2-fpm

You can skip this step if you already have PHP installed on your server.

Step 2 - Configure Nginx

This will tell Nginx how to respond to requests for your website using PHP.

sudo nano /etc/nginx/sites-available/example.com.conf
server {
  listen 80;
  listen [::]:80;
 
  root /var/www/example.com/html;
  index index.php index.html index.htm index.nginx-debian.html;
 
  server_name example.com www.example.com;
 
  location / {
    try_files $uri $uri/ =404;
  }
 
  # This will tell Nginx to pass PHP files to the PHP processor
  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.2-fpm.sock;
  }
}

Step 3 - Create a Sample Index Page

nano /var/www/example.com/html/index.php
<?php
  phpinfo();
?>

Conclusion

You should now have a basic understanding of how to configure Nginx on Ubuntu 22.04.

Resources

What's Next

Nginx SSL/TLS Configuration with Let's Encrypt Certbot

Learn how to configure Nginx with SSL/TLS using Let's Encrypt Certbot.

View