Tutorial: How to Add a Reverse Proxy in Nginx on HestiaCP

tutorials- webadmin

This tutorial will guide you through setting up a reverse proxy in Nginx when using HestiaCP. The instructions are based on a community discussion where users solved this configuration challenge.

Understanding the Problem

When trying to set up a reverse proxy in HestiaCP, users often encounter issues because:

  1. HestiaCP automatically generates and manages Nginx configurations
  2. Directly editing Nginx config files may lead to changes being overwritten
  3. The standard location for custom configurations isn’t immediately obvious

Solution: Using Custom Templates

HestiaCP provides a way to customize server configurations without modifying the auto-generated files. Here’s how to properly add a reverse proxy:

Step 1: Locate or Create the Custom Template Directory

  1. Connect to your server via SSH
  2. Navigate to the HestiaCP template directory:
   cd /usr/local/hestia/data/templates/web/nginx/

Step 2: Create a Custom Template

  1. Copy the default PHP-FPM template to use as a base:
   cp -r php-fpm/ php-fpm-proxy
  1. Edit the new template’s files. The main file you need to modify is:
   nano php-fpm-proxy/template.tpl

Step 3: Add Reverse Proxy Configuration

In the template.tpl file, add your reverse proxy configuration inside the server block. Here’s an example:

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;
}

Save the file after making your changes.

Step 4: Apply the New Template

  1. In HestiaCP’s web interface, go to the domain you want to configure
  2. Change the “Web Template” to your new “php-fpm-proxy” template
  3. Save the changes

Step 5: Restart Nginx

For the changes to take effect:

sudo systemctl restart nginx

Alternative Solution: Using a Custom Configuration File

If you prefer not to create a custom template, you can add your reverse proxy configuration to a custom include file:

  1. Create a new file in your domain’s conf directory:
   nano /home/user/conf/web/domain.com/nginx.custom.conf
  1. Add your reverse proxy configuration there
  2. HestiaCP will automatically include this file in the main Nginx configuration

Important Notes

  1. Always back up your configuration files before making changes
  2. Test your Nginx configuration before restarting:
   nginx -t
  1. If you encounter issues, check the Nginx error logs:
   tail -f /var/log/nginx/error.log

Conclusion

By following either the custom template method or the custom configuration file approach, you can successfully add a reverse proxy to your Nginx setup in HestiaCP without having your changes overwritten by the control panel’s automatic configuration management.

reference: Nginx add reverse proxy – Community Support / Install & Set-Up – Hestia Control Panel – Discourse

Tags:

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *

Latest Comments
No comments to show.