Example of using Nginx reverse proxy to go-fastdfs

Example of using Nginx reverse proxy to go-fastdfs

background

go-fastdfs is a distributed file system that supports the http protocol. In general projects, the file system address is rarely exposed directly. Most of them are reversed through software such as nginx. Due to the relatively special business and network environment scenarios of our company, the hybrid cloud network system consists of a public network part (public cloud) and an intranet part (private cloud). The public cloud mainly serves as an exit and entrance and runs some audit and authentication applications to process upstream requests, thereby reducing the number of private cloud processing times and improving performance. That is why, in a public network environment, a reverse proxy must be used to access the services provided by a private cloud. The same is true for access to the file system. How can we configure nginx so that external network requests can be reverse proxied to go-fastdfs? This article will explain step by step.

General Configuration

In general, friends who are familiar with nginx know that if you need to configure a reverse proxy, you can directly write a location context and a proxy module. If you need a custom prefix, you can use a rewrite module. A simple example is as follows:

location ~ /dfs/group([0-9]) {
 proxy_pass http://localhost:8080;
 rewrite ^/dfs/(.*)$ /$1 break;
 proxy_set_header Host $host:$server_port;
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

At this point, the general anti-generation configuration is OK, but is it OK for go-fastdfs? For go-fastdfs, general uploading is OK, but it is still not enough for breakpoint resumption using tus. Why? Because the tus server will return a 301 redirect and needs to carry certain request headers, special settings are required.

Support Tus anti-generation configuration

If you need to do TUS reverse generation, support 301 redirect Location rewriting and certain request header forwarding, how should you configure it? Please refer to the following configuration

location ~ /dfs1/group([0-9]) {
 access_log logs/dfs/access.log main;
 error_log logs/dfs/error.log error;
 rewrite ^/dfs1/(.*)$ /$1 break;
 proxy_pass http://localhost:8051;

 # Disable request and response buffering
 proxy_request_buffering off;
 proxy_buffering off;
 proxy_http_version 1.1;

 proxy_set_header Host $host:$server_port;
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 # If server_name is not a public domain name, this place can be set to ip
 proxy_set_header X-Forwarded-Host $hostname;
 proxy_set_header X-Forwarded-Proto $scheme;
 proxy_set_header Upgrade $http_upgrade;
 proxy_set_header Connection "upgrade";
 # Because prefix plus rewrite is used, the returned Location needs to be modified to add the reverse proxy prefix proxy_redirect ~^(.*)/group([0-9])/big/upload/(.*) /dfs/group$2/big/upload/$3;
 client_max_body_size 0;
}

Note that the two configurations above are proxy_redirect and client_max_body_size. The first configuration is because the redirect Location returned by the tus server does not carry a custom prefix, so you need to add a custom prefix yourself. Here I use /dfs. If it is something else, just change it. The second one is client_max_body_size. If this is set to 0, it means that no matter how large the uploaded file is, the request too large problem will not be reported and it will be forwarded directly. If it needs to be set, then please set a number greater than or equal to chunkSize. What is chunkSize? It refers to the size of each chunk when the tus client uploads in chunks. For details, please refer to the official documentation.

Load balancing configuration

When a cluster server is configured, how to load balance uploads or downloads? Use nginx to do reverse generation, which can be achieved with the upstream module. Please refer to the following configuration for details.

upstream dfs_stream {
 server host1:port;
 server host2:port;
 ip_hash;
}

The above configuration is no different from general load balancing. The only thing you need to pay attention to is to configure ip_hash. Why? Because when using breakpoint resume, the file is uploaded in blocks. If it is not ip_hash, the first few pieces may be uploaded to server A, and the last few pieces may be uploaded to server B. In this case, the file will not be complete, so you need to pay attention to this issue.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of two ways to implement session persistence in Nginx reverse proxy
  • Implementation of multi-port mapping of nginx reverse proxy
  • Nginx reverse proxy forwards port 80 requests to 8080
  • Nginx reverse proxy configuration removes prefix
  • Detailed explanation of Nginx reverse proxy example

<<:  MySQL 5.7.21 installation and configuration tutorial

>>:  A brief discussion on the types of node.js middleware

Recommend

Summary of solutions for MySQL not supporting group by

I downloaded and installed the latest version of ...

Detailed steps for adding hosts you need to monitor in zabbix

Add monitoring host Host 192.168.179.104 is added...

Summary of the differences between get and post requests in Vue

The operating environment of this tutorial: Windo...

Specific use of Linux which command

We often want to find a file in Linux, but we don...

The simplest form implementation of Flexbox layout

Flexible layout (Flexbox) is becoming increasingl...

CentOS7.5 installation tutorial of MySQL

1. First check whether the system has mysql insta...

HTML table tag tutorial (27): cell background image attribute BACKGROUND

We can set a background image for the cell, and w...

HTML+CSS to achieve responsive card hover effect

Table of contents accomplish: Summarize: Not much...

Things about installing Homebrew on Mac

Recently, Xiao Ming just bought a new Mac and wan...