nginx common configuration templates

nginx common configuration templates

1. Forward proxy

# Forward proxy to baidu service
location = /baidu.html {

The concept of forward proxy:Forward proxy means adding a proxy server between the client and the target server. The client directly accesses the proxy server, and then the proxy server accesses the target server and returns to the client. During this process, the client needs to know the proxy server address and configure the connection.

Application scenarios:Nexus services for crawlers, circumvention, and maven.

2. Reverse proxy

# Reverse proxy to the local 8010 service
location /test/ {

The concept of reverse proxy:Reverse proxy means that the client accesses the target server. There is a unified access gateway inside the target service that forwards the request to the server that is actually processed by the backend and returns the result. In this process, the client does not need to know the proxy server address, and the proxy is transparent to the client.

Application scenarios: nginx, Apache load balancing application.

Agent related parameters:

# Proxy service
# Whether to allow redirection
proxy_redirect off;
# Pass header parameters to the backend service
proxy_set_header Host $host;
#Set the request header, which is the client IP address
proxy_set_header X-Forwarded-For $remote_addr;
#Connection proxy service timeout
proxy_connect_timeout 90;
#Maximum time for request sending
proxy_send_timeout 90;
#Read maximum time
proxy_read_timeout 90;
proxy_buffer_size 4k; 
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k; 
proxy_temp_file_write_size 64k;

3. Load balancing

Requests can be proxied to backend services through proxy_pass, but in order to achieve higher load and performance, we usually have multiple backend services. This is the time to implement load balancing through the upstream module.

upstream backend {     
	server weight=1;
	server weight=2;

	server weight=1 backup;
location / {
	proxy_pass http://backend;

upstream related parameters:

# Reverse service address plus port
# Weights
# After the number of failures, the host is considered to be down and kicked out.
# Re-detection time after kicking out
# backup service
#Maximum number of connections allowed
# When the node recovers, it does not join immediately, but waits for slow_start before joining the service queue.

Introduction to upstream load balancing algorithm

# Polling weight (default)
# Based on Hash calculation, used to maintain session consistency
# Static resource caching, saving storage and speeding up (third party)
# Minimum links (third party)
# Minimum response time, calculate the average response time of the node, and then take the one with the fastest response and assign a higher weight (third party)
# Note: Third parties need to install plug-ins for nginx themselves, and nginx itself does not implement them.

4.nginx cache configuration

Add cache declaration under http element

# proxy_cache_path cache path
# levels cache levels and directory bits
# keys_zone buffer memory size
# inactive validity period
# max_size hard disk size
proxy_cache_path /data/nginx/cache_test levels=1:2 keys_zone=cache_test:500m inactive=20d max_size=1g;

Set cache policy for specified location

#Specify cache area
proxy_cache cache_test;
# Use the full path md5 value as the key 
proxy_cache_key $host$uri$is_args$args;
# Set different cache times for different HTTP status codes
proxy_cache_valid 200 304 12h;

cache clearing

# This function can be implemented using the third-party module ngx_cache_purge
# Clear configuration
location ~ /clear(/.*) {
  #IP allowed access
   allow ;
   allow ;
   #Banned IP
   deny            all;
   #Configure to clear the specified cache area and path (together with proxy_cache_key)
   proxy_cache_purge    cache_test $host$1$is_args$args;
#Access the generated cache file
# Clear the generated cache. If the specified cache does not exist, a 404 error will be reported.

Related Posts

1+X intermediate mall cluster construction (three hosts)

nginx installation on Centos system

Nginx configuration load balancing

Chapter 2 Building a high-performance video system

Project practice, nginx service reverse proxy configuration instructions

Nginx startup error: nginx: [emerg] bind() to failed (13: Permission denied)

Nginx forward https

What is Nginx? Detailed explanation of Nginx basic teaching. How to install Nginx on Linux

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>