Use EPG to quickly set up a public network egress proxy
Set up a public network egress proxy for Elastic Compute Service (ECS) instances in a Virtual Private Cloud (VPC). The ECS instance needs no public IP address, because outbound traffic egresses through the fixed EIPs of Egress Proxy Gateway (EPG). You can apply fine-grained control by destination domain name or source IP address, and you no longer need to build or maintain your own proxy cluster.
Solution architecture
ECS instances in the same VPC access the Internet through the EPG proxy. Outbound traffic egresses through the EIPs in the egress address pool. The ECS instance uses the DNS domain name assigned by EPG as the proxy address and connects through environment variable configuration.
Prerequisites
EPG is activated.
A VPC is created in the China (Shanghai) region, and vSwitches are created in Zone L and Zone M respectively.
An ECS instance is created in the preceding VPC, and you have the SSH logon permissions for the ECS instance.
Procedure
1. Create an egress proxy configuration
When you create an EPG instance, you must associate an egress proxy configuration with it. Create the configuration first. Make sure that the egress proxy configuration and the EPG instance to be created are in the same region. Otherwise, they cannot be associated.
Log on to the EPG console and select the China (Shanghai) region. In the left-side navigation pane, choose Configuration Management > Egress Proxy Configuration, and then click Create Egress Proxy Configuration.
Enter an Egress Proxy Configuration Name, retain the default settings for Default Rule Phase Action (PreDns phase, PreRequest phase, and Post-Response Phase are all set to Allow), and then click Create.
2. Create an EPG instance and configure listener ports
In the left-side navigation pane, choose Instance Management > Instance List, and then click Create Instance.
On the Create Instance page, complete the following configurations and click Confirm Creation.
Instance Name: Required. Enter an easily identifiable instance name.
Region and Virtual Private Cloud: In this example, select the China (Shanghai) region and the VPC where the ECS instance resides.
Create Default Address Pool: Do not select this option in this example. The egress address pool is created in Step 3.
Availability Zone/VSwitch: In this example, select Shanghai Zone L and Shanghai Zone M, and select the corresponding vSwitch for each zone. To ensure high availability, we recommend that you select at least two zones.
Associated Configuration: Select the egress proxy configuration that you created in Step 1.
Listening: Two listeners,
HTTP:1080andHTTPS:443, are included by default.Listener ports cannot be modified after the EPG instance is created. Confirm the settings before you submit them.
In the Basic Information section of the instance details page, copy the DNS domain name of the instance (in the format of
epg-xxx.cn-shanghai.epg.aliyuncs.com). This value will be used for proxy access configuration later.
When you create an EPG instance, you must configure both the HTTP and HTTPS listener ports. However, in this example, all clients access EPG through HTTP:1080.
Protocol | Port | Purpose | Required |
HTTP | 1080 (a commonly used port for proxy services) | The proxy access entry point for clients. It supports both HTTP and HTTPS destinations. HTTP requests are forwarded directly, and HTTPS requests use a CONNECT tunnel. | Yes |
HTTPS | 443 | TLS client access, which is used to encrypt the proxy channel between the client and EPG. | Required during creation, but not used in this example. |
3. Create an egress address pool
You must configure at least one egress address pool and associate EIPs with it before EPG can access the Internet.
On the instance details page, click the Egress address pool tab, and then click Create an egress address pool.
On the Create an egress address pool page, complete the following configurations and click Confirm Creation.
Name: Required. Enter an easily identifiable address pool name.
Network type: Select Public network.
Scheduling policy: Retain the default value Random.
Set as default address pool: Keep this option selected by default.
Elastic IP Address: In the Zone Mapping section, select the EIP to associate for Shanghai Zone L and Shanghai Zone M.
If no EIPs are available, you can purchase new ones. EIPs are billed separately.
4. Configure the proxy on the ECS instance and verify
This example uses Alibaba Cloud Linux 4 (with bash as the default shell).
Log on to the ECS instance over SSH and run the following commands to configure the proxy environment variables (replace
<your-EPG-domain-name>with the DNS domain name of the instance).sudo tee /etc/profile.d/epg-proxy.sh > /dev/null <<'EOF' # Configure the proxy used for HTTP requests export http_proxy="http://<your-EPG-domain-name>:1080" export HTTP_PROXY="$http_proxy" # Configure the proxy used for HTTPS requests export https_proxy="http://<your-EPG-domain-name>:1080" export HTTPS_PROXY="$https_proxy" #Configure Alibaba Cloud internal services and private network addresses to bypass the proxy export no_proxy="localhost,127.0.0.1,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,100.64.0.0/10,100.100.100.200,aliyuncs.com" export NO_PROXY="$no_proxy" EOF source /etc/profile.d/epg-proxy.shThe standard HTTP proxy protocol uses a single plaintext port for both HTTP and HTTPS destinations: HTTP requests are forwarded directly, and HTTPS requests establish a tunnel by using the CONNECT method. Therefore,
https_proxyis set to the plaintext HTTP proxy port 1080.Verify connectivity.
#Verify public HTTP access (success if HTTP status code 301 is returned) curl -I --max-time 15 http://help.aliyun.com #Verify public HTTPS access (success if HTTP status code 302 is returned) curl -I --max-time 15 https://help.aliyun.com #Verify that no_proxy works: Alibaba Cloud internal services are accessed directly without the proxy (success if an XML error response is returned) curl --max-time 15 https://ecs.cn-shanghai.aliyuncs.com #Verify direct access to the metadata service (success if the instance ID is returned) curl --max-time 5 http://100.100.100.200/latest/meta-data/instance-id(Optional) Remove the proxy configuration.
Temporarily disable (current session only):
unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY no_proxy NO_PROXYCompletely remove:
Delete the startup script.
sudo rm /etc/profile.d/epg-proxy.shDisable the proxy for the current session.
unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY no_proxy NO_PROXYNew terminal sessions no longer load the proxy configuration.
(Optional) Access control rules
EPG supports a three-phase rule engine (PreDns, PreRequest, and PostResponse) for fine-grained control of source IP addresses, domain names, URLs, and headers.
In the left-side navigation pane, choose Rule Group Management > Rule Group List, and then click Create a rule group. Complete the following configurations and click Create.
Rule Group Name: The rule group name is required.
Associated outbound address pool: Do not configure this parameter in this example. When it is left empty, the rule group uses the default address pool of the instance to access the Internet, as described in Rule engine and rule configuration.
List of rules: Add rules to each phase as needed. Set Rule Name, Priority (1 to 1000; a smaller value indicates a higher priority), Matching Condition, and Action (Allow or Deny).
ImportantAdding rules to PostResponse phase is not supported, and the action is fixed to Allow. When TLS interception is not enabled for the instance, an HTTPS request uses the CONNECT method to establish an end-to-end encrypted tunnel with the destination server through EPG. EPG only forwards the traffic and cannot parse the request content inside the tunnel. The HTTPS request does not enter the
PreRequestphase, so neither the rules nor the default action of that phase take effect, and the request is forwarded directly through the default address pool. To control HTTPS traffic, enable TLS interception. To control traffic only by source IP address or destination domain name, configure rules in thePreDnsphase.
In the left-side navigation pane, choose Configuration Management > Egress Proxy Configuration. Find the egress proxy configuration created in Step 1 and click the configuration ID to go to the details page.
On the Rule Group tab, click Bind Rule Group, select a rule group, set its priority, and then click Confirm. The rule group takes effect after its status changes to Available.
FAQ
curl command fails or uses the wrong port
When verifying the proxy, we recommend that you run curl -I --max-time 15 http://help.aliyun.com directly so that the environment variables automatically specify the proxy address. Avoid manually specifying the proxy by using -x, which may cause connection failures due to incorrect port or format. Make sure that both http_proxy and https_proxy point to the plaintext proxy port 1080.
Request timeout or 503 upstream connect error
The ECS instance cannot access EPG. Make sure that the EPG listener is in a normal state and that the access path from the ECS instance to EPG is not blocked by any rule.
The EIP is in an abnormal state (for example, overdue or released). Check the EIP status in the EIP console.
Outbound traffic from the EIP is blocked by Cloud Firewall. Check whether the Internet Firewall policy in Cloud Firewall allows outbound traffic from the EIP.
Proxy CONNECT aborted error
This is usually caused by an incorrect https_proxy configuration. For example, if https_proxy is set to http://<EPG-domain-name>:443, the client uses a plaintext protocol to connect to the TLS port, causing a protocol mismatch and connection abort. Set https_proxy to the plaintext proxy port, such as http://<EPG-domain-name>:1080.