一、安装gcc环境、perl库、zlib、openssl等
#nginx编译依赖gcc环境
yum install -y gcc-c++
#pcre:(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式
yum install -y pcre pcre-devel
#该库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip
yum install -y zlib zlib-devel
#一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。nginx不仅支持http协议,还支持https(即在ssl协议上传输http)
yum install -y openssl openssl-devel
#重启
reboot
二、官网下载最新的nginx源码包nginx-1.14.2.tar.gz, 上传至Linux服务器中的/usr/local文件下。并进行解压源码后进入该文件夹
tar -zxf nginx-1.14.2.tar.gz
cd nginx-1.14.2
三、编译Nginx
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make
make install
四、检查安装有效性
#显示安装Nginx的路径, 会发现安装目录是/usr/local/nginx
whereis nginx
cd /usr/local/nginx/
五、编辑nginx的配置文件,进行优化,参考优化配置文件如下
#编辑nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
#查看每个物理CPU中core的个数(即核数)
cat /proc/cpuinfo| grep "cpu cores"| uniq
#查看逻辑CPU的个数
cat /proc/cpuinfo| grep "processor"| wc -l
#参考如下:
user root;
worker_processes 4;
worker_rlimit_nofile 102400;
events {
use epoll;
worker_connections 65535;
multi_accept on;
}
http {
access_log off;
error_log /dev/null;
server_tokens off;
open_file_cache max=102400 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;
reset_timedout_connection on;
gzip on;
gzip_min_length 2k;
gzip_buffers 4 32k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain text/javascript text/xml text/css application/json application/javascript application/x-javascript application/xml;
gzip_disable "MSIE [1-6]\."
gzip_vary on;
gzip_proxied any;
tcp_nopush on;
tcp_nodelay on;
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 16;
proxy_connect_timeout 8s;
proxy_send_timeout 8s;
proxy_read_timeout 8s;
send_timeout 3s;
upstream {name} {
server 192.168.1.0:9001 max_fails=5 fail_timeout=30s weight=1;
server 192.168.1.1:9001 max_fails=5 fail_timeout=30s weight=1;
server 192.168.1.2:9001 max_fails=5 fail_timeout=30s weight=1;
}
server {
listen 80;
listen 443 ssl;
server_name localhost;
ssl_certificate cert/214600202240691.pem;
ssl_certificate_key cert/214600202240691.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
proxy_set_header host $host;
proxy_set_header X-forwarded-for $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
location / {
proxy_pass http://{name};
}
}
server {
listen 8080;
server_name localhost;
location / {
root {path};
}
}
}
六、验证Nginx配置文件的有效性
/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
#如果出现如下提示, 则表示配置文件有效。
#nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
#nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
七、进入nginx的sbin目录下, 并(启动|重新加载|退出)Nginx,
cd /usr/local/nginx/sbin
#启动Nginx
./nginx -c /usr/local/nginx/conf/nginx.conf
#停止Nginx
./nginx -s quit
#重新加载配置文件
./nginx -s reload
附录:
一、Nginx的日志按照天切割
vim split_nginx_log.sh
#!/bin/bash
LOG_PATH=/usr/local/nginx/logs/
PID=/usr/local/nginx/logs/nginx.pid
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)
mv ${LOG_PATH}access.log ${LOG_PATH}access-${YESTERDAY}.log
mv ${LOG_PATH}error.log ${LOG_PATH}error-${YESTERDAY}.log
kill -USR1 `cat ${PID}`
chmod +x ./split_nginx_log.sh
#编辑定时任务
crontab -e
#输入任务脚本
00 00 * * * /bin/bash /usr/local/nginx/split_nginx_log.sh
二、Nginx常用日志查询
awk '{print $7}' access.log|sort | uniq -c |sort -n -k 1 -r|more
awk '{print $1}' access.log |sort |uniq -c|sort -n
三、Nginx配置黑名单
deny 120.27.XX.XX;
include blacklist.conf;
./nginx -s reload