Nginx 安装及调优

下载:nginx
http://nginx.org/en/download.html
Nginx 安装及调优
上传服务器
tar -zxvf nginx-1.15.8
cd nginx-1.15.8

安装组件包
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
./configure
make
make install

Nginx启动脚本
vi /etc/rc.d/init.d/nginx

#! /bin/sh
Default-Start: 2 3 4 5
Default-Stop: 0 1 6
Short-Description: starts the nginx web server

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

set -e
[ -x "$DAEMON" ] || exit 0

do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}

do_stop() {
kill -INT cat $PIDFILE || echo -n "nginx not running"
}

do_reload() {
kill -HUP cat $PIDFILE || echo -n "nginx can't reload"
}

case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}">&2
exit 3
;;
esac

exit 0

chmod u+x /etc/rc.d/init.d/nginx

查看CPU核数
cat /proc/cpuinfo| grep "cpu cores"| uniq
查看分页大小
getconf PAGESIZE

简单调优
vi /usr/local/nginx/conf/nginx.conf
worker_processes 4; # 指定nginx要开启的子进程数量(根据CPU核数)
worker_cpu_affinity 00000001 00000010 00000100 00001000; #为每个进程分配cpu
worker_rlimit_nofile 65535; #指当nginx进程打开的最多文件描述符数目,ulimit
-n的值保持一致
events {
use epoll; #使用epoll的I/O模型
worker_connections 102400; #每个进程允许的最多连接数,理论上每台 nginx 服务器的最大连接数为worker_processes*worker_connections
}
http {
keepalive_timeout 60; # keepalive超时时间
client_header_buffer_size 4k; #设置分页大小
open_file_cache max=65535 inactive=20s; #将打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive 是指经过多长时间文件没被请求后删除缓存。
open_file_cache_valid 30s; #指多长时间检查一次缓存的有效信息。
open_file_cache_min_uses 1; #open_file_cache指令中的inactive 参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如果有一个文件在inactive 时间内一次没被使用,它将被移除。
}
Nginx 安装及调优

猜你喜欢

转载自blog.51cto.com/zhizhimao/2347864
今日推荐