nginx的一些基本配置
1.并发优化
vim /usr/local/nginx/conf/nginx.conf
内容如下:
worker_processes 2; #工作进程数 最大不超过cpu个数
events {
worker_connections 65535; #单个工作进程并发连接数
}
2.负载均衡
机器 | 服务 |
---|---|
server4 | 控制主机 |
server2 | httpd服务 |
server3 | nginx服务 |
server4:
vim /usr/local/nginx/conf/nginx.conf
内容如下:
upstream westos { #负载均衡器upstream
server 172.25.14.2:80; #server2
server 172.25.14.3:80; #server3
}
server {
listen 80;
server_name www.westos.org;
location / {
proxy_pass http://westos; # 反向代理器proxy_pass
}
}
nginx -t 检测语法
nginx -s reload 刷新nginx服务
server2:
安装apache服务,开启服务,编辑默认发布文件
yum install httpd -y
cd /var/www/html/
echo server2 > index.html
systemctl enable --now httpd.service
server3:
从server4传nginx并生成软链接方便使用
scp -r root@172.25.14.4:/usr/local/nginx /usr/local/
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
编辑nginx主配置文件
vim /usr/local/nginx/conf/nginx.conf
内容如下:
location / {
root html;
index index.html index.htm;
}
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi.conf;
#}
编辑默认发布文件
cd /usr/local/nginx/html
echo server3 > index.html
nginx #开启服务
测试主机:
vim /etc/hosts
添加解析:
172.25.14.4 www.westos.org
测试主机进行查看
[root@foundation14 ~]# curl www.westos.org
server2
[root@foundation14 ~]# curl www.westos.org
server3
3.nginx平滑升级
平滑升级:升级时不会关闭服务。
下载1.19版本安装包并编译安装
tar zxf nginx-1.19.4.tar.gz
cd /nginx-1.19.4
./configure --prefix=/opt/nginx
make
make install
解压新版本只做make不make install
tar zxf nginx-1.20.1.tar.gz
cd nginx-1.20.1/
./configure
make
cd objs/
\cp -f nginx /opt/nginx/sbin/
/opt/nginx/sbin/nginx -v
查看当前nginx版本
[root@server4 ~]# /opt/nginx/sbin/nginx -v
nginx version: nginx/1.19.4
查看进程号
[root@server4 nginx-1.20.1]# ps ax|grep nginx
9470 ? ss 0:00 nginx: master process /opt/nginx/ / sbin/nginx
9471 ? s 0:00 nginx: worker process
12407 pts/0 S+_ 0:00 grep --color=auto nginx
启动新版本
[root@server4 ~]# kill -USR2 9470
[root@server4 ~]# ps ax | grep nginx
9470 ? ss 0:00 nginx: master process /opt/nginx/ /sbin/nginx
9471 ? s 0:00 nginx: worker process
12453 ? s 0:00 nginx: master process /opt/nginx/ / sbin/nginx
12454 ? s 0:00 nginx: worker process
12457 pts/0 S+ 0:00 grep --color=auto nginx
关闭老版本进程
[root@server4 ~]# kill -WINCH 9470
版本回退
cp -f nginx.old nginx
kill -HUP 原进程号 #唤醒原进程
kill —QUIT 新版本主进程号 #关闭新版本主进程
4.nginx限流
vim /usr/local/nginx/conf/nginx.conf
内容如下:
limit_conn_zone $binary_remote_addr zone=addr:10m; #
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; # 限制访问频次,这里限制每秒1次
server {
location /download {
limit_conn addr 1; #限制并发次数
limit_rate 50k; #限制带宽
limit_req zone=one;
limit_req zone=one burst=5;
limit_req zone=one burst=5 nodelay;
}
}
5.nginx配置管理
1.缓存配置
[root@server4 conf]# vim nginx.conf
location ~ .*\.(gif|jpg|png)$ {
expires 365d; ##缓存有效时长
root html;
}
[root@server4 conf]# nginx -s reload
[root@foundation14 ~]# curl -I http://172.25.14.4/download/vim.jpg
2.限制ip访问
[root@server4 conf]# vim nginx.conf
location / {
deny 172.25.14.250;
allow 172.25.14.0/24;
}
3.日志轮询
编写脚本nginx_log.sh
#!/bin/bash
cd /usr/local/nginx/logs && mv access.log access_$(date +%F -d -1day).log # date +%F -d -1day #表示当前日期减1天
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid` #cat /usr/local/nginx/logs/nginx.pid #查看nginx进程号
执行脚本,查看日志