Nginx reverse proxy to go-fastdfs case explanation

Nginx reverse proxy to go-fastdfs case explanation

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

l

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 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 prefix of the reverse proxy 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.

This is the end of this article about the case study of Nginx reverse proxy to go-fastdfs. For more relevant content about Nginx reverse proxy to go-fastdfs, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to implement Nginx weighted polling algorithm in Go
  • Detailed explanation of how to add Nginx proxy using Go
  • Golang project with nginx deployment reverse proxy load balancing explanation

<<:  What is the base tag and what does it do?

>>:  The easiest way to debug stored procedures in Mysql

Recommend

DOCTYPE element detailed explanation complete version

1. Overview This article systematically explains ...

Solution for VMware Workstation Pro not running on Windows

After the National Day holiday, did any of you fi...

Vue implements video upload function

This article example shares the specific code of ...

How to implement the paging function of MyBatis interceptor

How to implement the paging function of MyBatis i...

About IE8 compatibility: Explanation of the X-UA-Compatible attribute

Problem description: Copy code The code is as fol...

Comparison between Redis and Memcache and how to choose

I've been using redis recently and I find it ...

The most commonly used HTML tags to create web pages

1. Optimization of commonly used HTML tags HTML s...

Summary of several common ways to abbreviate javascript code

Table of contents Preface Arrow Functions Master ...

Detailed explanation of execution context and call stack in JavaScript

Table of contents 1. What is the execution contex...

WHMCS V7.4.2 Graphical Installation Tutorial

1. Introduction WHMCS provides an all-in-one solu...

Solution to the problem of MySQL deleting and inserting data very slowly

When a company developer executes an insert state...