Setting Up Nginx as a Reverse Proxy on AlmaLinux 8

NGINX is an open-source web server that can also be used as a reverse proxy, load balancer, cache server, and media streaming server.

In this guide, we will configure Nginx as a reverse proxy on an AlmaLinux 8 server.

You can learn more about Nginx from the official Nginx website.

Prerequisites

Before proceeding, make sure you have:

  • A system with AlmaLinux 8 installed and running.
  • root access to the server.
  • Nginx installed and running.
  • A domain name pointed to the server.
  • An application running on the backend server, such as 127.0.0.1:8080.

If you need to install the LEMP stack, refer to our guide on How to Install LEMP Stack on AlmaLinux 8.

Configure Nginx

Open the Nginx configuration file:

nano /etc/nginx/nginx.conf

Make sure the configuration contains the following:

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

    access_log /var/log/nginx/access.log main;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    include /etc/nginx/conf.d/*.conf;
}

Save and exit the file.

If you are using nano, press Ctrl+O, press Enter, and then Ctrl+X.

Configure SELinux

If SELinux is enabled on the server, allow Nginx to make network connections:

setsebool -P httpd_can_network_connect 1

Configure Nginx as a Reverse Proxy

Create a new Nginx configuration file:

nano /etc/nginx/conf.d/dev.conf

Add the following configuration:

upstream app {
    server 127.0.0.1:8080;
}

server {
    listen 80;
    server_name dev.domainhere.info;

    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;
    server_name dev.domainhere.info;

    ssl_certificate /etc/letsencrypt/live/dev.domainhere.info/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/dev.domainhere.info/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    location / {
        proxy_pass http://app;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_set_header X-Real-Port $server_port;
        proxy_set_header X-Real-Scheme $scheme;
    }
}

Replace the following values according to your configuration:

  • dev.domainhere.info with your domain name.
  • 127.0.0.1:8080 with the IP address and port of your backend application.
  • /etc/letsencrypt/live/dev.domainhere.info/ with the correct SSL certificate path.

Save and exit the file.

If you are using nano, press Ctrl+O, press Enter, and then Ctrl+X.

If you are using vi, press Esc, type :wq!, and press Enter.

Configure a Domain with SSL

If you want to host a website directly from the server instead of forwarding requests to a backend application, create a configuration file:

nano /etc/nginx/conf.d/dev.conf

Add the following configuration:

server {
    listen 80;
    server_name dev.domainhere.info;

    root /var/www/html/;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/run/php-fpm/www.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/dev.domainhere.info/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/dev.domainhere.info/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}

Replace dev.domainhere.info with your domain name.

Replace /var/www/html/ with the actual directory containing your website files.

Replace the SSL certificate paths if your certificates are stored in a different location.

Save and exit the file.

Set Website Permissions

If your website files are stored in /var/www/html/, set the appropriate permissions:

chmod -R 755 /var/www/html/

Set Nginx as the owner:

chown -R nginx:nginx /var/www/html/

If SELinux is enabled, run:

setsebool -P httpd_can_network_connect 1

Test the Nginx Configuration

Before restarting Nginx, check the configuration for syntax errors:

nginx -t

If the configuration is valid, you should see output similar to:

syntax is ok
test is successful

Restart Nginx

Restart Nginx:

systemctl restart nginx

Check the Nginx service status:

systemctl status nginx

Enable HTTP and HTTPS in the Firewall

Allow HTTP traffic through port 80:

firewall-cmd --permanent --zone=public --add-service=http

Allow HTTPS traffic through port 443:

firewall-cmd --permanent --zone=public --add-service=https

Reload the firewall:

firewall-cmd --reload

Verify the firewall rules:

firewall-cmd --list-services

You should see:

http https

Verify the Reverse Proxy

Open your domain in a web browser:

https://dev.domainhere.info

Nginx should receive the request and forward it to the configured backend application.

You can also test the backend from the server using:

curl http://127.0.0.1:8080

If the backend application is responding correctly, Nginx should be able to proxy requests to it.

Request Flow

The basic reverse proxy request flow is:

Client
   |
   | HTTPS :443
   v
Nginx
   |
   | HTTP
   v
Backend Application
127.0.0.1:8080

Conclusion

Nginx is now configured as a reverse proxy on AlmaLinux 8.

The same Nginx server can also be configured to serve websites directly from a document root such as /var/www/html/.

References


CrownCloud - Get a SSD powered KVM VPS at $4.5/month!
Use the code WELCOME for 10% off!

1 GB RAM / 25 GB SSD / 1 CPU Core / 1 TB Bandwidth per month

Available Locations: LAX | MIA | ATL | FRA | AMS