nginx reverse proxy, load balancing, dynamic and static combination

Preface

What is nginx?

Is a high-performance HTTP and reverse proxy server. Unzip the nginx installation package and use it

1. Startup abnormality

Port 1.80 is occupied

① Close the occupied port

netstat -ano  //查看端口情况,查看80端口对应的PID,然后任务管理器给关掉就行

Insert image description here

② Modify the nginx.conf file

In nginx -> conf

Insert image description here

2. Start nginx and test whether it starts successfully

① Start

The administrator starts it. If nginx exists in the task manager, it means it has been started.
Insert image description here

② Test computer IP + the port you set in nginx.conf

Insert image description here

2. Load balancing of application scenarios

This means that execution is distributed to multiple operating units. For example, two servers deploy the same service, and one has good performance and one has poor performance. Then, through load balancing, the server with good performance can receive more requests.

http {
    include       mime.types;
    default_type  application/octet-stream;

	# 权重越大,如果三次请求,两次会访问到8081
	upstream xinxin{
	server 10.73.100.110:8082 weight=1;
	server 10.73.100.110:8081 weight=2;
    }

    server {
        listen       8099;
        server_name  localhost;


        location / {
            root   html;
            index  index.html index.htm;
			proxy_pass xinxin; #当你输入 localhost + 8099 或 你的主机名 + 8099
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }
}

3. Application scenario reverse proxy

1. The difference between forward proxy and reverse proxy

Forward proxy (almost rarely used): client <——> proxy —> server.
The client knows the server and knows the existence of the proxy. The server does not know the existence of the client and knows the existence of the proxy.
Example: A (tenant) B (shady agent) C (landlord)
When A wants to form a relationship with C but cannot contact C, then B gets to know C, and B rents C's house, and then rents it to A.

Reverse proxy (commonly used): Client -> Proxy <-> Server.
The client does not know the server, and the server does not know the client. They only know the existence of the proxy.
Liezi: A (tenant) B (shady agency) C (landlord)
A wants to rent a house, but B rents C’s house and then rents it to A.

2. Configure reverse proxy

nginx -s reload //修改配置文件之后,重启nginx使配置文件生效

Insert image description here

Insert image description here
After setting, your IP + the port you set will jump to www.baidu.com

4. Combination of dynamic and static application scenarios

Separate static resources and dynamic resources to improve server performance and maintainability.
Insert image description here

server {
     listen       8099;
      server_name  localhost;
      
      # 动态请求
      location /dong {
      	  #动态资源的路径
          proxy_pass http://localhost:8088;
          # 客户端IP
          proxy_set_header   X-Real-IP        $remote_addr; 
          # 请求头中Host信息
          proxy_set_header   Host             $host; 
          # 访问协议
          proxy_set_header   X-Forwarded-Proto $scheme;
      }

	  #静态请求
	  location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt)$
	  {
	   root /static;
	   expires 30d;
	  }

	  # 出现错误的返回路径
      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
          root   html;
      }
}

Guess you like

Origin blog.csdn.net/twotwo22222/article/details/128662044