All Products
Search
Document Center

Object Storage Service:Configure the Java SDK to access OSS through an Nginx proxy

Last Updated:Nov 19, 2025

When you use the Java Software Development Kit (SDK) to make calls to OSS through an Nginx proxy, you must correctly configure the proxy parameters for the SDK. Also, ensure that the Nginx proxy does not automatically add or overwrite key request headers, such as Content-Type. Otherwise, the server returns a 403 error. This topic describes how to configure Nginx reverse proxy rules and initialize the Alibaba Cloud OSS Java SDK.

Configurations

Nginx proxy configuration

Create a separate .conf configuration file, such as oss-proxy.conf, in the /etc/nginx/conf.d/ folder. Then, edit the proxy rules and save the file.

server { 
    listen 80; 
    server_name <your_server_ip>; 
    client_max_body_size 100M; 
    location / { 
        # Replace bucketName with your actual bucket name.
        proxy_pass http://bucketName.oss-cn-hangzhou.aliyuncs.com; 
        proxy_set_header Host bucketName.oss-cn-hangzhou.aliyuncs.com; 
    } 
}

After you configure the file, run nginx -t to verify the syntax. Then, run the nginx -s reload command to reload the configuration.

Java SDK initialization

When you initialize the client, specify the proxy server address and port using the setProxyHost and setProxyPort methods.

String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// Obtain access credentials from environment variables. Before you run this code sample, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
//DefaultCredentialProvider credentialsProvider = CredentialsProviderFactory.newDefaultCredentialProvider("ak","sk");
// Specify the bucket name.
String bucketName = "bucketName";
// Specify the full path of the object. The full path cannot contain the bucket name.
String objectName = "test/1.txt";
String region = "cn-hangzhou";
// Create an OSSClient instance.
//OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
// Enable access to OSS using a second-level domain. This feature is disabled by default. You must set this value for OSS Java SDK 2.1.2 and earlier. For OSS Java SDK versions later than 2.1.2, the IP address is automatically detected and you do not need to set this value.
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
clientBuilderConfiguration.setProxyHost("<your_proxy_server_ip>");
clientBuilderConfiguration.setProxyPort(80);
OSS ossClient = OSSClientBuilder.create()
        .endpoint(endpoint)
        .credentialsProvider(credentialsProvider)
        .clientConfiguration(clientBuilderConfiguration)
        .region(region)
        .build();

Verify the result

After you complete the configuration, upload a file to OSS using the Java SDK. If the file is uploaded successfully, the configuration is correct.

References

For more information, see Access OSS through an ECS reverse proxy.