Modify outgoing request headers

更新时间:
复制 MD 格式

By default, Alibaba Cloud CDN and support forwarding request headers, such as those that contain the client IP address. You can also configure custom headers. To rewrite HTTP headers in origin requests, you can modify outgoing request headers. This feature allows you to add, delete, change, or replace origin HTTP request headers to meet your business requirements.

Background information

An HTTP request header is part of an HTTP request message and is used to pass specific parameter information to a server.

When a CDN or point of presence (POP) sends a request to an origin server to retrieve resources, the origin server can access the information in the origin request header. You can use this feature to rewrite HTTP header information in origin requests and send specific parameters to your origin server.

To learn how an origin server retrieves the real client IP address from the X-Forwarded-For header in an origin request, see Obtain the real IP address of a client.

image

Usage notes

  • An outgoing request is an HTTP message that is sent from CDN to an origin server. Modifying the outgoing request header configuration affects only HTTP messages that are sent from CDN to an origin server. This configuration does not affect HTTP messages that are served directly from CDN POPs to users.

  • You cannot modify outgoing request headers for a wildcard domain name.

  • If this feature is configured with conditions from the rules engine, the execution order is determined by the priority of the rule conditions, not the configuration order of the feature.

  • By default, Alibaba Cloud CDN and include the following HTTP request headers in origin requests. No additional configuration is required.

    Origin HTTP header

    Description

    Example

    Ali-Cdn-Real-Ip

    The real IP address of the client connecting to a CDN or POP.

    Ali-Cdn-Real-Ip:192.168.0.1

    X-Forwarded-For

    The IP addresses of the client and all intermediate CDN or POPs in the path of the origin request.

    X-Forwarded-For:192.168.0.1, 172.16.0.1

    X-Client-Scheme

    The application-layer protocol, such as HTTP or HTTPS, that a client uses to send a request to a CDN or POP.

    X-Client-Scheme:http

    Host

    The domain name of the origin website that is accessed by the origin request.

    Host:example.com

    Via

    The names of all CDN or POPs that the client request passes through.

    Via:cn2546-10.l1, cache1.cn2546-10, l2cn2547-7.l2, cache1.l2cn2547-7

  • If you set the value of an outgoing request header to a variable, the variable is resolved to its actual value at runtime. The following variables are available.

    Parameter

    Origin HTTP header

    Description

    Example

    Ali-Cdn-Real-Port

    $http_Ali_Cdn_Real_Port

    Adds the client's real port to the origin request header to pass the port information to the origin server.

    Ali-Cdn-Real-Port:80

    Ali-Cdn-Real-Ip

    $http_Ali_Cdn_Real_Ip

    Adds the real IP address of the client to the origin request header to pass the client IP address information to the origin server.

    Ali-Cdn-Real-Ip:192.168.0.1

    x_forwarded_for

    $proxy_add_x_forwarded_for

    Appends the client IP address and the IP addresses of intermediate proxy servers to the X-Forwarded-For header to pass them to the origin server.

    X-Forwarded-For:192.168.0.1, 172.16.0.1

FAQ

Missing required HTTP header fields

As an intermediate proxy, CDN cannot directly insert missing HTTP response headers, such as Cache-Control, into responses to fix security vulnerabilities. CDN can only modify origin request headers.

We recommend that you configure custom origin request headers in the CDN console to pass the required parameters to your origin server, such as a CLB instance. If your origin server does not support this or cannot be modified, CDN does not provide another way to directly add response headers.

Note

To add HTTP headers to the responses that CDN sends to clients, see Modify outgoing response headers.

Custom HTTP headers and access speed

No. Configuring custom origin request headers in CDN does not affect access speed.

Procedure

  1. Log on to the CDN console.

  2. In the left navigation pane, click Domain Names.

  3. On the Domain Names page, find the target domain name and click Manage in the Actions column.

  4. In the domain's navigation pane, click Origin Fetch.

  5. Click the Modify Outgoing Request Header tab.

  6. Click Customize.

  7. Configure the outgoing request header.

    Important

    If you configure different operations for the same origin request header, a conflict occurs, and the highest-priority operation is executed. The priority order is: Replace > Add > Change and Delete.

    Add header

    Parameter

    Example

    Description

    Operation

    Add

    Adds the specified request header to origin requests.

    Request Header

    Custom Origin Request Header

    Select Custom Back-to-origin Request Headers or a preset request header.

    Header Name

    x-code

    Specifies the custom request header name as x-code.

    Header Value

    key1, key2

    You can configure multiple values for a request header. Separate multiple values with a comma (,).

    Allow Duplicates

    Yes

    • Yes: Allows duplicate request headers to be added. For example, x-code:key1 and x-code:key2.

    • No: If you add a header with the same name, the new value overwrites the existing one. For example, if you first add x-code:key1 and then add x-code:key2, the final value is x-code:key2.

    Rule Condition

    Do not use

    Rule conditions identify various parameters in user requests to determine whether a configuration applies to a specific request.

    Important

    When you use a rule condition, matching is based on the priority of the associated rule condition, not the configuration order of the feature itself.

    • Do not use: No rule condition is used.

    • To add or edit a rule condition, go to the rules engine.

    Delete header

    Parameter

    Example

    Description

    Operation

    Delete

    Deletes all headers that match the specified name, including duplicates.

    Request Header

    Custom Origin Request Header

    Select Custom Back-to-origin Request Headers or a preset request header.

    Header Name

    x-code

    Specifies the custom request header name as x-code.

    Rule Condition

    Do not use

    A rule condition allows a rule to be applied only when a request meets specific criteria.

    • Do not use: Disables conditional rules.

    • You can add or edit conditional rules in the Rules engine.

    Change header

    Parameter

    Example

    Description

    Operation

    Change

    Changes a header's value. This operation is not allowed if duplicate headers with the same name exist.

    Request Header

    Custom Origin Request Header

    Select Custom Back-to-origin Request Headers or a preset request header.

    Header Name

    x-code

    Specifies the custom request header name as x-code.

    Change Value To

    key1, key3

    You can configure multiple values for a request header. Separate multiple values with a comma (,).

    Rule Condition

    Do not use

    A rule condition allows a rule to be applied only when a request meets specific criteria.

    • Do not use: Disables conditional rules.

    • You can add or edit conditional rules in the Rules engine.

    Replace header

    Parameter

    Example

    Description

    Operation

    Replace

    Replaces a header's value. This operation is not allowed if duplicate headers with the same name exist.

    Request Header

    Custom Origin Request Header

    Select Custom Back-to-origin Request Headers or a preset request header.

    Header Name

    x-code

    Specifies the custom request header name as x-code.

    Find

    key

    A regular expression to find the value to replace.

    Replace With

    abc

    The replacement value.

    Match

    Match All

    • Match All: Replaces all matched values. For example, for the header x-code:key1,key2,key3, if you use a regular expression to match "key" and replace it with "abc", the result is x-code:abc1,abc2,abc3.

    • Match the First Only: Replaces only the first matched value. For example, for the header x-code:key1,key2,key3, if you use a regular expression to match "key" and replace it with "abc", the result is x-code:abc1,key2,key3.

    Rule Condition

    Do not use

    A rule condition allows a rule to be applied only when a request meets specific criteria.

    • Do not use: Disables conditional rules.

    • You can add or edit conditional rules in the Rules engine.

  8. Click OK.