AskHandle

AskHandle Blog

How do I set up a proxy server to handle environment variables?

May 27, 2025Elise Taylor3 min read

How do I set up a proxy server to handle environment variables?

Many developers face the challenge of configuring proxy servers in a way that correctly manages environment variables. Proper setup is essential for controlling network traffic, enhancing security, and supporting different deployment environments like development, staging, and production. This article explains how to set up a proxy server that respects and manages environment variables effectively, with example code snippets to guide you through the process.

What is a proxy server?

A proxy server acts as an intermediary between your local machine and the internet. It forwards requests from your applications to external servers and vice versa. Proxy servers are useful for load balancing, caching, anonymizing traffic, and sometimes filtering or blocking content.

Importance of environment variables in proxy setup

Environment variables allow you to configure application behavior without changing code. For setting up a proxy server, environment variables like HTTP_PROXY, HTTPS_PROXY, and NO_PROXY are commonly used. They specify the proxy server address and define exceptions for certain local addresses or domains.

Basic steps to set up a proxy server with environment variables

Step 1: Define Environment Variables

Start by setting the environment variables that define your proxy configuration. Here is how you can do it in various operating systems:

On Linux or macOS:

bash
1export HTTP_PROXY="http://proxy.example.com:8080"
2export HTTPS_PROXY="https://proxy.example.com:8080"
3export NO_PROXY="localhost,127.0.0.1,.mycompany.internal"

On Windows CMD:

cmd
1set HTTP_PROXY=http://proxy.example.com:8080
2set HTTPS_PROXY=https://proxy.example.com:8080
3set NO_PROXY=localhost,127.0.0.1,.mycompany.internal

On Windows PowerShell:

powershell
1$env:HTTP_PROXY="http://proxy.example.com:8080"
2$env:HTTPS_PROXY="https://proxy.example.com:8080"
3$env:NO_PROXY="localhost,127.0.0.1,.mycompany.internal"

Step 2: Configure Application or Environment to Use Proxy

Most command-line tools and programming language frameworks automatically respect these environment variables. For example, in curl:

bash
1curl http://example.com

This will automatically route through the proxy if the environment variables are set.

In Node.js, popular HTTP libraries like axios or node-fetch also pick up these variables. Example:

javascript
1const axios = require('axios');
2
3axios.get('http://example.com')
4  .then(response => console.log(response.data))
5  .catch(error => console.error(error));

The above code will use the proxy environment variables if needed.

Step 3: Set Up a Local Proxy Server

Sometimes, you want to run your own proxy server locally, which manages environment variables dynamically. Software like Squid or TinyProxy can be installed for this purpose.

For instance, setting up TinyProxy:

bash
1sudo apt-get install tinyproxy

Configure /etc/tinyproxy/tinyproxy.conf to include your desired proxy settings. For example:

conf
1# Example configuration snippet
2Listen 8080
3Allow 127.0.0.1
4ConnectPort 80
5ConnectPort 443
6DefaultProxy proxy.example.com:8080

Restart TinyProxy:

bash
1sudo systemctl restart tinyproxy

Then, set environment variables to point to your local proxy:

bash
1export HTTP_PROXY="http://127.0.0.1:8080"
2export HTTPS_PROXY="http://127.0.0.1:8080"

This setup allows your applications to route traffic through your local proxy server which manages the actual forwarding, respecting environment variables.

Step 4: Dynamic Proxy Management

In complex environments, you might want to dynamically switch proxies based on environment, branch, or deployment stage. A good practice is to script environment variable setup.

For example, in a shell script:

bash
1#!/bin/bash
2
3if [ "$ENVIRONMENT" == "production" ]; then
4  export HTTP_PROXY="http://prod.proxy.com:8080"
5  export HTTPS_PROXY="http://prod.proxy.com:8080"
6elif [ "$ENVIRONMENT" == "staging" ]; then
7  export HTTP_PROXY="http://staging.proxy.com:8080"
8  export HTTPS_PROXY="http://staging.proxy.com:8080"
9else
10  unset HTTP_PROXY
11  unset HTTPS_PROXY
12fi

This script ensures the correct proxies are used depending on the environment.

Tips for managing proxy configuration with environment variables

  • Always include NO_PROXY for addresses that should bypass the proxy, such as local or internal services.
  • Remember that different programming environments might have their own proxy configuration methods aside from environment variables. For example, some libraries allow explicit proxy settings.
  • When testing, temporarily unsetting proxy variables with unset