search
HomeOperation and MaintenanceNginxHow to configure a reverse proxy using Nginx

一般配置

在一般的情况下,熟悉nginx的朋友都知道,如果需要配置反向代理,直接写一个location上下文和proxy模块即可,如果需要自定义前缀,使用一个rewrite模块即可。简单例子如下:

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;
}

到这里,一般的反代配置就算是可以了,那么对于go-fastdfs而言是否可以了呢?对于go-fastdfs一般上传是可以了,但是呢,对于使用了tus做断点续传还是不够,为什么呢?因为tus服务端会返回301重定向,并且需要携带一定的请求头过去,所以还需要特别的设置一下。

支持tus的反代配置

如果需要做tus的反代,要支持301重定向location的重写还要支持一定的请求头转发,那该如何配置呢?请参考下面的配置

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;
 # 如果server_name不是公网域名,这个地方可以设置成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";
 # 因为使用了前缀加rewrite,所以要修改返回的location加上反向代理的前缀
 proxy_redirect ~^(.*)/group([0-9])/big/upload/(.*) /dfs/group$2/big/upload/$3;
 client_max_body_size 0;
}

上面要注意一下就是proxy_redirect和client_max_body_size这两个配置,第一个配置是因为tus服务端返回的重定向location是不会携带自定义前缀的,因此需要自己加入自定义的前缀,我这里是/dfs,如果是别的,更换下就好。第二个就是client_max_body_size了,这个是设置为0表示不管上传多大的文件都不会报request too large的问题,直接转发过去,如果需要设置,那么请设置大于等于chunksize的数字。什么是chunksize?就是tus客户端分块上传的时候,每一块的大小,具体请参考。

负载均衡配置

在配置了集群服务器的情况下,上传或者下载如何进行负载均衡呢?使用nginx做了反代,配合upstream模块就可以实现了,具体参考如下配置

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

上面的配置和一般的负载均衡没有什么不一样,唯一要注意的是要配置ip_hash,为什么呢?因为使用断点续传的时候,文件是分块上传的,如果不是ip_hash的话,有可能会前面几片被上传到a服务器,后面几片被上传到b服务器,那么这样子文件就不是完整的了,因此需要注意这个问题。

The above is the detailed content of How to configure a reverse proxy using Nginx. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
NGINX Unit's Purpose: Running Web ApplicationsNGINX Unit's Purpose: Running Web ApplicationsApr 30, 2025 am 12:06 AM

The purpose of NGINXUnit is to simplify the deployment and management of web applications. Its advantages include: 1) Supports multiple programming languages, such as Python, PHP, Go, Java and Node.js; 2) Provides dynamic configuration and automatic reloading functions; 3) manages application lifecycle through a unified API; 4) Adopt an asynchronous I/O model to support high concurrency and load balancing.

NGINX: An Introduction to the High-Performance Web ServerNGINX: An Introduction to the High-Performance Web ServerApr 29, 2025 am 12:02 AM

NGINX started in 2002 and was developed by IgorSysoev to solve the C10k problem. 1.NGINX is a high-performance web server, an event-driven asynchronous architecture, suitable for high concurrency. 2. Provide advanced functions such as reverse proxy, load balancing and caching to improve system performance and reliability. 3. Optimization techniques include adjusting the number of worker processes, enabling Gzip compression, using HTTP/2 and security configuration.

NGINX vs. Apache: A Look at Their ArchitecturesNGINX vs. Apache: A Look at Their ArchitecturesApr 28, 2025 am 12:13 AM

The main architecture difference between NGINX and Apache is that NGINX adopts event-driven, asynchronous non-blocking model, while Apache uses process or thread model. 1) NGINX efficiently handles high-concurrent connections through event loops and I/O multiplexing mechanisms, suitable for static content and reverse proxy. 2) Apache adopts a multi-process or multi-threaded model, which is highly stable but has high resource consumption, and is suitable for scenarios where rich module expansion is required.

NGINX vs. Apache: Examining the Pros and ConsNGINX vs. Apache: Examining the Pros and ConsApr 27, 2025 am 12:05 AM

NGINX is suitable for handling high concurrent and static content, while Apache is suitable for complex configurations and dynamic content. 1. NGINX efficiently handles concurrent connections, suitable for high-traffic scenarios, but requires additional configuration when processing dynamic content. 2. Apache provides rich modules and flexible configurations, which are suitable for complex needs, but have poor high concurrency performance.

NGINX and Apache: Understanding the Key DifferencesNGINX and Apache: Understanding the Key DifferencesApr 26, 2025 am 12:01 AM

NGINX and Apache each have their own advantages and disadvantages, and the choice should be based on specific needs. 1.NGINX is suitable for high concurrency scenarios because of its asynchronous non-blocking architecture. 2. Apache is suitable for low-concurrency scenarios that require complex configurations, because of its modular design.

NGINX Unit: Key Features and CapabilitiesNGINX Unit: Key Features and CapabilitiesApr 25, 2025 am 12:17 AM

NGINXUnit is an open source application server that supports multiple programming languages ​​and provides functions such as dynamic configuration, zero downtime updates and built-in load balancing. 1. Dynamic configuration: You can modify the configuration without restarting. 2. Multilingual support: compatible with Python, Go, Java, PHP, etc. 3. Zero downtime update: Supports application updates that do not interrupt services. 4. Built-in load balancing: Requests can be distributed to multiple application instances.

NGINX Unit vs. Other Application ServersNGINX Unit vs. Other Application ServersApr 24, 2025 am 12:14 AM

NGINXUnit is better than ApacheTomcat, Gunicorn and Node.js built-in HTTP servers, suitable for multilingual projects and dynamic configuration requirements. 1) Supports multiple programming languages, 2) Provides dynamic configuration reloading, 3) Built-in load balancing function, suitable for projects that require high scalability and reliability.

NGINX Unit: The Architecture and How It WorksNGINX Unit: The Architecture and How It WorksApr 23, 2025 am 12:18 AM

NGINXUnit improves application performance and manageability with its modular architecture and dynamic reconfiguration capabilities. 1) Modular design includes master processes, routers and application processes, supporting efficient management and expansion. 2) Dynamic reconfiguration allows seamless update of configuration at runtime, suitable for CI/CD environments. 3) Multilingual support is implemented through dynamic loading of language runtime, improving development flexibility. 4) High performance is achieved through event-driven models and asynchronous I/O, and remains efficient even under high concurrency. 5) Security is improved by isolating application processes and reducing the mutual influence between applications.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.