nginx 常用

CentOS 7 (使用yum进行安装)

建议先替换CentOS 7阿里镜像地址

1
2
3
4
5
6
7
8
9
10
#添加CentOS 7阿里镜像地址
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
或者
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
#安装Nginx
sudo yum install -y nginx
#启动Nginx
sudo systemctl start nginx
#CentOS 7 开机启动Nginx
sudo systemctl enable nginx

常用配置

静态资源

1
2
3
4
5
6
7
8
9
10
server {
server_name static.domain.com;
root /app/static;
index null;
location / {
autoindex on; #是否开启索引
autoindex_exact_size on;
autoindex_localtime on;
}
}

静态资源加认证

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#生成用户名密码
printf "hugo:$(openssl passwd -crypt 123456)\n" >>/etc/nginx/user_file
cat /etc/nginx/user_file
#增加认证配置
server {
server_name static.domain.com;
root /app/static;
index null;
location / {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/user_file;
autoindex on; #是否开启索引
autoindex_exact_size on;
autoindex_localtime on;
}
}

代理支持ws

1
2
3
4
5
6
7
8
9
10
11
12
13
14
server {
server_name ws.domain.com;
location / {
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $remote_addr;

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

proxy_pass http://127.0.0.1:8080;
}
}

配置https证书

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
server {
server_name test.domain.com;

location ^~ /.well-known/acme-challenge/ {
default_type "text/plain";
root /usr/share/nginx/html;
}

location / {
return 301 https://test.domain.com$request_uri;
}
}

server {
server_name test.domain.com;
listen 443 ssl;

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

location / {
proxy_pass https://192.168.99.100:31808;
}
}

upstream 五种分配方式

1、轮询(weight=1)

默认选项,当weight不指定时,各服务器weight相同,
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

1
2
3
4
upstream testapi {
server 192.168.1.61:8085;
server 192.168.1.62:8085;
}

2、weight

指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
如果后端服务器down掉,能自动剔除。
例如以下配置,则62服务器的访问量为61服务器的两倍。

1
2
3
4
upstream testapi {
server 192.168.1.61:8085 weight=1;
server 192.168.1.62:8085 weight=2;
}

3、ip_hash

每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session不能跨服务器的问题。
如果后端服务器down掉,要手工down掉。

1
2
3
4
5
upstream testapi {
ip_hash;
server 192.168.1.61:8085;
server 192.168.1.62:8085;
}

4、fair(第三方插件)

按后端服务器的响应时间来分配请求,响应时间短的优先分配。

1
2
3
4
5
upstream testapi {
server 192.168.1.61:8085;
server 192.168.1.62:8085;
fair;
}

5、url_hash(第三方插件)

按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存服务器时比较有效。
在upstream中加入hash语句,hash_method是使用的hash算法。

1
2
3
4
5
6
upstream testapi {
server 192.168.1.61:8085;
server 192.168.1.62:8085;
hash $request_uri;
hash_method crc32;
}

下面来看一个负载均衡示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
upstream testapi {
#ip_hash;
server 192.168.1.61:8085;
server 192.168.1.62:8085 weight=100 down;
server 192.168.1.63:8085 weight=100;
server 192.168.1.64:8085 weight=100 backup;
server 192.168.1.65:8085 weight=100 max_fails=3 fail_timeout=30s;
}

server {
server_name test.domain.com;
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://testapi;
}
}

示例说明:

  1. down 表示当前的server暂时不参与负载
  2. weight 默认为1.weight越大,负载的权重就越大。
  3. backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
  4. 上例中192.168.1.65:8085设置最大失败次数为 3,也就是最多进行 3 次尝试,且超时时间为 30秒。max_fails 的默认值为 1,fail_timeout 的默认值是 10s。

注意:

当upstream中只有一个server时,max_fails 和 fail_timeout 参数可能不会起作用。

weight\backup不能和ip_hash关键字一起使用。

Donate comment here