Configuration Steps

Step 1: Basic NGINX Setup Start by opening the NGINX configuration file located at /etc/nginx/nginx.conf. This file controls how NGINX operates. You’ll need to add a new stream block where you can define your proxy settings.


stream {
    # Database proxy configuration will go here
}

Step 2: Define the Proxy Server

Inside the stream block, define a new server block. This is where you’ll specify the connection details for your database proxy.


stream {
    server {
        listen 3306; # Listen on the port your database uses (3306 is default for MySQL)
        proxy_pass db_server:3306; # Replace 'db_server' with your database server's address
    }
}

Step 3: Configure Load Balancing (Optional)

If you have multiple database servers and wish to implement load balancing, you can define an upstream block within the stream section.


stream {
    upstream db_servers {
        server db_server1:3306;
        server db_server2:3306;
    }

    server {
        listen 3306;
        proxy_pass db_servers;
    }
}

Step 4: Testing and Restarting NGINX

After configuring NGINX, it’s crucial to test your configuration for any errors:


sudo nginx -t

If the test is successful, restart NGINX to apply the changes:


sudo systemctl restart nginx