Loading

22、编译安装nginx及性能优化

22.1、编译安装nginx:

1、下载nginx:

[root@slave-node1 ~]# mkdir -p /tools/

[root@slave-node1 ~]# cd /tools/

[root@slave-node1 tools]# wget http://nginx.org/download/nginx-1.16.0.tar.gz


2、安装:

[root@slave-node1 tools]# yum install openssl openssl-devel gcc pcre pcre-devel -y

[root@slave-node1 tools]# useradd -M -s /sbin/nologin nginx

[root@slave-node1 tools]# tar -xzf nginx-1.16.0.tar.gz

[root@slave-node1 tools]# cd nginx-1.16.0/

[root@slave-node1 nginx-1.16.0]# ./configure --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --prefix=/application/nginx-1.16.0

[root@slave-node1 nginx-1.16.0]# make && make install

[root@slave-node1 nginx-1.16.0]# ln -s /application/nginx-1.16.0/ /application/nginx


3、启动nginx:

[root@slave-node1 nginx-1.16.0]# cd /application/

[root@slave-node1 application]# nginx/sbin/nginx

#nginx/sbin/nginx -s reload:重启nginx

#nginx/sbin/nginx -s stop:关闭nginx

[root@slave-node1 application]# netstat -tunlp | grep 80


4、访问验证:

http://172.16.1.91


22.2、nginx性能优化:

1、性能优化概述:

在做性能优化前, 我们需要对如下进行考虑:

(1)当前系统结构瓶颈:

观察指标

压力测试

(2)了解业务模式:

接口业务类型

系统层次化结构

(3)性能与安全:

性能好安全弱

安全好性能低


2、影响性能的指标:

影响性能方便整体关注

(1)网络:

网络的流量

网络是否丢包

这些会影响http的请求与调用

(2)系统:

硬件有没有磁盘损坏,磁盘速率

系统负载、内存、系统稳定性

(3)服务:

连接优化、请求优化

根据业务形态做对应的服务设置

(4)程序

接口性能

处理速度

程序执行效率

(5)数据库

每个架构服务与服务之间都或多或少有一些关联, 我们需要将整个架构进行分层, 找到对应系统或服务的短板, 然后进行优化。


3、压力测试工具:

(1)安装压力测试工具ab:

[root@slave-node1 application]# yum install httpd-tools -y


(2)使用ab工具进行压测:

[root@slave-node1 application]# ab -n 2000 -c 2 http://172.16.1.91/index.html

# -n:表示总的请求数;-c:表示并发请求数;-k:是否开启长连接

主要关注的参数如下:

Time taken for tests:总花费总时长

Complete requests:总请求数

Failed requests:请求失败数

Requests per second:每秒多少请求/s(总请求出/总共完成的时间)

Time per request:客户端访问服务端, 单个请求所需花费的时间

Time per request:服务端处理单个请求所花的时间

Transfer rate:判断网络传输速率,观察网络是否存在瓶颈


4、系统性能优化:

(1)文件句柄:

1)Linux一切皆文件,文件句柄可以理解为就是一个索引;

文件句柄会随着我们进程的调用频繁增加;

系统默认对文件句柄有限制,不能让一个进程无限的调用;

需要限制每个进程和每个服务使用多大的文件句柄;

文件句柄是必须要调整的优化参数。

2)调整文件描述符大小:

echo '* - nofile 65535' >>/etc/security/limits.conf

ulimit -SHn 65535

ulimit -n


(2)内核参数设置:

1)配置内核参数:

cat >>/etc/sysctl.conf<<EOF

net.ipv4.tcp_tw_reuse = 1

net.ipv4.ip_local_port_range = 1024 65535

EOF


说明:

net.ipv4.tcp_tw_reuse = 1

# 表示开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭。

net.ipv4.ip_local_port_range = 1024 65535

#指定外部连接的端口范围。默认值为32768 61000

2)生效配置的内核参数:

sysctl -p


5、nginx性能优化:

CPU亲和, 减少进程之间不断频繁迁移, 减少性能损耗。

(1)查看当前cpu的状态:

[root@slave-node1 ~]# lscpu |grep "CPU(s)"

#一颗物理cpu,每颗2核心,总共2核心。


(2)将nginx worker进程绑定到不同的核心上:

启动多少worker进程, 官方建议和cpu核心一致。

1)第一种绑定组合方式:

worker_processes 2;

worker_cpu_affinity 01 10;

# 表示:两核cpu开启2个进程,01表示启用第一个cpu内核,10表示启用第二个cpu内核。

2)第二种方式(最佳方式绑定方式):

worker_processes auto;

worker_cpu_affinity auto;


(3)查看nginx worker进程绑定对应的cpu:

[root@slave-node1 ~]# ps -eo pid,args,psr|grep [n]ginx


6、nginx通用优化配置文件:

(1)配置文件:

[root@slave-node1 application]# cat nginx/conf/nginx.conf

user nginx;

# worker进程的数量

worker_processes auto;

# 绑定Nginx的worker进程到指定的CPU内核

worker_cpu_affinity auto;


error_log logs/error.log warn;

pid logs/nginx.pid;

# worker进程可以打开的最大句柄描述符个数

# 调整至1w以上,负荷较高建议2-3w以上

worker_rlimit_nofile 65535;


events {

use epoll;

# 每个worker进程的最大连接数

# worker_processes乘以worker_connections等于nginx总体最大连接数

worker_connections 10240;

}


http {

include mime.types;

default_type application/octet-stream;

# 统一使用utf-8字符集

charset utf-8;

log_format main '$remote_addr - $remote_user [$time_local] "$request"'

'$status $body_bytes_sent "$http_referer"'

'"$http_user_agent" "$http_x_forwarded_for"';

access_log logs/access.log main;


# Core module

sendfile on;

# 静态资源服务器建议打开

tcp_nopush on;

# 动态资源服务建议打开,需要打开keepalived

tcp_nodelay on;

keepalive_timeout 65;


# Gzip module

gzip on;

gzip_disable "MSIE [1-6]\.";

gzip_http_version 1.1;


# Virtal Server

include conf.d/*.conf;

}


(2)创建配置文件目录:

[root@slave-node1 application]# mkdir -p nginx/conf/conf.d/


(3)测试 test.conf 文件:

[root@slave-node1 application]# vim nginx/conf/conf.d/test.conf

server {

listen 0.0.0.0:80;

server_name localhost;


location / {

root html;

index index.html index.htm;

}

error_page 404 /404.html;

error_page 500 502 503 504 /50x.html;

}


(4)验证配置文件:

[root@slave-node1 application]# nginx/sbin/nginx -t


(5)重启nginx:

[root@slave-node1 application]# nginx/sbin/nginx -s reload


(6)访问:

http:172.16.1.91




















posted @ 2020-03-15 14:43  云起时。  阅读(467)  评论(0编辑  收藏  举报