All Products
Search
Document Center

ApsaraVideo VOD:Deploy a request proxy

Last Updated:Nov 17, 2025

In complex network environments, the default ApsaraVideo VOD domain name (*.aliyuncs.com) may be hijacked by carriers, which can cause access failures. You can deploy a dedicated proxy service to improve the request success rate and stability.

Before you begin

This topic uses Alibaba Cloud Function Compute (FC) 3.0 as an example. Before you begin, learn about Function Compute billing.

Steps

The deployment steps are as follows:

Note

If your FC console appears as shown in the following figure, click Experience Function Compute 3.0 in the upper-right corner to switch to the new console.

image

Step 1: Create a function

  1. Go to the Functions page in the FC console and click Create Function.image

  2. Select Task Function and click Create Task Function.

    • For Runtime Environment, select Built-in Runtimes > Python.

    • Keep the default values for other parameters or change them as needed.image

  3. Verify that the configurations are correct, and then click Create.

Step 2: Deploy the code

  1. On the Function Details page, click the Code tab.image

  2. Click the Open WebIDE button to open the code editor.image

  3. Copy the following code into the editor.

    Sample code

    import json
    import logging
    import http.client
    from urllib.parse import urlencode
    
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger()
    
    def get_target_domain(region_id):
        # If region_id is not specified, use the default value.
        if not region_id:
            return "vod.cn-shanghai.aliyuncs.com"
        return f"vod.{region_id}.aliyuncs.com"
    
    def handler(event, context):
        try:
            # If the event is in bytes, decode it first.
            if isinstance(event, bytes):
                event = json.loads(event.decode('utf-8'))
    
            # Get queryParameters.
            query_params = event['queryParameters']
            logger.info(f"queryParameters: {query_params}")
    
            # Get and remove the RegionId parameter.
            region_id = query_params.pop('RegionId', None)
            target_domain = get_target_domain(region_id)
    
            # Build the query string.
            query_string = urlencode(query_params)
            path = f"/?{query_string}"
    
            logger.info(f"Forwarding to: {target_domain}{path}")
    
            # Forward the request.
            conn = http.client.HTTPSConnection(target_domain)
            conn.request('GET', path)
    
            # Get the response.
            response = conn.getresponse()
            body = response.read()
    
            # Process the response header to ensure there is only one Access-Control-Allow-Origin.
            headers = dict(response.getheaders())
            # Set a single CORS header.
            headers['Access-Control-Allow-Origin'] = '*'
    
            return {
                'statusCode': response.status,
                'headers': headers,
                'body': body.decode('utf-8')
            }
    
        except Exception as e:
            logger.error(f"Error: {str(e)}")
            return {
                'statusCode': 500,
                'body': str(e)
            }
        finally:
            if 'conn' in locals():
                conn.close()
  4. Return to the Function Details page and click Deploy Code.

Step 3: Create a trigger

  1. On the Function Details page, click the Triggers tab.image

  2. Click Create Trigger and select HTTP Trigger. Set Authentication Method to No Authentication. Keep the default values for other parameters or change them as needed.image

  3. Verify that the configuration is correct, and then click OK.

Step 4: Verify the deployment

  1. In your player, set the customVodServer property to the generated domain name (public access address). Then, test the connection by accessing the proxy service.image

  2. A successful connection indicates that the request proxy is deployed successfully.

References