Nginx 重要概念

正向代理和反向代理

负载均衡

动静分离

Nginx linux下安装

Nginx 常用命令

nginx命令所在路径

1
/usr/local/software/nginx-1.20.2/sbin

查看版本号

1
./nginx -v

查看nginx进程

1
ps -ef | grep nginx

停止nginx

1
./nginx -s stop

启动nginx

1
./nginx

重新加载 nginx

1
./nginx -s reload

Nginx 配置文件

配置文件位置

/usr/local/software/nginx-1.20.2/conf/nginx.conf

配置文件内容详解

第一部分:全局块

配置nginx 服务器整体运行的一些指令:用户组、日志、进程pid、nginx进程数

1
2
3
4
5
6
7
8
#定义Nginx运行的用户和用户组
user www www;
#nginx进程数,建议设置为等于CPU总核心数。
worker_processes 8;
#全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
error_log /usr/local/nginx/logs/error.log info;
#进程pid文件
pid /usr/local/nginx/logs/nginx.pid;

第二部分:events块

配置nginx服务器与用户的网络连接

1
2
# 连接最大数
worker_connections 1024;

第三部分:http块

http块中包含 http全局块server块

http全局块

server块

server块中包含 server全局块location块

server全局块

location块

proxy_pass 路径;

路径匹配规则

负载均衡配置

动静分离配置

location 中的root,index ,autoindex

nginx 高可用集群

2台nginx服务器

2台服务器安装keepalived

keepalived 配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL # 路由,您的服务器中配置的dns映射,在etc/host文件中查看
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}

vrrp_instance VI_1 {
state MASTER #主服务器MASTER,备份服务器BACKUP
interface eth0 #网卡
virtual_router_id 51 #主备机的值要一致
priority 100 #优先级,一般主服务器设置100 , 备份服务器要<100
advert_int 1
authentication { # 认证校验
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.16 #虚拟IP地址
192.168.200.17
192.168.200.18
}
}

nginx 原理

master 和 work

一个master ;多个worker