返回目录:网站建设-绪论
因为nginx自带的openssl版本太低,所以我们需要自己编译新版的openssl到nginx中,同时加入我们需要的模块
其实openssl没必要纠结,用1.0.2版本的也可,openssl也在更新1.0.2
2017年11月28日更新
推荐2个模块:
Echo --add-module=../echo-nginx-module-0.61
limit_conn_zone 和 limit_req(默认内置)
可以了解两个模块:
--add-module=../nginx-http-sysguard-master
--with-google_perftools_module
echo
吸引我的一点是对于404页面可以更快地进行中转,例如:
error_page 404 /404home.html;
location /404home.html {echo '<meta http-equiv="refresh" content="0;url=https://www.xinroom.cn/4044.html">';}
之所以404要这样做,是为了正确返回404并减少服务器消耗和非法攻击。
echo一大特点是从内存读取的,显然比从硬盘读取快很多。
nginx-http-sysguard
如图,第一个峰点是没有启用,如果启用,当服务器占用高到一定程度时,返回503,目的么,使服务器在出了一身汗后迅速重新投入工作
google_perftools
是一个针对内存的分配优化模块,需要安装google_perftools库,但对于一般小型服务器也用不到,因为,宝宝没钱啊,只有一个nginx线程啊~
limit_conn_zone 和 limit_req
汉字版文档可以看淘宝译ngx_http_limit_conn_module和ngx_http_limit_req_module
limit_conn_zone $clientRealIp zone=ConnLimit:10m; //以$clientRealIp为key,key空间10m(用来存储ip等信息)
limit_conn ConnLimit 20; //单ip20链接数
limit_req_zone $clientRealIp zone=ReqLimitZone:10m rate=10r/s; //单ip并发数10每秒
limit_req zone=ReqLimitZone burst=5 nodelay; //允许超过频率限制的请求数不多于5个nodelay为超过直接返回503,而不是等待
由于我网站动静分离,所以我设置的值都比较小(建议以实际效果为准)
附:
$clientRealIp
(用于使用了中间层的服务器)
map $http_x_forwarded_for $clientRealIp {
"" $remote_addr;
~^(?P<firstAddr>[0-9\.]+),?.*$ $firstAddr;
}
第一步、安装yum版nginx
yum install nginx
//同时请确认安装的是1.12.1版本及以上,应该和之后下载的nginx源码版一致
第二步、下载nginx、openssl、ngx_cache_purge的源码并解压
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
wget http://nginx.org/download/nginx-1.12.1.tar.gz
wegt https://www.openssl.org/source/openssl-1.1.0f.tar.gz
tar -xzvf ngx_cache_purge-2.3.tar.gz
tar -xzvf nginx-1.12.1.tar.gz
tar -xzvf openssl-1.1.0f.tar.gz
注意
如果wget速度很慢!请直接下载到电脑上,再上传到服务器/root目录
有些教程还会下载zlib、pcre的源码,这里不用!
第三步、安装gcc等c、c++编译库并编译nginx
首先请确认你已按文章 第二部分之服务器基础配置和yum 配置了nginx官方的新版本源
//这一步很重要
yum updata //更新系统!!
yum -y install zlib-devel pcre-devel gcc gcc-c++ automake autoconf libtool make //编译库
现在开始配置编译参数
cd nginx-1.12.1 //从root目录进入nginx源码目录
./configure --prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio \
--with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module \
--with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module \
--with-http_secure_link_module --with-http_slice_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail \
--with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module \
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' \
--with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' \
--with-http_ssl_module --with-openssl=../openssl-1.1.0f --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-module=../ngx_cache_purge-2.3
/*其中关键代码
--with-http_ssl_module --with-openssl=../openssl-1.1.0f --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-module=../ngx_cache_purge-2.3
*/
现在开始编译
make
现在替换原来yum安装的的nginx-1.12.1的二进制文件
mv /usr/sbin/nginx /usr/sbin/nginx.old
cp objs/nginx /usr/sbin/nginx
systemctl start nginx //启动
systemctl enable nginx //设置开机启动
第四步、配置/etc/nginx.conf
events {
worker_connections 1024;
use epoll; #可以不用添加,默认如此
}
http {
#gzip配置
gzip on;
gzip_disable "msie6";
gzip_http_version 1.0; #针对cdn
gzip_min_length 1024;
gzip_comp_level 5; #不宜过高(0-9)
gzip_proxied expired no-cache no-store private auth;
#压缩类型,针对rss等xml格式的压缩js、css等,未添加png、jpg等图片格式的支持!
gzip_types text/plain text/xml application/xml application/rss+xml application/javascript text/css application/x-javascript image/svg+xml image/x-icon;
open_file_cache max=100000 inactive=30s;
open_file_cache_valid 60s;
open_file_cache_min_uses 2;
open_file_cache_errors off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
server_tokens off; #安全
keepalive_timeout 60;
fastcgi_intercept_errors on;
reset_timedout_connection on;
limit_conn_zone $clientRealIp zone=ConnLimit:10m;
limit_conn ConnLimit 10;
limit_req_zone $clientRealIp zone=ConnLimitZone:10m rate=10r/s; #自行动态调整
limit_req zone=ConnLimitZone burst=5 nodelay;
#防止直接用ip或不在列表内的host访问(同时避免他人直接cname你得域名)
server {
listen 80 default;
return 444;
#rewrite ^(.*) https://xinroom.cn;
}
server {
listen 80;
listen 443 ssl http2 fastopen=3 reuseport;
#ssl on; #80,443共存
ssl_certificate /......crt;
ssl_certificate_key /.........key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:ALL:!MD5:!aNULL:!eNULL:!NULL:!DH:!RC4:!LOW:!EXP:!DSS:!PSK:!SRP:!CAMELLIA:!SEED:!EDH:!kEDH:!kECDH;
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
root /.../...;
index index.php index.html index.htm;
server_name www.xinroom.cn;
}
}
这篇文章用时比较多了,因为我所有命令都是现场测试的,中途编译nginx时出现了几次错误
实验环境:腾讯云开发者实验室CentOS 7.2 64 位
更新于:2017-12-16
作者:xinroom 欢迎分享本文,转载请保留出处
本文地址:https://www.xinroom.cn/nginx-openssl.html