Help Center> Object Storage Service> Best Practices> Accessing OBS Through an NGINX Reverse Proxy
Updated on 2024-03-25 GMT+08:00

Accessing OBS Through an NGINX Reverse Proxy

Application Scenario

Generally, you can access OBS using a bucket's access domain name (for example, https://bucketname.obs.ap-southeast-1.myhuaweicloud.com) provided by OBS or using a user-defined domain name bound to an OBS bucket.

In some cases, you may need to use a fixed IP address to access OBS. For security purposes, some enterprises need to set a blacklist and a whitelist of external IP addresses. In this case, a fixed IP address is required. Also for security purposes, an OBS bucket does not have a fixed IP address, because the DNS service of Huawei Cloud resolves the bucket access domain name to different IP addresses.

In this case, you can set up an NGINX reverse proxy server on an ECS so that you can access OBS through a fixed IP address.

Solution Architecture

This part explains how to deploy NGINX on an ECS and set up an NGINX reverse proxy server. The proxy is imperceptible. Requests are sent to the reverse proxy server, which then obtains the required data from OBS and returns the data to users. The reverse proxy server and OBS work as a whole. Only the IP address of the proxy server is exposed, while the actual domain name or IP address of OBS is hidden.

Figure 1 Principles of accessing OBS through an NGINX reverse proxy

Prerequisites

  • You have known the region and access domain name of the bucket. For example, the access domain name of a bucket in the CN-Hong Kong region is nginx-obs.obs.ap-southeast-1.myhuaweicloud.com. To obtain the information, see Querying Basic Information of a Bucket.
  • You have a Linux ECS in the same region. CentOS is used here as an example. For details, see Purchasing an ECS.
  • The ECS is bound with an EIP, so that you can download the NGINX installation package over the public network.

Procedure

  1. Install NGINX on an ECS.

    In this example, CentOS 7.6 is used as an example.
    1. Log in to the ECS where you will set up the NGINX reverse proxy server.
    2. Run the wget command to download the NGINX installation package for your operating system in use.
      wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
    3. Run the following command to create the NGINX yum repository:
      rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm
    4. Run the following command to install NGINX:
      yum -y install nginx
    5. Run the following commands to start NGINX and configure NGINX to start upon system startup:
      systemctl start nginx
      systemctl enable nginx
    6. Use a browser on any device to access http://ECS EIP. If the following information is displayed, NGINX is successfully installed.
      Figure 2 NGINX installed successfully

  2. Modify the NGINX configuration file to configure the reverse proxy for your OBS bucket.

    1. Run the following command to open the default.conf file:
      vim /etc/nginx/conf.d/default.conf
    2. Press the i key to go to the edit mode and modify the default.conf file.
      server {
            listen       80;
            server_name  **.**.**.**;  # Enter the EIP of the ECS.
            proxy_buffering off;  # Disable the proxy buffer (memory).
      
            location / {
                 proxy_pass  https://nginx-obs.obs.ap-southeast-1.myhuaweicloud.com;  #Enter the OBS bucket domain name that starts with http:// or https://.
                 index  index.html index.htm ;  #Specify the homepage of the website. If there are multiple files, Nginx checks the files based on their enumeration sequence.
              }
      }
      Table 1 Parameters in the configuration file

      Parameter

      Description

      server_name

      IP address that provides the reverse proxy service. It is the fixed IP address that is exposed to end users for access.

      Enter the EIP of the ECS where the NGINX reverse proxy service is deployed.

      proxy_pass

      IP address of the proxied server.

      Enter the OBS bucket access domain name required in Prerequisites. The domain name must start with http:// or https://. Example:

      https://nginx-obs.obs.ap-southeast-1.myhuaweicloud.com

      Note:

      When you use an API, SDK, or obsutil for calling, set this parameter to the region domain name. The following is an example:

      obs.ap-southeast-1.myhuaweicloud.com

      proxy_buffering

      Whether to enable the proxy buffer. The value can be on or off.

      If this parameter is set to on, Nginx stores the response returned by the backend in a buffer and then sends the data to the client.

      If this parameter is set to off, Nginx sends the response to the client as soon as it receives the data from the backend.

      Default value: on

      Example: proxy_buffering off

    3. Press the Esc key and enter :wq to save the configuration and exit.
    4. Run the following command to check the status of the NGINX configuration file:
      nginx -t
    5. Run the following command to restart the NGINX service for the configuration to take effect:
      systemctl stop nginx
      systemctl start nginx

  3. (Optional) Configure an OBS bucket policy to allow the IP address of the NGINX proxy server to access OBS.

    If your bucket is publicly read or the URL needs to have a signature contained when accessing objects in a private bucket, skip this step. For details, see Authentication of Signature in a URL.

    If you do not want URLs containing a signature to access resources in your private bucket, configure the following bucket policy that allows only the IP address of the NGINX proxy server to access your bucket.

    1. In the navigation pane of OBS Console, choose Object Storage.
    2. In the bucket list, click the bucket you want to go to the Objects page.
    3. In the navigation pane, choose Permissions > Bucket Policies.
    4. Click Create.
    5. Choose a policy configuration method you like. Visual Editor is used here.
    6. Configure the following parameters.
      Table 2 Bucket policy parameters

      Parameter

      Description

      Policy Name

      Enter a policy name.

      Policy content

      Effect

      Select Allow.

      Principals

      • Select All accounts.

      Resources

      • Method 1:
        • Select Entire bucket (including the objects in it).
      • Method 2:
        • Select Current bucket and Specified objects.
        • Set the resource path to * to indicate all objects in the bucket.

      Actions

      • Choose Customize.
      • Select Get* and List*.

      Conditions (Optional)

      • Key: Select SourceIp.
      • Condition Operator: Select IpAddress
      • Value:
        • If the ECS uses a public DNS, the value is as follows:

          Elastic IP address of the ECS

        • If the ECS uses a Huawei Cloud private DNS, the value is as follows:
          100.64.0.0/10,214.0.0.0/7,Private IP address of the ECS
          NOTE:

          You can click Add to configure three IP addresses (CIDR blocks).

          IP addresses in the range starting with 100 or 214 are for ECSs to access OBS through an internal network.

    7. Click Create.

  4. Verify the reverse proxy configuration.

    On any device, use the ECS EIP and object name to access specified OBS resources. If the resources are properly accessed, the configuration is successful.

    For example, visit http://ECS EIP/ocean.jpg.

    Figure 3 Using a fixed IP address to access OBS resources