Help Center/ NAT Gateway/ Best Practices/ Using a Public NAT Gateway to Enable Servers to Share an EIP to Access the Internet
Updated on 2026-07-30 GMT+08:00

Using a Public NAT Gateway to Enable Servers to Share an EIP to Access the Internet

Scenarios

When there is a large number of ECSs running in a VPC, assigning an EIP to each of them reduces management efficiency and raises exposure to potential scans and attacks.

To address these issues, you can use a public NAT gateway to centrally manage the Internet ingress and egress:

  • Add an SNAT rule and configure an EIP to allow all ECSs in a VPC to share this EIP to access the Internet, and keep all resources in the VPC running properly.
  • Unbind the EIP from each ECS and add a DNAT rule to minimize port exposure risks and keep access mode unchanged.

Solution Architecture

In enterprise applications, backend services are usually centrally managed to improve service availability and management efficiency. However, directly exposing backend services to the Internet brings security risks. If your servers need to access the Internet, use the following solution to centrally manage the Internet ingress and egress:

  1. Use a load balancer as the unified ingress to distribute external requests across backend servers.
  2. Use a public NAT gateway as the unified egress, as shown in Figure 1. This solution helps you centrally manage backend services, reduce security risks, and improve O&M efficiency.
Figure 1 Unified egress

Resource Planning

In this practice, you need to create a VPC, three ECSs, a public NAT gateway, and a load balancer. For details about the resource planning, see Table 1.

Table 1 Resource planning

Resource

Quantity

Description

VPC and subnet

One VPC with a subnet

  • VPC-A, with the CIDR block of 192.168.0.0/16
  • subnet-A01, with the CIDR block of 192.168.0.0/24

Public NAT gateway

1

In this practice, the NAT gateway is named nat_public. You can add an SNAT rule to this NAT gateway to allow ECSs to share an EIP to access the Internet.

Load balancer

1

This load balancer distributes external traffic across ECSs.

ECS

3

ECS-A, ECS-B, and ECS-C are deployed in subnet-A01.

EIP

3

  • EIP-ELB: You can bind this EIP to the load balancer to distribute client requests from the Internet across ECSs.
  • EIP-SNAT: You can select this EIP for an SNAT rule to allow ECSs to share this EIP to access the Internet clients.
  • EIP-DNAT: You can select this EIP for a DNAT rule to allow Internet clients to access the ECSs through this EIP.

Preparations

Step 1: Create a Public NAT Gateway

  1. Go to the Buy Public NAT Gateway page.
  2. Configure the required parameters. For details, see Buying a Public NAT Gateway.
    Figure 2 Buying a public NAT gateway

Step 2: Add an SNAT Rule

ECSs in subnet-A01 can share the EIP configured for the SNAT rule to access the Internet.

  1. Go to the public NAT gateway list page.
  2. On the Public NAT Gateways page, click the public NAT gateway purchased in Step 1.

    The public NAT gateway details page is displayed.

  3. On the SNAT Rules tab, click Add SNAT Rule.
  4. Add an SNAT rule as prompted. Set Public IP Address Type to EIP and select EIP-SNAT.
    Figure 3 Adding an SNAT rule
  5. Click OK.

Step 3: Add a DNAT Rule

ECSs in subnet-A01 can share the EIP configured for the DNAT rule to provide services accessible from the Internet.

  1. Go to the public NAT gateway list page.
  2. On the Public NAT Gateways page, click the public NAT gateway purchased in Step 1.

    The public NAT gateway details page is displayed.

  3. On the DNAT Rules tab, click Add DNAT Rule.
  4. Add a DNAT rule as prompted.
    Set Port Type to All ports, Public IP Address Type to EIP, and select EIP-DNAT.
    Figure 4 Adding a DNAT rule
  5. Click OK.

Step 4: Verify Network Connectivity

  1. Remotely log in to the ECS where the backend service is deployed.

    Multiple methods are available for logging in to an ECS. For details, see Logging In to an ECS.

  2. Ping the three EIPs on the service ECS to verify that the ECSs can be accessed by the Internet clients.
    Figure 5 Successful Internet access to the server
  3. Run the following command on each of the three ECSs to obtain the IP address used by the ECSs to access the Internet:

    curl myip.ipip.net

    As shown in Figure 6, EIP-SNAT is returned for each of the ECS.

    Figure 6 Obtaining the IP address used by each server to access the Internet