10K
乃至 1000K
以上单机并发连接的高性能 Web 应用系统。yum install pcre-devel openssl-devel gcc curl
#开发库依赖包
sudo yum install yum-utils
sudo yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
#添加repo库
yum install openresty
#安装软件包
下载和编译安装
wget -c https://openresty.org/download/openresty-1.13.6.2.tar.gz
tar zxvf openresty-*.gz
cd openresty-*
./configure --with-http_stub_status_module --with-http_v2_module --with-http_realip_module
make && make install
快捷方式
ln -s /usr/local/openresty/nginx /usr/local/nginx
ln -s /usr/local/nginx/sbin/nginx /bin/
#nginx快捷方式
检查
ldd $(which /usr/local/nginx/sbin/nginx)
#查看lib文件
配置文件
user www;
worker_processes 2; #cpu 核数
events {
worker_connections 1024; #数值根据压测调优
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '{"timestamp":"$time_iso8601",'
'"remote_addr":"$remote_addr",'
'"remote_user":"$remote_user",'
'"time_local":"$time_local",'
'"request":"$request",'
'"response_time":$request_time,'
'"status":"$status",'
'"body_bytes_sent":"$body_bytes_sent",'
'"http_referer": "$http_referer",'
'"http_user_agent":"$http_user_agent",'
'"http_x_forwarded_for":"$http_x_forwarded_for",'
'"server_name":"$server_name",'
'"upstream_addr":"$upstream_addr",'
'"upstream_response_time":"$upstream_response_time",'
'"upstream_status":"$upstream_status"'
'}';
access_log access.log main; #默认日志路径
server_tokens off; #隐藏版本号
charset utf-8; # 编码默认utf-8
client_max_body_size 200m; #内容长度相关
client_header_buffer_size 512k;
large_client_header_buffers 4 512k;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
types_hash_max_size 2048;
keepalive_timeout 65;
client_header_timeout 30;
client_body_timeout 30;
proxy_ignore_client_abort on;
# 499解决,不主动关闭客户端连接。
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_comp_level 4;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript image/jpeg image/gif image/png font/ttf font/otf image/svg+xml;
gzip_vary on;
# lua_waf
lua_shared_dict limit 50m;
lua_shared_dict blackip 50m;
lua_package_path "/usr/local/nginx/conf/waf/?.lua";
init_by_lua_file /usr/local/nginx/conf/waf/init.lua;
access_by_lua_file /usr/local/nginx/conf/waf/access.lua;
# nginx status
server {
listen 18118;
server_name 127.0.0.1;
location /ngx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
}
include /usr/local/nginx/conf/vhosts/*.conf;#配置文件存放区域
}
vhosts null.conf
server {
listen 80 default_server;
server_name _;
return 444;
access_log /usr/local/nginx/logs/default.log;
}
#禁止ip直接访问
初始配置
mkdir /usr/local/nginx/conf/vhosts/
mkdir /www
groupadd www
#添加www组
useradd -M -s /sbin/nologin -g www -d /www www
#添加www用户
chmod 770 /www && chown -R www:www /www
启动/测试/载入
nginx #启动nginx
nginx -t #检测配置文件
nginx -s reload #载入修改配置
/etc/logrotate.d/openresty
/usr/local/openresty/nginx/logs/*log {
daily
missingok
rotate 7
notifempty
sharedscripts
postrotate
[ ! -f /usr/local/openresty/nginx/logs/nginx.pid ] || kill -USR1 `cat /usr/local/openresty/nginx/logs/nginx.pid`
endscript
}
更新logrotate配置
logrotate /etc/logrotate.conf
WAF - SLB - NGINX (https网站基于安全)
WAF - 配置ssl证书 - 跳转443
SLB - tcp 负载80
web服务 - tcp 监听80 (不用配置证书)
Nginx Server 配置
upstream pay_cluster{
server 172.16.xxx.1:7000;
server 172.16.xxx.2:7000;
}
server {
listen 80;
server_name pay.xxx.net;
location / {
proxy_pass http://pay_cluster;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
}
}