linux 下安装带有模块http_realip_module,http_image_filter_module=dynamic和http_ssl_module的nginx1.14服务+https的配

本文详细介绍了在Red Hat Enterprise Linux 7.3环境中,如何停用SELinux和firewalld,清理缓存,并编译安装带有http_realip_module、http_image_filter_module=dynamic和http_ssl_module模块的Nginx 1.14服务。此外,还提供了HTTPS配置步骤及浏览器验证方法。

一、实验环境(rhel7.3版本)

 

1selinux和firewalld状态为disabled

2各主机信息如下:

主机ip
server1172.25.8.1

 

二、安装带有模块http_realip_module,http_image_filter_module=dynamic和http_ssl_module的nginx1.14服务

前期准备:

  1. 停掉之前开启的nginx服务
  2. 清空缓存(删除之前安装nginx服务生成的Makefile文件和objs目录)

安装带有模块http_realip_module,http_image_filter_module=dynamic和http_ssl_module的nginx1.14服务

#安装预编译需要的依赖包openssl-devel

#进行预编译

#可以看到生成了Makefile文件和objs目录

#在objs目录下的ngx_modules.c文件中可以查看安装好的模块。但是这里面只能看到http_realip_module模块,而看不到http_image_filter_module模块

#编译

#在该目录下可以看到http_image_filter_module模块

#需要新建一个modules目录,来存放模块http_image_filter_module

#将生成的新的nginx二进制文件拷贝到/usr/local/nginx/sbin/目录下(覆盖之前该目录下的nginx文件)

三、https的配置

#其中的名字随意给

 

浏览器验证:https://www.westos.org

#在物理机上编写本地解析

点击“Add  Exception”

点击“Confirm  Security  Exception”

### Nginx 使用 `ngx_stream_geoip2_module` 模块 #### 安装依赖项 为了成功编译运行 `ngx_stream_geoip2_module`,需要先安装必要的开发工具库文件。这通常包括 MaxMind 的数据库支持库。 ```bash yum install libmaxminddb-devel -y ``` #### 下载并准备模块源码 获取最新的 `ngx_http_geoip2_module` `ngx_stream_geoip2_module` 源代码: ```bash cd /usr/local/src/ git clone https://github.com/leev/ngx_http_geoip2_module.git ``` 注意:虽然这里提到的是 `ngx_http_geoip2_module`,但是 GitHub 上该项目也包含了流模式的支持即 `ngx_stream_geoip2_module`. #### 编译安装 NGINX 并添加自定义模块 在编译 NGINX 之前,确保已经下载了官方发布的稳定版本,并将其解压缩到合适的位置。接着,在置阶段通过命令行参数指定要加入的新功能模块路径。 ```bash tar -zxvf nginx-1.20.1.tar.gz cd nginx-1.20.1/ ./configure \ --with-stream \ --add-dynamic-module=../ngx_http_geoip2_module \ --prefix=/etc/nginx \ --sbin-path=/usr/sbin/nginx \ --modules-path=/usr/lib/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-http_ssl_module \ --with-http_v2_module \ --with-http_realip_module \ --with-http_addition_module \ --with-http_xslt_module=dynamic \ --with-http_image_filter_module=dynamic \ --with-http_geoip_module=dynamic \ --with-http_sub_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_mp4_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_random_index_module \ --with-http_secure_link_module \ --with-http_degradation_module \ --with-http_slice_module \ --with-http_stub_status_module \ --with-mail=static \ --with-mail_ssl_module \ --with-pcre \ --with-pcre-jit \ --with-stream=dynamic \ --with-stream_ssl_module \ --with-stream_realip_module \ --with-stream_geoip_module=dynamic \ --with-stream_ssl_preread_module \ --with-debug make && make install ``` 上述过程会将 `ngx_stream_geoip2_module` 动态加载至 NGINX 中[^3]。 #### 获取 IP 地理位置数据集 (MaxMind DB 文件) 从官方网站或其他可信资源处获得最新版的 GeoLite2 数据包,并按照说明放置于适当位置供 NGINX 访问。 ```bash mkdir -p /etc/nginx/geoip cd /tmp wget https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=YOUR_LICENSE_KEY&suffix=tar.gz tar xf geoip_download\?edition_id\=GeoLite2-City* -C /etc/nginx/geoip --strip-components=1 ``` 请注意替换 URL 参数中的 `YOUR_LICENSE_KEY` 为你自己的许可证密钥[^2]。 #### 修改 NGINX 置以启用 Stream GeoIP 支持 编辑 `/etc/nginx/nginx.conf` 或者相应的虚拟主机置文件来引入新的指令用于处理 TCP/UDP 请求的同时解析客户端的真实地理位置信息。 ```nginx stream { upstream backend { server 192.168.1.1:80; } map $remote_addr $country_code { default "unknown"; include /etc/nginx/geoip/country.map; # 自定义映射表可选 } log_format stream_combined '$remote_addr [$time_local] ' '"$protocol $status" $bytes_sent "$upstream_addr"' ' country=$country_code'; access_log /var/log/nginx/stream-access.log stream_combined; server { listen 12345; proxy_pass backend; geoip2 /etc/nginx/geoip/GeoLite2-City.mmdb { auto_load on; $city city.names.en; $country_code country.iso_code; } set $allowed_countries "CN US CA"; # 允许访问的国家列表 if ($country_code !~* ^($allowed_countries)$ ) { return 503; } } } ``` 此段置实现了基于地理区域限制服务可用性的基本逻辑,仅当请求来自特定几个允许的国家时才转发流量给后端服务器;否则返回 HTTP 503 错误响应表示暂时不可用[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值