nginx基本配置、反向代理、负载均衡以及高可用

nginx基本配置、反向代理、负载均衡以及高可用

nginx的基本配置。

配置nginx是通过编辑nginx的配置文件实现的。

nginx配置文件位于/etc/nginx/nginx.conf或者/etc/nginx/conf.d/目录中。

nginx安装

bash
1
2
# 安装nginx
sudo yum install -y nginx

nginx命令

bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 查看nginx状态
sudo systemctl status nginx

# 启动nginx
sudo systemctl start nginx

# 停止nginx
sudo systemctl stop nginx

# 重启nginx
sudo systemctl restart nginx

# 重新载入nginx配置文件
sudo systemctl reload nginx

# 系统启动时启动nginx
sudo systemctl enable nginx

# 测试nginx配置文件是否正确
sudo nginx -t

nginx基本配置

conf
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
server {
# 监听80端口
listen 80;
# 域名
server_name example.com www.example.com;

# 网站根目录
root /var/www/example;
# 默认首页
index index.html index.htm;

location / {
try_files $uri $uri/ =404;
}

# 定义错误页面
error_page 404 /404.html;
location = /404.html {
internal;
}

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

nginx配置静态资源目录

conf
1
2
3
4
5
6
7
8
9
10
11
12
13
server {
listen 80;
server_name example.com;

location / {
proxy_pass http://localhost:3000;
}

# 配置静态文件目录,当客户端访问下面的几种格式的静态文件时nginx会定向到/static目录下
location ~ \.jpg|.png|.gif|.css|.js$ {
root static;
}
}

nginx配置反向代理

nginx除了可以支持正向代理还可以支持反向代理,反向代理就是对客户端隐藏了真实的服务端。

下面的配置会将所有传入的请求转发给本地的http://localhost:3000服务器。

conf
1
2
3
4
5
6
7
8
9
10
11
12
server {
listen 80;
server_name example.com;

location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}

nginx配置负载均衡

conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 服务器集群,serverlist是集群名称
upstream serverlist {
# 真实服务器
server 192.168.1.101:8080;
server 192.168.1.102:8080;
}

server {
listen 80;
server_name example.com;

location / {
# 指定服务器集群
proxy_pass http://serverlist;
}
}

负载均衡策略:

  • 轮询

nginx默认使用轮询策略。

  • 权重

给服务器集群的每台真实服务器添加权重,各个权重表示各个服务器被访问的频率比例。

conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 服务器集群,serverlist是集群名称
upstream serverlist {
# 真实服务器,带权重
server 192.168.1.101:8080 weight=1;
server 192.168.1.102:8080 weight=2;
}

server {
listen 80;
server_name example.com;

location / {
# 指定服务器集群
proxy_pass http://serverlist;
}
}
  • ip_hash

根据客户端ip分配真实服务器。

conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 服务器集群,serverlist是集群名称
upstream serverlist {
# ip_hash
ip_hash;
# 真实服务器
server 192.168.1.101:8080;
server 192.168.1.102:8080;
}

server {
listen 80;
server_name example.com;

location / {
# 指定服务器集群
proxy_pass http://serverlist;
}
}
  • 第三方插件

使用第三方插件,插件可能使用了其他策略。

nginx配置https

下面配置了Let’s Encrypt证书:

conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}

server {
listen 443 ssl;
server_name example.com;

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

root /var/www/example;
index index.html index.htm;

location / {
try_files $uri $uri/ =404;
}
}

安装certbot和获取证书:

bash
1
2
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com

nginx高可用

以上都是基于单个nginx节点进行的配置,如果这个节点挂了那么整个后端服务都挂了。

为了实现nginx的高可用,一般是在多台服务器上部署nginx节点,在每台服务器上安装keepalived,再配置keepalived。

  1. 准备多台服务器并部署nginx

  2. 在每个服务器上安装keepalived

bash
1
sudo yum install -y keepalived
  1. 配置keepalived

文件路径:/etc/keepalived/keepalived.conf

conf
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
# 节点ip
smtp_ server 192.168.232.166
smtp_connect_timeout 30
# 节点ip
router_id 192.168.232.166
}

# 定义chk_http_port
vrrp_script chk_http_port {
# nginx检测脚本
script "/usr/local/src/nginx_check.sh"
interval 2
# 如果上面脚本检测成功则降低优先级,反之则相反
weight -20
}

# 节点配置
vrrp_instance VI_1 {
# 将这个节点配置为主节点,BACKUP为从节点
state MASTER
# 使用的网络接口
interface eth0
# VRID,必须在主备服务器上保持一致
virtual_router_id 51
# 节点优先级,数值越大优先级越高
priority 100
# 通告间隔时间,秒
advert_int 1
# 认证密码,主从服务器必须保持一致
authentication {
auth type PASS
auth pass 1111
}
# 虚拟ip,该ip需要配置和其他节点ip在同一个网段,主备节点共享
virtual_ipaddress {
192.168.0.100
}
#将 track_script 块加入 instance 配置块
track_script {
chk_http_port #执行 Nginx 监控服务
}
}
  1. /usr/local/src下添加nginx_check.sh文件
shell
1
2
3
4
5
6
#!/bin/bash
# 检查是否开启nginx
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
systemctl stop keepalived
fi
  1. 启动keepalived

在配置完所有的节点后,在每个节点启动keepalived。

bash
1
2
sudo systemctl enable keepalived
sudo systemctl start keepalived.service
  1. 验证配置

6.1 在主节点上查看虚拟ip是否已经绑定到了接口。

bash
1
ip addr show

6.2 在备用服务器上,停止主服务器的keepalived服务,检查虚拟IP是否切换到备用服务器。

6.3 检查日志文件确保keepalived正常工作。

bash
1
2
cat /var/log/syslog
cat /var/log/messages
  1. 访问虚拟ip获取服务

现在可以访问虚拟ip获取服务。

评论