[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-04。"],[],[],null,["# Cloud NAT rules\n===============\n\nThe NAT rules feature lets you create access rules that define how\nCloud NAT is used to connect to the internet. NAT rules support source\nNAT based on destination address.\n\nWhen you configure a NAT gateway without NAT rules, the VMs using that NAT gateway\nuse the same set of NAT IP addresses to reach all internet addresses. If you\nneed more control over packets that pass through Cloud NAT, you can\nadd NAT rules. A NAT rule defines a match condition and a corresponding action.\nAfter you specify NAT rules, each packet is matched with each NAT rule. If a\npacket matches the condition set in a rule, then the action corresponding to\nthat match occurs.\n| **Note:** The information on this page applies to Public NAT. For information about Private NAT, see [Private NAT](/nat/docs/private-nat).\n\nCloud NAT rule configuration example\n------------------------------------\n\nThe following example illustrates how to use NAT rules when your destination\nallows access from only a few IP addresses. We recommend that the traffic to\nsuch destinations from your Google Cloud VMs in private subnets are\nsource NAT-translated with only the permitted IP addresses. We recommend that\nyou do not use these IP addresses for other destinations.\n\nConsider the following requirements for VMs in `Subnet-1` (`10.10.10.0/24`),\nwhich is in Region A of the VPC network `test`:\n\n- The VMs must use NAT IP address `203.0.113.20` to send traffic to destination `198.51.100.20/30`.\n- The VMs must use NAT IP address `203.0.113.30` to send traffic to destination `198.51.100.30` or `198.51.100.31`.\n- The VMs must use NAT IP address `203.0.113.40` to send traffic to any other internet destination.\n\nThis VPC network also contains two additional subnets in the\nsame region. These VMs must use NAT IP address `203.0.113.10` to send traffic\nto any destination.\n[](/static/nat/images/nat-rules-example.svg) Cloud NAT configuration with two Cloud NAT gateways (click to enlarge).\n\nYou can use NAT rules for this example, but you need two NAT gateways because\n`Subnet-1` (`10.10.10.0/24`) has NAT rules that are different from the other\nsubnets. To create this configuration, follow these steps:\n\n1. Create a gateway called `Cloud NAT Gateway 1` for `Subnet-1` with NAT IP address `203.0.113.40` and add the following rules:\n 1. NAT rule 1 in `Cloud NAT Gateway 1`: When the destination is `198.51.100.20/30`, use source NAT with `203.0.113.20`.\n 2. NAT rule 2 in `Cloud NAT Gateway 1`: When the destination is `198.51.100.30` or `198.51.100.31`, use source NAT with `203.0.113.30`.\n2. Create a gateway called `Cloud NAT Gateway 2` for the region's other subnets and assign the NAT IP address as `203.0.113.10`. No NAT rules are needed in this step.\n\nNAT rules specifications\n------------------------\n\nBefore working with NAT rules, note the following specifications:\n\n- A rule number uniquely identifies a NAT rule. No two rules can have the same rule number.\n- Each NAT configuration has a default rule:\n - The default rule is applied if no other NAT rule matches in the same NAT configuration.\n - The rule number of the default rule is `65001`.\n - The destination IP CIDR range of the default rule is `0.0.0.0/0`.\n- Cloud NAT rules are supported only when the value of the NAT IP allocate option is `MANUAL_ONLY`.\n- All IP addresses configured in a given rule must be of the same tier.\n\n You cannot use a mix of Premium Tier and Standard Tier IP addresses within\n the same rule (including the default rule).\n- Destination IP CIDR ranges in the match condition must not overlap across NAT\n rules. There can be at most one rule that can match any given packet.\n\n You cannot create a NAT rule with a destination IP CIDR range of `0.0.0.0/0`,\n because that range is used in the default rule.\n- NAT IP addresses across NAT rules must not overlap.\n\n- A rule must either have a non-empty `Active` or non-empty\n [`Drain`](/nat/docs/ports-and-addresses#draining-nat-ips) IP address.\n If the rule has an empty `Active` IP address, new connections that\n match the NAT rule are dropped.\n\n- NAT rules cannot be added to a NAT gateway that has\n [Endpoint-Independent Mapping](/nat/docs/overview#specs-rfcs) enabled. You\n cannot enable Endpoint-Independent Mapping on a NAT gateway that\n has NAT rules in it.\n\nIn addition, all VMs get ports assigned to them from the value for minimum ports\nper VM for each Cloud NAT rule. If the ports allocated to a VM from a\nNAT rule are exhausted, new connections that match the NAT rule are dropped.\n\nFor example, if you configure 4,096 ports per VM and have 16 VMs and 2 NAT rules\n(`rule1` with 1 IP address and `rule2` with 2 IP addresses), alongside the default rule\n(`default`) with 2 IP addresses, all 16 VMs would get 4,096 ports in each bundle\nof NAT rules. In this example, there are no issues in `default` or `rule2` for\nall their VMs, but `rule1` isn't able to allocate ports for all its VMs.\nTherefore, traffic from VMs that needs to go through `rule1` might be dropped\nand show signs of being out of resources because the traffic does not use the\ndefault rule.\n\nRule expression language\n------------------------\n\nNAT rules are written using\n[Common Expression Language](https://github.com/google/cel-spec)\nsyntax.\n\nAn expression requires two components:\n\n- *Attributes* that can be inspected in rule expressions.\n- *Operations* that can be performed on the attributes as part of an expression.\n\nFor example, the following expression uses the attributes `destination.ip` and\n`198.51.100.0/24` in the operation `inIpRange()`. In this case, the expression\nreturns true if `destination.ip` is within the `198.51.100.0/24` IP address range. \n\n```\ninIpRange(destination.ip, '198.51.100.0/24')\n```\n\nNAT rules support only the following attributes and operations:\n\n### Attributes\n\nAttributes represent information from an outgoing packet, such as the\ndestination IP address.\n\n### Operations\n\nThe following reference describes the operators that you can use with attributes\nto define rule expressions.\n\n### Example expressions\n\nMatch traffic with destination IP address `198.51.100.20`. \n\n```\n\"destination.ip == '198.51.100.20'\"\n```\n\nMatch traffic with destination IP address `198.51.100.10/30` or `198.51.100.20`. \n\n```\n\"inIpRange(destination.ip, '198.51.100.10/30') || destination.ip == '198.51.100.20'\"\n```\n\nWhat's next\n-----------\n\n- Learn to [configure NAT rules](/nat/docs/using-nat-rules)."]]