1.下载nginx安装包和依赖包
wget http://nginx.org/download/nginx-1.15.9.tar.gz
wget http://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz
wget http://www.zlib.net/fossils/zlib-1.2.11.tar.gz
2.解压依赖包
tar -xzvf pcre-8.43.tar.gz -C /usr/local/myapps
tar -xzvf zlib-1.2.11.tar.gz -C /usr/local/myapps
3.解压nginx安装包
tar -xzvf nginx-1.15.9.tar.gz
4.安装编译依赖环境
yum -y install gcc-c++
5.安装nginx
cd nginx-1.15.9/
./configure --prefix=/usr/local/myapps/nginx --sbin-path=/usr/sbin/nginx --pid-path=/usr/local/myapps/nginx/logs/nginx.pid --error-log-path=/usr/local/myapps/nginx/logs/error.log --http-log-path=/usr/local/myapps/nginx/logs/access.log --with-pcre=/usr/local/myapps/pcre-8.43 --with-zlib=/usr/local/myapps/zlib-1.2.11 --with-http_stub_status_module --with-stream
make && make install
参数说明:
--with-http_stub_status_module 加载性能统计模块
--with-stream 加载四层负载均衡支持模块
6.验证
nginx -V
7.创建nginx运行用户
groupadd www
useradd -M -g www -s /sbin/nologin www
8.编写初始配置文件
mv /usr/local/myapps/nginx/conf/nginx.conf /usr/local/myapps/nginx/conf/nginx.conf_bak
mkdir /usr/local/myapps/nginx/conf/vhosts/
cat << EOF >/usr/local/myapps/nginx/conf/nginx.conf
user www www;
worker_processes 1;
events {
worker_connections 1024;
}
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';
sendfile on;
keepalive_timeout 65;
include /usr/local/myapps/nginx/conf/vhosts/http*.conf;
}
stream {
include /usr/local/myapps/nginx/conf/vhosts/tcp*.conf;
}
EOF
9.检查配置文件的正确性
nginx -t
10.启动nginx
nginx -c /usr/local/myapps/nginx/conf/nginx.conf
11.停止nginx
ps -ef | grep nginx | grep -v grep | awk '{print $2}' |xargs kill -9
12.重载配置文件
nginx -s reload
nginx调优配置
重启nginx生效
ps -ef | grep nginx | grep -v grep | awk '{print $2}' |xargs kill -9
nginx -c /usr/local/myapps/nginx/conf/nginx.conf
1.查看正在运行的nginx进程的ulimit资源限制
nginxpid=`ps -ef | grep nginx | awk '{if($3 == 1) print $2}'`
cat /proc/${nginxpid}/limits
2.配置任意用户最大可打开的文件句柄数
cat << EOF >> /etc/security/limits.conf
* soft nofile 204800
* hard nofile 204800
EOF
3.配置任意用户最大可运行的线程数
sed -i 's/^*/#&/' /etc/security/limits.d/20-nproc.conf
cat << EOF >> /etc/security/limits.d/20-nproc.conf
* soft nproc 204800
* hard nproc 204800
EOF
4.nginx打开的进程数(与CPU核数一致)
worker_processes 8;
5.配置nginx打开的最多文件描述符数目(与ulimit -n的值保持一致)
worker_rlimit_nofile 204800;
6.使用epoll的I/O模型
use epoll;
7.配置每个进程允许的最多连接数
worker_connections 204800;
8.配置每个TCP连接请求处理完后保持不断开的最长时间
#有些浏览器最多只保持60秒,所以可以设定为60秒
keepalive_timeout 60s;
9.根据系统分页大小来设置每个客户端请求头部占用的缓冲区大小
#系统分页大小可以用命令getconf PAGESIZE取得
client_header_buffer_size 4k;
10.为打开的文件启用缓存
#缓存数量配置与(ulimit -n)一致,设置经过20s缓存文件没被再次请求就删除。
open_file_cache max=204800 inactive=20s;
open_file_cache_min_uses 1;
11.配置30s启动一次对缓存文件的有效性检查
open_file_cache_valid 30s;
一个简单的nginx优化配置文件内容
user www www;
worker_processes 8;
error_log logs/error.log crit;
pid /usr/local/myapps/nginx/nginx.pid;
worker_rlimit_nofile 204800;events
{
use epoll;
worker_connections 204800;
}
http
{
include mime.types;
default_type application/octet-stream;
charset utf-8;
client_header_buffer_size 4k;
sendfile on;
keepalive_timeout 60s;
open_file_cache max=204800 inactive=20s;
open_file_cache_min_uses 1;
open_file_cache_valid 30s;
fastcgi_cache_path /usr/local/myapps/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 16k;
fastcgi_buffers 16 16k;
fastcgi_busy_buffers_size 16k;
fastcgi_temp_file_write_size 16k;
fastcgi_cache TEST;
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;
fastcgi_cache_min_uses 1;
fastcgi_cache_use_stale error timeout invalid_header http_500;log_format access '$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 access;
include /usr/local/myapps/nginx/conf/vhosts/http*.conf;
}
stream {
include /usr/local/myapps/nginx/conf/vhosts/tcp*.conf;
}