Nginx configuration file composition (reverse proxy, load balancing implementation)

The configuration file is divided into 3 parts
1. Global block
Configure the instructions for the overall operation
Insert picture description here

worker_processes  1;

The larger the value, the more the number of concurrent processing by nginx

2. The events block
mainly affects the network connection between the nginx server and the user.
Insert picture description here
Here is the maximum number of connections supported by Nginx, which has a greater impact on Nginx in practice

3.http global block The
most frequently configured part
includes http and server blocks





A reverse proxy

  1. Effect: To access port 80, switch to port 8080
启动nginx
start nginx

关闭
nginx.exe -s stop
nginx.exe -s quit
注:stop是快速停止nginx,可能并不保存相关信息;quit是完整有序的停止nginx,并保存相关信息。

重新载入Nginx:
nginx.exe -s reload
当配置信息修改,需要重新载入这些配置时使用此命令。

Insert picture description here

location ~ .* {
    
    
            proxy_pass   http://127.0.0.1:8080;
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Fonwarded-For $proxy_add_x_forwarded_for;
        }

Two: load balancing

1: Add the address to be balanced on the server.
Insert picture description here
Be careful not to add http. We will add it later

upstream myserver {
    
    
	   server 127.0.0.1:8080 weight=10;
	   server www.handsomehuang.cn weight=10;
	}

2: Register the address we just wrote in the location.
Insert picture description here
At this point, you can access the nginx80 port to achieve load balancing.

Adding ip_hash here allows each user to access a certain port fixedly, which can solve the session problem

upstream myserver {
    
    
	   ip_hash;
	   server 127.0.0.1:8080 weight=10;
	   server www.handsomehuang.cn weight=10;
	}

Guess you like

Origin blog.csdn.net/qq_45432665/article/details/114284233