评论

收藏

[Linux] Openresty+lua+GeoIP编译配置部署

服务系统 服务系统 发布于:2021-07-16 15:51 | 阅读数:623 | 评论:0

Openresty+lua+GeoIP编译配置部署
标签(空格分隔): 运维系列
[toc]

一: openresty 简介
1.1 openresty 介绍
OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。
<p>OpenResty® 通过汇聚各种设计精良的 Nginx 模块(主要由 OpenResty 团队自主开发),从而将 Nginx 有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。</p>
<p>OpenResty® 的目标是让你的Web服务直接跑在 Nginx 服务内部,充分利用 Nginx 的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都进行一致的高性能响应。</p>
<p>openresty 中文官网:</p>
<p>http://openresty.org/cn/</p>
二:openresty 的部署安装
2.1 openresty 的编译安装
配置依赖包:
<p>yum install pcre-devel openssl-devel gcc curl zlib-devel readline readline-devel\</p>
<p>readline-devel libxslt-devel gd-devel \</p>
libevent libevent-devel
DSC0000.png
tar -zxvf openresty-1.19.3.1.tar.gz 
cd openresty-1.19.3.1<br><h1>编译安装LuaJIT</h1><br>
<p>cd bundle/LuaJIT-2.1-20201027</p>
<p>make clean &amp;&amp; make &amp;&amp; make install</p>
<br><h1>安装openresty  可根据自己需要启用模块</h1><br>
<p>./configure \</p>
<p>--prefix=/usr/local/openresty \</p>
<p>--http-proxy-temp-path=//usr/local/openresty/proxy_temp \</p>
<p>--http-fastcgi-temp-path=/usr/local/openresty/nginx/fastcgi_temp \</p>
<p>--with-http_ssl_module \</p>
<p>--with-threads \</p>
<p>--with-file-aio \</p>
<p>--with-http_ssl_module \</p>
<p>--with-http_iconv_module \</p>
<p>--with-http_realip_module \</p>
<p>--with-http_gzip_static_module \</p>
<p>--with-http_secure_link_module \</p>
<p>--with-http_stub_status_module \</p>
<p>--with-http_auth_request_module \</p>
<p>--with-http_random_index_module \</p>
<p>--with-http_image_filter_module</p>
<p>make &amp;&amp; make install</p>
DSC0001.png
启动openresty:
<p>cd /usr/local/openresty/nginx/</p>
<p>sbin/nginx</p>
DSC0002.png

DSC0003.png

DSC0004.png

2.2 集成GeoIP2 模块
geoip2核心识别库
<p>下载:libmaxminddb</p>
<p>https://github.com/maxmind/libmaxminddb/releases/tag/1.6.0</p>
<p>下载libmaxminddb-1.6.0.tar.gz</p>
<p>tar -xzf libmaxminddb-1.6.0.tar.gz</p>
<p>cd libmaxminddb-1.6.0</p>
<p>./configure</p>
<p>make</p>
<p>make check</p>
<p>sudo make install</p>
sudo ldconfig
DSC0005.png
geoip2-nginx模块
<p>下载地址:</p>
<p>https://github.com/TravelEngineers/ngx_http_geoip2_module</p>
<p>git clone https://github.com/TravelEngineers/ngx_http_geoip2_module</p>
<p>从新编译openresty 增加 geoip2 模块</p>
<p>cd /root/openresty-1.19.3.1</p>
<p>./configure --prefix=/usr/local/openresty --with-http_stub_status_module  --with-http_realip_module --with-http_gzip_static_module  --add-module=/root/ngx_http_geoip2_module/</p>
make &amp;&amp; make install
DSC0006.png
geoip2 IP地址库下载:
<p>下载地址:https://dev.maxmind.com/geoip/geoip2/geolite2/</p>
<p>注意GeoLite2 City 和GeoLite2 Country 2个文件都要下载。</p>
<p>下载选择:MaxMind DB binary, gzipped</p>
<p>GeoLite2-City.mmdb  GeoLite2-Country.mmdb 文件</p>
<p>mkdir -p /data/softwares/</p>
<p>GeoLite2-City.mmdb  GeoLite2-Country.mmdb 文件 放到/data/softwares/GeoIP 下面</p>
<p># nginx加载使用geoip2数据库</p>
<p>geoip2 /data/softwares/GeoIP/GeoLite2-City.mmdb {</p>
<space40ab65925af6c34846fb1f5af052a892Code 0>
<p>fastcgi_param COUNTRY_CODE $geoip2_data_country_code;</p>
<p>fastcgi_param COUNTRY_NAME $geoip2_data_country_name;</p>
<p>fastcgi_param CITY_NAME  $geoip2_data_city_name;</p>
<p>fastcgi_param PROVINCE_NMAE $geoip2_data_province_name;</p>
}
nginx 配置文件
<p>/usr/local/openresty/nginx/conf</p>
<p>vim nginx.conf</p>
<hr />
<p>user root root;</p>
<p>worker_processes auto;</p>
<p>worker_cpu_affinity auto;</p>
<p>worker_rlimit_nofile 65535;</p>
<p>daemon on;</p>
<p>error_log /usr/local/openresty/nginx/logs/error.log warn;</p>
<p>pid /usr/local/openresty/nginx/pid/nginx.pid;</p>
<p>events {</p>
<p>use epoll;</p>
<p>worker_connections  65535;</p>
}
<br><h1>开启环境变量</h1><br>
<p>env SPRING_PROFILES_ACTIVE=master;</p>
<p>http {</p>
<p># 加载lua库和动态库</p>
<p>lua_package_path  "/usr/local/openresty/lualib/?.lua;;";</p>
<p>lua_package_cpath  "/usr/local/openresty/lualib/?.so;;";</p>
<p>include mime.types;</p>
<p>default_type application/octet-stream;</p>
<p>charset utf-8;</p>
<p>log_format main '$remote_addr - $remote_user [$time_local] '</p>
<space40ab65925af6c34846fb1f5af052a892Code 1>
<p>log_format main1 '$remote_addr|$remote_user|[$time_local]|$request|'</p>
<space40ab65925af6c34846fb1f5af052a892Code 2>
<p>log_format main3 '$http_x_forwarded_for|$remote_user|[$time_local]|$request|'</p>
<space40ab65925af6c34846fb1f5af052a892Code 2>
<p>log_format lua '$remote_addr|$remote_user|[$time_local]|$request|'</p>
<space40ab65925af6c34846fb1f5af052a892Code 4>
<p>log_format  main2  escape=json</p>
<space40ab65925af6c34846fb1f5af052a892Code 5>
<p>#基础优化</p>
<p>server_tokens off;</p>
<p>sendfile on;</p>
<p>tcp_nopush on;</p>
<p>tcp_nodelay on;</p>
<p>keepalive_timeout  65;</p>
<p>keepalive_requests 8192;</p>
<p># gzip</p>
<p>gzip on;</p>
<p>gzip_min_length 1k;</p>
<p>gzip_buffers 4 16k;</p>
<p>gzip_comp_level 3;</p>
<p>gzip_types text/plain application/javascript  text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/x-httpd-php image/jpeg image/gif image/png;</p>
<p>gzip_vary on;</p>
<p>client_body_timeout 300;</p>
<p>client_header_timeout 300;</p>
<p>send_timeout 600;</p>
<p>reset_timedout_connection on;</p>
<p>client_max_body_size 50m;</p>
<p>#client_body_buffer_size 4096k;</p>
<p>client_body_buffer_size 8192k;</p>
<p>#client_header_buffer_size 4k;</p>
<p>#large_client_header_buffers 4 64k;</p>
<p>client_header_buffer_size 16k;</p>
<p>large_client_header_buffers 8 256k;</p>
<p>server_names_hash_bucket_size 512;</p>
<p>proxy_connect_timeout 600;</p>
<p>proxy_read_timeout 600;</p>
<p>proxy_send_timeout 600;</p>
<p>proxy_buffer_size  128k;</p>
<p>proxy_buffers   8 128k;</p>
<p>proxy_busy_buffers_size 256k;</p>
<p>output_buffers 1 32k;</p>
<p>postpone_output 1460;</p>
<p>open_file_cache max=65535 inactive=60s;</p>
<p>open_file_cache_valid  80s;</p>
<p>open_file_cache_min_uses 1;</p>
<p>open_file_cache_errors   on;</p>
<p># fastcgi set</p>
<p>fastcgi_ignore_client_abort     on;</p>
<p>fastcgi_connect_timeout       300;</p>
<p>fastcgi_send_timeout        300;</p>
<p>fastcgi_read_timeout        300;</p>
<p>#fastcgi_buffer_size         4k;</p>
<p>#fastcgi_buffers           8 4k;</p>
<p>#fastcgi_busy_buffers_size     8k;</p>
<p>#fastcgi_temp_file_write_size    8k;</p>
<p>fastcgi_buffer_size         64k;</p>
<p>fastcgi_buffers           4 64k;</p>
<p>fastcgi_busy_buffers_size     128k;</p>
<p>fastcgi_temp_file_write_size    128k;</p>
<p># fastcgi TEST</p>
<p>fastcgi_cache_valid 200 302 1h;</p>
<p>fastcgi_cache_valid 301 1d;</p>
<p>fastcgi_cache_valid any 1m;</p>
<p>fastcgi_cache_min_uses 1;</p>
<p>fastcgi_cache_use_stale error timeout invalid_header http_500;</p>
<p># include /data/conf/nginx/conf.d/<em>.conf;</p>
<p># include /data/conf/nginx/conf.d/private-01/</em>.conf;</p>
<p># include /data/conf/nginx/conf.d/private-12/*.conf;</p>
<p># include /data/conf/nginx/conf.d/private-13/<em>.conf;</p>
<p># 开启缓存LUA代码</p>
<p>lua_code_cache on;</p>
<p># 允许用户自定义请求头</p>
<p>underscores_in_headers on;</p>
<p># include config</p>
<p>include /data/apps/nglua/conf/</em>.conf;</p>
<p># nginx 使用 geoip设置</p>
<p>map $http_x_forwarded_for $real_ip {</p>
<space40ab65925af6c34846fb1f5af052a892Code 6>
<p># nginx加载使用geoip2数据库</p>
<p>geoip2 /data/softwares/GeoIP/GeoLite2-City.mmdb {</p>
<space40ab65925af6c34846fb1f5af052a892Code 0>
<p>fastcgi_param COUNTRY_CODE $geoip2_data_country_code;</p>
<p>fastcgi_param COUNTRY_NAME $geoip2_data_country_name;</p>
<p>fastcgi_param CITY_NAME  $geoip2_data_city_name;</p>
<p>fastcgi_param PROVINCE_NMAE $geoip2_data_province_name;</p>
<p>}
</p>
----
关于启动报错:
<p>sbin/nginx: error while loading shared libraries: libmaxminddb.so.0: cannot open shared object file: No such file or directory</p>
ldd $(which /usr/local/openresty/nginx/sbin/nginx)
DSC0007.png
这个libmaxmind 包编译完成放在了/usr/local/lib/ 下面 需要重新建立软连接到新的
<p>/lib64 下面</p>
<p>cd /usr/local/lib</p>
ln -s /usr/local/lib/libmaxminddb.so.0.0.7 /lib64/libmaxminddb.so.0
DSC0008.png

DSC0009.png
cd /usr/local/openresty/nginx
sbin/nginx -t
DSC00010.png
sbin/nginx - stop 
<p>sbin/nginx</p>
<p>ps -ef |grep nginx</p>
DSC00011.png


关注下面的标签,发现更多相似文章