Case explanation of Nginx reverse proxy to go fastdfs

  • 2021-11-02 03:31:08
  • OfStack


go-fastdfs is a distributed file system supporting http protocol, In a general project, It is rare to expose the address of the file system directly. Most of them will reverse the past through software such as nginx, Due to the relatively special business and network environment scenarios of our company, the hybrid cloud network system composed of public network part (public cloud) and internal network part (private cloud), the public cloud mainly acts as an exit and entrance and runs some audit and authentication applications to process upstream requests, thus reducing the processing times of private cloud and improving performance. Then it is precisely because of this, in the public network environment, to access the services provided by the private cloud, we must use reverse proxy. Similarly, for file system access, how can you configure it in nginx so that external network requests can reverse proxy to go-fastdfs? This article will elaborate step by step.

1 general configuration

Under the same circumstances, friends who are familiar with nginx know that if you need to configure reverse proxy, you can write an location context and an proxy module directly, and if you need to customize prefixes, you can use an 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 1-like anti-generation configuration is OK, so is it OK for go-fastdfs? Upload like go-fastdfs1 is OK, but it is not enough to use tus for breakpoint continuation. Why? Because the tus server will return 301 redirection and need to carry a request header of 1, it also needs a special setting of 1.

Support the reverse generation configuration of Tus

If you need to do the reverse generation of tus, support the rewriting of 301 redirection Location and support the request header forwarding of 1, how to configure it? Please refer to the following configuration


ocation ~ /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 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 of the prefix plus rewrite So you want to modify the returned Location Prefix reverse proxy 
	proxy_redirect ~^(.*)/group([0-9])/big/upload/(.*) /dfs/group$2/big/upload/$3;
	client_max_body_size 0;

Attention should be paid to the two configurations of proxy_redirect and client_max_body_size. The first configuration is because the redirected Location returned by tus server will not carry a custom prefix, so it is necessary to add a custom prefix by itself. I is/dfs here. If it is something else, just replace it. The second one is client_max_body_size, which is set to 0, which means that no matter how big the file is uploaded, it will not report the problem of request too large, and it will be forwarded directly. If it needs to be set, please set a number greater than or equal to chunkSize. What is chunkSize? That is, when tus client uploads in blocks, the size of each block, please refer to the official document for details.

Load balancing configuration

How can upload or download load balance when a cluster server is configured? Using nginx to do anti-generation, with upstream module can be achieved, specific reference to the following configuration

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

There is nothing different between the above configuration and 1-like load balancing. The only thing to pay attention to is to configure ip_hash. Why? Because when using breakpoint to continue transmission, the file is uploaded in blocks. If it is not ip_hash, it is possible that the first few pieces will be uploaded to A server and the last few pieces will be uploaded to B server, so the file is not complete, so attention should be paid to this problem.

Related articles: