安装OpenResty


  1. 安装依赖
1
yum install -y pcre-devel openssl-devel gcc curl make wget geoip geoip-devel
  1. 下载安装包
1
2
mkdir -p /root/source
wget https://openresty.org/download/openresty-1.21.4.1.tar.gz -O /root/source/openresty-1.21.4.1.tar.gz
  1. 解压
1
tar -zxvf /root/source/openresty-1.21.4.1.tar.gz
  1. 安装
1
2
3
4
cd /root/source/openresty-1.21.4.1/
./configure --with-http_realip_module --with-http_geoip_module --with-http_geoip_module=dynamic --with-http_gzip_static_module --with-http_v2_module --with-luajit
make
make install
  1. 配置系统服务
1
vim /usr/lib/systemd/system/openresty.service
1
2
3
4
5
6
7
8
9
10
11
12
13
[Unit]
Description=openresty
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/openresty/nginx/sbin/nginx
ExecReload=/usr/local/openresty/nginx/sbin/nginx -s reload
ExecStop=/usr/local/openresty/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
1
2
systemctl daemon-reload
systemctl start openresty

注意事项:


  1. 修改配置文件隐藏nginx头
1
2
3
4
5
6
7
vim /root/source/openresty-1.21.4.1/bundle/nginx/src/http/ngx_http_header_filter_module.c # 49-50行
内容:
static char ngx_http_server_string[] = "Server: nginx" CRLF;
static char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF
更改为:
static char ngx_http_server_string[] = "Server: X-Web" CRLF;
static char ngx_http_server_full_string[] = "Server:X-Web " CRLF;
  1. 载入外部依赖,必须放在events前面
1
load_module modules/ngx_http_geoip_module.so;
  1. 配置允许访问国家
1
2
3
4
5
geoip_country /usr/share/GeoIP/GeoIP.dat;
map $geoip_country_code $allowd_country {
default no;
CN yes;
}