OpenResty 是以 Nginx 为核心的 Web 开发平台,可以解析执行 Lua 脚本,方便集成开发。
- 下载安装包
wget https://openresty.org/download/openresty-1.21.4.2rc1.tar.gz
-修改源码
进行安全的配置
tar xvf openresty-1.21.4.2rc1.tar.gz
cd openresty-1.21.4.2rc1/bundle/nginx-1.21.4
# 1.隐藏版本 vim src/core/nginx.h
#define NGINX_VERSION "888"
#define NGINX_VER "FW/" NGINX_VERSION ".8"
#define NGINX_VAR "FW"
# 2.修改头部
vim src/http/ngx_http_header_filter_module.c
#static u_char ngx_http_server_string[] = "Server: FW" CRLF;
# 3.修改错误页响应头部(response header)
vim src/http/ngx_http_special_response.c
# "<hr><center>\"FW\"</center>" CRLF
# ...
# "<hr><center>\"FW\"</center>" CRLF
# ...
# "<hr><center>FW</center>" CRLF
-添加第三方模块
-
动态配置 upstream 的模块
nginx_upstream_check_module
git clone https://github.com/yzprofile/ngx_http_dyups_module.git
-
upstream 监控检查模块
nginx_upstream_check_module
git clone https://github.com/yaoweibin/nginx_upstream_check_module.git
-
监控模块
nginx-module-vts
git clone https://github.com/vozlt/nginx-module-vts.git
-
openssl
openssl-1.1.1t
wget https://www.openssl.org/source/openssl-1.1.1t.tar.gz
-编译之前打上补丁
yum install patch
cd openresty-1.21.4.2rc1/bundle/nginx-1.21.4
patch -p1 < /opt/nginx_upstream_check_module/check_1.20.1+.patch
-编译安装
./configure --prefix=/opt/openresty --with-http_realip_module --with-http_v2_module --with-http_image_filter_module --with-http_iconv_module --with-stream_realip_module --with-stream --with-stream_ssl_module --with-stream_geoip_module --with-http_slice_module --with-http_sub_module --add-module=/opt/ngx_http_dyups_module --add-module=/opt/nginx_upstream_check_module --with-http_stub_status_module --with-http_geoip_module --with-http_gzip_static_module --add-module=/opt/nginx-module-vts --with-openssl=/opt/openssl-1.1.1t
make
make install
-安全配置
-
关闭 nginx 版本号显示
http {
server_tokens off
....
-
控制资源和限制
http {
client_body_buffer_size 1K;
client_header_buffer_size 1k;
client_max_body_size 1k;
large_client_header_buffers 2 1k;
client_body_timeout 10;
client_header_timeout 10;
keepalive_timeout 5 5;
send_timeout 10;
...
-
禁用所有不需要的 HTTP 方法
location / {
limit_except GET HEAD POST {
deny all;
}
}
-默认 server
匹配不到 server 时会跳到该默认 server,并返回403.
server {
listen 80 default;
server_name _;
location / {
return 403;
}
}
控制并发连接数
http {
limit_conn_zone $binary_remote_addr zone=limit1:10m;
limit_conn_zone $server_name zone=limit2:10m;
server {
listen 80;
server_name example.com;
root /data/webapp;
index index.html;
location / {
limit_conn limit1 10;
limit_conn limit2 1000;
}
}
}
不仅会限制同一IP同一时间的连接数为 10,也会限制虚拟服务的总连接数为 1000