Nginx Proxy Config
当前文档有中文版本:点击这里切换到中文
What is a Proxy
At its core, a proxy acts as an intermediary between two systems. In the context of networks, it’s a server that handles traffic between clients (like your web browser) and other servers on the internet.
Forward Proxy:
- Client-side: A forward proxy sits in front of a group of clients within a network.
- Function:
- Manage Outgoing Traffic: Controls and filters internet access for clients.
- Security: Provides a protective layer to shield clients from malicious content or websites.
- Caching: Stores frequently accessed web content to reduce bandwidth usage and improve load times.
- Anonymization: Helps users mask their IP addresses to browse the web privately.
Reverse Proxy
- Server-Side: A reverse proxy sits in front of one or more web servers.
- Function:
- Traffic Distribution: Distributes incoming requests across multiple servers (load balancing), optimizing performance and preventing overload.
- Security: Acts as a shield for backend servers, masking their true identities from direct client contact.
- Caching: Stores static content, reducing the load on backend servers.
- Content Delivery: Can optimize content for different devices and locations.
Nginx Common Commands
#get nginx version |
Nginx Config
The nginx config file consists of three parts
Part 1: Global
From the beginning of the configuration file to the events block
The configuration here is designed for nginx servers running
# work_processes |
Part 2: Event
The configuration here refers to setting the number of connections between the Nginx server and the user’s network.
events { |
Part 3: Http
Includes: file introduction, MIME-TYPE definition, log customisation, connection timeout, maximum number of requests for a single link, etc.
http { |
Part 4: Server
Each server block represents a distinct website or application, allowing Nginx to serve different content based on client requests.
server { |
Nginx Reverse Proxy
Config Example
server { |
server { |
Some Reverse Proxy Config
# specify proxy url or name |
Load Balance
config example
upstream myserver{ |
Load Balance Policy
Round Robin (default)
Distributes requests sequentially across a list of servers. Simple and easy, but doesn’t account for server capacity differences.
upstream myserver{ |
Weight
Assigned different weights to influence the distribution of traffic.
upstream myserver{ |
IP Hash
Maps a client’s IP address to a specific server, ensuring consistency for some types of applications.
upstream myserver{ |
Fair
Nginx keeps track of how busy each upstream server is, including factors like active connections and request processing time.
When a new request arrives, Nginx will try to select the server that currently has the lightest load.
upstream myserver{ |