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

Detailed example of using case statement in MySQL stored procedure

This article uses an example to illustrate the us...

Detailed explanation of how to select all child elements using CSS

How to recursively select all child elements usin...

How to quickly build ELK based on Docker

[Abstract] This article quickly builds a complete...

How to optimize MySQL performance through MySQL slow query

As the number of visits increases, the pressure o...

Docker setting windows storage path operation

When installing Docker on Windows 10, after selec...

Docker mounts local directories and data volume container operations

1. Docker mounts the local directory Docker can s...

Steps to package and release the Vue project

Table of contents 1. Transition from development ...

Import CSS files using judgment conditions

Solution 1: Use conditional import in HTML docume...

How to use CSS styles and selectors

Three ways to use CSS in HTML: 1. Inline style: s...

Sample code for implementing radar chart with vue+antv

1. Download Dependency npm install @antv/data-set...

Solution to index failure in MySQL due to different field character sets

What is an index? Why create an index? Indexes ar...

MySQL operations: JSON data type operations

In the previous article, we introduced the detail...

Steps to transplant the new kernel to the Linux system

1. Download the ubuntu16.04 image and the corresp...

Markup language - simplified tags

Click here to return to the 123WORDPRESS.COM HTML ...

How to generate mysql primary key id (self-increment, unique and irregular)

Table of contents 1. Use the uuid function to gen...