Setting Up Nginx as a Reverse Proxy for a Go Web Server

2 min read .

In this tutorial, we’ll configure Nginx to act as a reverse proxy for a Go web server. This setup allows Nginx to handle incoming HTTP requests and forward them to the Go application running on a different port (8080 in this case). The advantage of using Nginx as a reverse proxy includes improved security, load balancing, and the ability to serve static content efficiently.

Step 1: Install Nginx

If Nginx is not already installed on your server, you can install it using the following commands. The exact command may vary depending on your Linux distribution:

For Ubuntu/Debian:

sudo apt update
sudo apt install nginx

Step 2: Configure Nginx as a Reverse Proxy

Once Nginx is installed, you need to configure it to forward requests to your Go application. This is done by creating or modifying a server block in the Nginx configuration file.

Open the default configuration file or create a new one:

sudo nano /etc/nginx/sites-available/myapp

Add the following configuration to the file:

server {
    listen 80;
    server_name yourdomain.com;  # Replace with your domain or IP address

    location / {
        proxy_pass http://localhost:8080;
        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 configuration tells Nginx to listen on port 80 (the default HTTP port) and forward all requests to the Go application running on localhost:8080.

Step 3: Enable the Nginx Configuration

If you created a new configuration file, you’ll need to enable it by creating a symbolic link to the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/

Step 4: Test the Nginx Configuration

Before reloading Nginx, it’s a good idea to test the configuration to ensure there are no syntax errors:

sudo nginx -t

If the test is successful, you should see a message indicating that the configuration is okay.

Step 5: Reload Nginx

After confirming that the configuration is correct, reload Nginx to apply the changes:

sudo systemctl reload nginx

Step 6: Access Your Application

With Nginx configured as a reverse proxy, you can now access your Go web server by navigating to your domain (e.g., http://yourdomain.com). Nginx will forward the request to the Go application running on port 8080, and you should see the JSON response as expected.

Optional: Enable HTTPS with Let’s Encrypt

To secure your application with HTTPS, you can use Let’s Encrypt to obtain a free SSL certificate. Install Certbot and run the following command:

sudo certbot --nginx -d yourdomain.com

Follow the prompts to configure SSL automatically. After completion, Nginx will be set up to serve your application over HTTPS.

Conclusion

Setting up Nginx as a reverse proxy for your Go web server provides a robust and flexible way to manage incoming HTTP requests. This configuration allows you to take advantage of Nginx’s features, such as SSL termination, caching, and load balancing, while still using your Go application to handle the backend logic.

See Also

chevron-up