GVKun编程网logo

CentOS 6.3下Nginx性能优化设置(nginx性能调优)

11

本文将带您了解关于CentOS6.3下Nginx性能优化设置的新内容,同时我们还将为您解释nginx性能调优的相关知识,另外,我们还将为您提供关于20.Nginx性能优化、ab测试nginxNginx

本文将带您了解关于CentOS 6.3下Nginx性能优化设置的新内容,同时我们还将为您解释nginx性能调优的相关知识,另外,我们还将为您提供关于20. Nginx性能优化、ab测试nginx Nginx性能优化、CentOS 6.3下nginx、php-fpm、drupal快速部署、CentOS 6.5下Nginx 编译安装的实用信息。

本文目录一览:

CentOS 6.3下Nginx性能优化设置(nginx性能调优)

CentOS 6.3下Nginx性能优化设置(nginx性能调优)

一.Nginx优化配置

1.主配置文件优化:
# vi /usr/local/Nginx/conf/Nginx.conf
—————————————?
user Nginx Nginx;

worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000;

error_log /usr/local/Nginx/logs/Nginx_error.log crit;

pid /usr/local/Nginx/logs/Nginx.pid;

worker_rlimit_nofile 204800;

events
{
use epoll;
worker_connections 204800;
}

http {
include mime.types;
default_type application/octet-stream;

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;

charset utf-8;

server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 20m;

sendfile on;
tcp_nopush on;

keepalive_timeout 60;

fastcgi_cache_path /usr/local/Nginx/fastcgi_cache levels=1:2
keys_zone=TEST:10m
inactive=5m;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;

open_file_cache max=204800 inactive=20s;
open_file_cache_min_uses 1;
open_file_cache_valid 30s;

tcp_nodelay on;

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
}
—————————————?

注:部分配置详解:
worker_processes 8;
Nginx进程数,建议按照cpu数目来指定,一般为它的倍数。

worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
为每个进程分配cpu,上例中将8个进程分配到8个cpu,当然可以写多个,或者将一个进程分配到多个cpu。

worker_rlimit_nofile 204800;
这个指令是指当一个Nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit -n)与Nginx进程数相除,但是Nginx分配请求并不是那么均匀,所以最好与ulimit -n的值保持一致。
注:这里需要设置 ulimit -SHn 204800

use epoll;
使用epoll的I/O模型,这个不用说了吧。

worker_connections 204800;
每个进程允许的最多连接数,理论上每台Nginx服务器的最大连接数为worker_processes*worker_connections。

keepalive_timeout 60;
keepalive超时时间。

client_header_buffer_size 4k;
客户端请求头部的缓冲区大小,这个可以根据你的系统分页大小来设置,一般一个请求的头部大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。分页大小可以用命令getconf PAGESIZE取得。

open_file_cache max=102400 inactive=20s;
这个将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存。

open_file_cache_valid 30s;
这个是指多长时间检查一次缓存的有效信息。

open_file_cache_min_uses 1;
open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive时间内一次没被使用,它将被移除。


20. Nginx性能优化

20. Nginx性能优化

ab性能测试

yum install httpd-tools
ab -n 100 -c 100 http://127.0.0.1/

#并发数量
Concurrency Level:      100
#整个测试花费的时间
Time taken for tests:  0.014 seconds
#完成请求的数量
Complete requests:      100
#失败请求的数量
Failed requests:        0
#写错误数量
Write errors:          0
#非200响应码数量
Non-2xx responses:      100
#总共传输字节
Total transferred:      37900 bytes
#html字节大小
HTML transferred:      18500 bytes
#每秒可以请求数量
Requests per second:    6914.67 [#/sec] (mean)
#客户端处理每个请求花费的时间
Time per request:      14.462 [ms] (mean)
#服务器端处理每个请求花费的时间
Time per request:      0.145 [ms] (mean, across all concurrent requests)
#传输速度
Transfer rate:          2559.24 [Kbytes/sec] received

性能优化方案

  • How To Optimize Nginx Configuration https://www.digitalocean.com/community/tutorials/how-to-optimize-nginx-configuration

专题阅读

  • 1. Nginx的优点
  • 2. Nginx的安装与开机自启
  • 3. Nginx目录和配置语法
  • 4. Nginx模块
  • 5. Nginx静态资源处理
  • 6. Nginx浏览器缓存原理
  • 7. Nginx资源的跨域访问
  • 8. Nginx资源的防盗链
  • 9. Nginx代理
  • 10. Nginx负载均衡
  • 11. Nginx缓存
  • 12. Nginx动静分离
  • 13. Nginx Rewrite
  • 14. Nginx Secure Link
  • 15. Nginx Geo
  • 16. Nginx HTTPS服务
  • 17. Nginx与Lua开发
  • 18. Nginx与Lua灰度发布
  • 19. Nginx常见错误
  • 20. Nginx性能优化
  • 21. Nginx安全管理

ab测试nginx Nginx性能优化

ab测试nginx Nginx性能优化

 转自:https://www.cnblogs.com/nulige/p/9369700.html

1.性能优化概述

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

  • 1.当前系统结构瓶颈
    • 观察指标
    • 压力测试
  • 2.了解业务模式
    • 接口业务类型
    • 系统层次化结构
  • 3.性能与安全
    • 性能好安全弱
    • 安全好性能低

2.压力测试工具

1.安装压力测试工具ab

[root@nginx-lua ~]# yum install httpd-tools -y

2.了解压测工具使用方式

[root@nginx-lua ~]# ab -n 200 -c 2 http://127.0.0.1/ //-n总的请求次数 //-c并发请求数 //-k是否开启长连接 

3.配置Nginx静态网站与tomcat动态网站环境

[root@nginx-lua conf.d]# cat jsp.conf 
server {
        server_name localhost;
        listen 80;
        location / {
                root /soft/code;
                try_files $uri @java_page;
                index index.jsp index.html;
        }
        location @java_page{
                proxy_pass http://192.168.56.20:8080; } } //分别给Nginx准备静态网站 [root@nginx-lua ~]# cat /soft/code/bgx.html <h1> Ab Load </h1> //给Tomcat准备静态网站文件 [root@tomcat-node1-20 ROOT]# cat /soft/tomcat-8080/webapps/ROOT/bgx.html <h1> Ab Load </h1> 

4.使用ab工具进行压力测试

//进行压力测试
[root@Nginx conf.d]# ab -n2000 -c2 http://127.0.0.1/bgx.html ... Server Software: nginx/1.12.2 Server Hostname: 127.0.0.1 Server Port: 80 Document Path: /bgx.html Document Length: 19 bytes Concurrency Level: 200 # 总花费总时长 Time taken for tests: 1.013 seconds # 总请求数 Complete requests: 2000 # 请求失败数 Failed requests: 0 Write errors: 0 Total transferred: 510000 bytes HTML transferred: 38000 bytes # 每秒多少请求/s(总请求出/总共完成的时间) Requests per second: 9333.23 [#/sec] (mean) # 客户端访问服务端, 单个请求所需花费的时间 Time per request: 101.315 [ms] (mean) # 服务端处理请求的时间 Time per request: 0.507 [ms] (mean, across all concurrent requests) # 判断网络传输速率, 观察网络是否存在瓶颈 Transfer rate: 491.58 [Kbytes/sec] received 

5.将nginx下的bgx文件移走, 再次压测会由tomcat进行处理

Concurrency Level:      200
Time taken for tests: 1.028 seconds Complete requests: 2000 Failed requests: 0 Write errors: 0 Total transferred: 510000 bytes HTML transferred: 38000 bytes Requests per second: 1945.09 [#/sec] (mean) Time per request: 102.823 [ms] (mean) Time per request: 0.514 [ms] (mean, across all concurrent requests) Transfer rate: 484.37 [Kbytes/sec] received 

3.影响性能指标

影响性能方便整体关注

  • 1.网络
    • 网络的流量
    • 网络是否丢包
    • 这些会影响http的请求与调用
  • 2.系统
    • 硬件有没有磁盘损坏,磁盘速率
    • 系统负载、内存、系统稳定性
  • 3.服务
    • 连接优化、请求优化
    • 根据业务形态做对应的服务设置
  • 4.程序
    • 接口性能
    • 处理速度
    • 程序执行效率
  • 5.数据库

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

4.系统性能优化

  • 文件句柄, Linux一切皆文件,文件句柄可以理解为就是一个索引
    • 文件句柄会随着我们进程的调用频繁增加
    • 系统默认对文件句柄有限制,不能让一个进程无限的调用
    • 需要限制每个进程和每个服务使用多大的文件句柄
    • 文件句柄是必须要调整的优化参数
  • 设置方式
    • 系统全局性修改
    • 用户局部性修改
    • 进程局部性修改

vim /etc/security/limits.conf //针对root用户 root soft nofile 65535 root hard nofile 65535 //所有用户, 全局 * soft nofile 25535 * hard nofile 25535 //对于Nginx进程 worker_rlimit_nofile 65535; //root用户 //soft提醒 //hard限制 //nofile文件数配置项 //65535最大大小

 备注:可以使用ulimit -a 命令查看open files  65535 系统最大打开文件数的值。

 

5.Nginx性能优化

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

1.查看当前CPU物理状态

[root@nginx ~] # lscpu |grep "CPU(s)"
CPU(s):                24
On-line CPU(s) list:   0-23
NUMA node0 CPU(s):     0,2,4,6,8,10,12,14,16,18,20,22
NUMA node1 CPU(s):     1,3,5,7,9,11,13,15,17,19,21,23
//2颗物理cpu,没颗cpu12核心, 总共24核心 

2.将Nginx worker进程绑到不同的核心上

// 启动多少worker进程, 官方建议和cpu核心一致, 第一种绑定组合方式
#worker_processes 24;
#worker_cpu_affinity 000000000001 000000000010 000000000100 000000001000 000000010000 000000100000 000001000000 000010000000 000100000000 001000000000 010000000000 10000000000;
 
// 第二种方式
#worker_processes 2;
#worker_cpu_affinity 101010101010 010101010101;
 
// 最佳方式绑定方式
worker_processes auto;
worker_cpu_affinity auto;

3.查看nginx worker进程绑定至对应cpu

ps -eo pid,args,psr|grep [n]ginx 

4.Nginx通用优化配置文件

[root@nginx ~] # cat nginx.conf
user nginx;
worker_processes auto;
worker_cpu_affinity auto;
 
error_log /var/log/nginx/error .log warn;
pid /run/nginx .pid;
#调整至1w以上,负荷较高建议2-3w以上
worker_rlimit_nofile 65535;
 
events {
     use epoll;
#限制每个进程能处理多少个连接请求,10240x16
     worker_connections 10240;
}
 
http {
     include             /etc/nginx/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  /var/log/nginx/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 /etc/nginx/conf .d/*.conf;
}

CentOS 6.3下nginx、php-fpm、drupal快速部署

CentOS 6.3下nginx、php-fpm、drupal快速部署

一切本着从简原则来做,能yum/rpm的,坚决不手工编译 :) 本次部署环境基于CentOS 6.3 x86_64系统。 0. 准备工作 #更新yum[root@imysql ~]# yum -y update[root@imysql ~]# yum install libaio-devel.x86_64#drupal 8.0需要用到curl模块[root@imysql ~]# yum

一切本着从简原则来做,能yum/rpm的,坚决不手工编译 :)
本次部署环境基于CentOS 6.3 x86_64系统。
0. 准备工作

#更新yum
[root@imysql ~]# yum -y update
[root@imysql ~]# yum install libaio-devel.x86_64
#drupal 8.0需要用到curl模块
[root@imysql ~]# yum install curl-devel
[root@imysql ~]# yum -y install libpng-devel libjpeg-devel freetype-devel gmp-devel libxml2-devel
登录后复制

1. 安装nginx

#安装nginx官方yum源包
[root@imysql ~]# rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
[root@imysql ~]# yum -y install nginx
[root@imysql ~]# chkconfig nginx on
登录后复制

2. 安装php-fpm

#安装php yum源包
[root@imysql ~]# rpm -ivh rpm -Uvh http://repo.webtatic.com/yum/el6/latest.rpm
[root@imysql ~]# yum -y install php54w
登录后复制

3. 配置nginx+php
/etc/nginx/nginx.conf 配置文件可以不用做任何修改。
编辑 /etc/nginx/conf.d/default.conf,以本站为例,配置文件如下:

server {
    listen       80;
    server_name  imysql.com *.imysql.com;
    root   /data/www/imysql.cn/;
    index index.php index.htm index.html index.shtml;
    error_page  404               /page_not_found;
    error_page   500 502 503 504  /page_not_found;
    location ~ /\.ht {
        deny  all;
    }
    if ($fastcgi_script_name ~ \..*\/.*php) {
        return 403;
    }
    location / {
        if (!-e $request_filename) {
            rewrite ^/(.*)$ /index.php?q=$1 last;
        }
    }
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
        expires 30d;
    }
    location ~ .*\.(js|css)?$ {
        expires 1h;
    }
    location ^~ /sites/default/files/imagecache/ {
        index index.php index.html;
        if (!-e $request_filename) {
            rewrite ^/(.*)$ /index.php?q=$1 last; break;
        }
    }
}
登录后复制

上述配置包括了nginx虚拟主机的配置,以及drupal的rewrite规则配置,简单快速。

4. 启动测试
每次修改完配置文件后,都记得执行下面的命令测试配置文件正确性:

[root@imysql ~]# /etc/init.d/nginx configtest
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
登录后复制

确认配置文件无误后,执行下面的命令重载nginx,使其生效:

[root@imysql ~]# /etc/init.d/nginx reload
#或者restart
[root@imysql ~]# /etc/init.d/nginx restart
登录后复制

大功告成 :)

原文地址:CentOS 6.3下nginx、php-fpm、drupal快速部署, 感谢原作者分享。

CentOS 6.5下Nginx 编译安装

CentOS 6.5下Nginx 编译安装

编译安装默认值

nginx源码下载到 /data/src目录
nginx 安装目录 /data/soft/nginx

下载nginx最新安装包,安装依赖,创建nginx用户

sh yum install -y pcre-devel libmxl2-devel libxslt-devel gd-devel
 groupadd www
 useradd -c nginx-user -g www -M nginx

编译参数

sh ./configure --prefix=/data/soft/nginx\
  --user=nginx\
  --group=www\
  --with-http_ssl_module\
  --with-http_spdy_module\
  --with-http_realip_module\
  --with-http_addition_module\
  --with-http_xslt_module\
  --with-http_image_filter_module\
  --with-http_sub_module\
  --with-http_auth_request_module\
  --with-http_stub_status_module\
  --with-http_gzip_static_module

编译安装之后

chown -R nginx:www nginx

vim /etc/init.d/nginx 创建自动启动脚本
自动启动脚本模板 https://gist.github.com/luxixing/6163492
修改模板shell脚本 nginx 和 NGINX_CONF_FILE内容

sh chmod +x /etc/init.d/nginx
 chkconfig nginx on
 service nginx start

关于CentOS 6.3下Nginx性能优化设置nginx性能调优的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于20. Nginx性能优化、ab测试nginx Nginx性能优化、CentOS 6.3下nginx、php-fpm、drupal快速部署、CentOS 6.5下Nginx 编译安装等相关知识的信息别忘了在本站进行查找喔。

本文标签: