1. Nginx安装(以Ubuntu为主)
1.1 ubuntu
sudo apt update
sudo apt install nginx
1.2 centos
cat /etc/yum.repos.d/nginx.repo#修改源
[root]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$branch/
gpgcheck=0
enabled=1
1.3 配置文件详解
/ect/nginx/nginx.conf
user #用户
event.worker_connections#每个工作进程可以启动的连接
http.default_type=application/octet-stream#二进制流
1.4 启动 nginx
sudo systemctl start nginx
1.5 卸载 nginx
sudo apt-get remove nginx nginx-common
sudo apt-get purge nginx nginx-common
sudo apt-get autoremove
sudo apt-get remove nginx-full nginx-common
1.6 相关命令
systemctl status/start/stop nginx:查看、启动、关闭nginx
nginx -s reload|stop|start:重启、关闭、启动
nginx -V:查看编译参数
/etc/nginx/nginx.conf:默认配置文件
nginx -s reload :修改配置后重新加载生效
注意:配置反向代理要关闭selinux,setenforce 0
[注意] 修改nginx
配置后应该使用nginx -s reload
使配置生效
2. ngx_http_stub_status监控连接信息
2.1 添加配置
/etc/nginx/conf.d/*.conf
location = /nginx_status{
stub_status on;
access_log off;
allow 127.0.0.1;#允许本机
deny all;#其它都拒绝
}
2.2 查看stub_status
输出
wget http://localhost/nginx_status
3. ngxtop监控请求信息
3.1 安装python-pip
#centos
yum install epel-release
yum install python-pip
#ubuntu
sudo apt install python-pip
3.2 安装ngxtop
pip install ngxtop
3.3 ngxtop使用
3.3.1 指定配置文件
ngxtop -c /etc/nginx/nginx.conf
3.3.2 查询状态是200
ngxtop -c /etc/nginx/nginx.conf -i 'status==200'
3.3.3 查询访问最多ip
ngxtop -c /etc/nginx/nginx.conf - g remote_addr
4. nginx-rrd图形化监控
4.1 安装pip
sudo apt-get install php php-gd php-soap php-mbstring php-xmlrpc php-dom php-fpm -y
4.2 nginx整合php-fpm
修改
/etc/php-fpm.d/www.conf
文件中的user
和group
,与nginx.conf
中的user
一致。
user=nginx
group=nginx
注意
sudo find / -name www.conf
查看www.conf
文件位置
4.3 启动php-fpm
服务
[ubuntu]
4.4 修改nginx的配置
location ~ \.php${
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
}
4.5 重启nginx
nginx -s reload
4.6 安装rrdtool相关依赖
[centos]
yum install perl rrdtool perl-libwww-perl libwww-perl perl-rrdtool -y
4.7 安装nginx-rdd
wget http://soft.vpser.net/status/nginx-rrd/nginx-rrd-0.1.4.tgz
tar zxvf nginx-rrd-0.1.4.tgz
cd nginx-rrd-0.1.4
cp usr/sbin/* /usr/sbin # 复制主程序文件到/usr/sbin下
cp etc/nginx-rrd.conf /etc #复制配置文件到/etc下
cp html/index.php /usr/share/nginx/html/
4.8 修改配置vi /etc/nginx-rrd.conf
RRD_DIR="/usr/share/nginx/html/nginx-rrd";
WWW_DIR="/usr/share/nginx/html";
4.9 新建定时任务
crontab -e# 新建任务
** *** /bin/sh /usr/sbin/nginx-collect
*/1 * *** /bin/sh /usr/sbin/nginx-graph
crontab -l 查看任务
tail -f /var/log/cron
4.10 ab压测
[centos]
yum -y install httpd-tools
ab -n 10000 -c 10 http://127.0.0.1/index.html
# 10000个请求,并发为10;
5. nginx优化
- 增加工作线程和并发连接数
nginx默认是一个工作线程,1024个并发连接数。
/etc/nginx/nginx.conf
work_process 4|auto;//CPU个数
events{
worker_connections 10240;//[需要修改系统文件配置]每个工作线程的最大连接数,包含了nginx与客户端和nginx与upstream之间的连接。
multi_accept on;//可以一次建立多个连接
use epoll;
}
- 启用长连接
/etc/nginx/conf.d/default.conf
upstream
:配置反向代理集群
upstream server_pool{
server localhost:8080 weight=1 max_fails=2 fail_timeouts=30s;
server localhost:8081 weight=1 max_fails=2 fail_timeouts=30s;
keepalive 300;#300个长连接
}
location / {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://server_pool/;
}
- 启用缓存、压缩
# 配置压缩
gzip on;
gzip_http_version 1.1;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_proxied any;
gzip_types text/plain text/css application/javascript application/x- javascript application/isonapplication/xmlapplication/vnd.ms-
fontobject application/x-font-ttf application/svg+xml application/x-icon;
gzip_vary on;#Vary:Accept-Encoding
gzip_static on;#如果有压缩好的直接使用
- 操作系统优化
配置文件/etc/sysctl.conf
sysctl -w net.ipv4.tcp_syncookies=1#防止一个套接字在有过多试图连接到达时引起过载
sysctl -w net.core.somaxconn=1024#默认128,连接队列
sysctl -w net.ipv4.tcp_fin_timeout=10#timewait的超时时间
sysctl -w net.ipv4.tcp_tw_reuse=1#os直接使用timewait的连接
sysctl -w net.ipv4.tcp_tw_recycle=0#回收禁用
配置文件/etc/security/limits.conf
* hard nofile 204800
* soft nofile 204800
* soft core unlimited
* soft stack 204800
- 其他优化
/etc/nginx/nginx.conf
sendfile on;#减少文件再应用和内核之间的拷贝
tcp_nopush on;#当数据包达到一定大小再发送
tcp_nodelay off;#有数据随时发送