All Products
Search
Document Center

:Use NGINX as a proxy for Apsara File Storage NAS

Last Updated:Apr 15, 2024

This topic describes how to use NGINX as a proxy for Apsara File Storage NAS (NAS).

Background information

NGINX is a light-weight high-performance web server. It provides various features and can be used as a reverse proxy. One of the most common application modes for NGINX is to serve as a reverse proxy. A proxy server receives connection requests from clients over the Internet. Then, the proxy server forwards these requests to a server that resides in a private network and returns responses from the server to these clients. In such cases, when a proxy server acts on behalf of the server, it is called a reverse proxy.

An application server that resides in a private network is inaccessible to clients outside the private network. In such cases, a reverse proxy is required to serve as an intermediary between the application server and clients. The reverse proxy resides in the same private network as the application server but is accessible to clients outside the private network. The reverse proxy and the application server can share the same physical server but use different ports.

The following example uses one NGINX server as a reverse proxy, four NGINX servers as proxy servers, and NAS as backend storage. NAS stores cache files of proxy servers, mirroring-based back-to-origin files, or static data files uploaded by users. NAS allows shared access to the same file system from different proxy servers. This way, data can be synchronized between proxy servers, ensuring data consistency. This also prevents servers from repeatedly retrieving files from the origin and guarantees efficient use of bandwidth. The following figure shows an example of network topology.

组网图

You can create an environment as shown in the preceding topology by following the instructions provided in this topic. This topic uses an Elastic Compute Service (ECS) instance on CentOS as an example.

Step 1: Deploy an NGINX reverse proxy

  1. Install NGINX.

    sudo yum install nginx
  2. Configure a reverse proxy that points to a proxy server.

    1. Run the following command to open the /etc/nginx/nginx.conf file:

      vim /etc/nginx/nginx.conf
    2. In the /etc/nginx/nginx.conf file, configure the following information:

      http {
      upstream web{
               server 10.10.0.10;
               server 10.10.0.11;
               server 10.10.0.12;
               server 10.10.0.13;
            }
            server {
                listen 80;
                    location / {
                         proxy_pass http://web;
                     }
            }
      }

Step 2: Create a file system and a mount target

  1. Create a Network File System (NFS) file system in a region. For more information, see Create a General-purpose NAS file system in the NAS console.

    Note

    The file system and the ECS instance on which the file system is mounted must reside in the same region.

  2. Create a mount target in a virtual private cloud (VPC). For more information, see Create a mount target.

Step 3: Deploy an NGINX proxy server

  1. Run the following command to install NGINX:

    sudo yum install nginx
  2. Run the following command to install an NFS client:

    sudo yum install nfs-utils
  3. Run the following command to mount the file system on a directory of the NGINX website:

    sudo mount -t nfs -o vers=4.0,file-system-id.region.nas.aliyuncs.com:/ /usr/share/nginx/html/ 

    In the preceding command, file-system-id.region.nas.aliyuncs.com:/ specifies the domain name of the mount target. Replace the domain name based on your business requirements.

  4. Edit the NGINX root file.

    echo "This is Testing for Nginx&NAS"> /usr/share/nginx/html/index.html
  5. Repeat the preceding steps to configure the other three NGINX proxy servers and mount the same NFS file system on each proxy server.

  6. Verify the configuration result.

    The proxy servers are configured successfully if each NGINX proxy server can access the index.html root file.

    验证结果