nginx several applications

nginx.conf configuration

nginx load balancing

upstream load balancing, added under http

    upstream backend{
		server 127.0.0.1:8080 weight=1;	#一次8080
		server 127.0.0.1:8081 weight=2;	#两次8081
		
		#两个服务器崩溃,备用服务器
		#server backup.example.com:8080 weight=1 backup;
	}
	server {
        listen 80;
        server_name backend;
        location / {
        	#backend不要加斜杠,拼接location 斜杠/
        	#如果backend包含 斜杠/ 则会出现 双斜杠//
            proxy_pass http://backend;
            index index.html index.htm;
        }
    }

Run and reload nginx.conf

./nginx -s reload

Anti-theft chain, if it is not from hawkii.com, return 403 code

location /img/ {  #img是相对目录,是html目录下的img目录
  valid_referers none blocked server_names *.hawkii.com ; #允许访问该目录的域名或IP
  if ($invalid_referer) {return 403;} #不允许访问返回403
}
#打开防火墙80端口访问限制
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload

#查看已经开放端口号
firewall-cmd --list-all

Reverse proxy

access

http://127.0.0.1/edu/
#映射到 http://127.0.0.1:8081/edu/;
http://127.0.0.1/vod/
#映射到 http://127.0.0.1:8080/vod/;
    server {
        listen       80;
        #本机在局域网地址
        server_name  172.16.143.10;
        #通配符~ 包含/edu/
        location ~ /edu/ {
		proxy_pass http://127.0.0.1:8081;
        }
        location ~ /vod/ {
		proxy_pass http://127.0.0.1:8080;
        }

Insert picture description here

High availability

Virtual IP binding to multiple nginx servers

  1. Prepare two Linux systems (two virtual machines)
  2. Both servers have nginx and keepalived installed
  3. Virtual ip to nginx, configure keepalived- keepalived.conf , set virtual_ipaddress
  4. Write a script to detect whether nginx is running, nginx_check.sh
    Insert picture description here

nginx principle

One master, multiple workers

The number of workers and the number of server CPUs match best

An independent process for each worker

The setting of worker_connection of nginx, two or four, because the client needs to come back and forth, tomcat back and forth.

Nginx uses multiplexed io mechanism like redis to improve maximum performance

Nginx a master and four workers, each worker supports the maximum number of connections is 1024, what is the maximum number of concurrent support?

  • Static connection: work_connection * work_process / 2
  • Reverse proxy: work_connection * work_process / 4

Insert picture description here

Published 17 original articles · praised 0 · visits 211

Guess you like

Origin blog.csdn.net/neheqi/article/details/105446274