###http_geoip_module模块
需要更换下nginx的yum源
````
vim /etc/yum.repos.d/nginx.repo
````
````
###配置nginx
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
````
###yum安装geoip
````
yum install nginx-module-geoip
yum -y install GeoIP GeoIP-devel GeoIP-data
````
````
###安装nginx
yum install nginx -y
````
####下载geoip dat数据文件
````
地址如下
国家文件: wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
城市文件:wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
````
解压相应文件
````
gunzip GeoIP.dat.gz
gunzip GeoLiteCity.dat.gz
````
###把解压的数据放到/etc/nginx/下
nginx编译版本下载 到官网下载相应的版本解压即可
在现有的版本查看一下版本以及编译的模块 nginx -V
````
[root@localhost ~]# nginx -V
nginx version: nginx/1.14.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/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-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
````
###添加模块进行编译 --with-http_geoip_module
###安装依赖库
````
yum install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel cURL cURL-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel wget gcc-c++ ncurses-devel cmake make perl gcc autoconf automake zlib libxml libgcrypt libtool bison pcre-devel epel-release-6-8.noarch ntp gcc-c++ ncurses-devel cmake make perl gcc autoconf automake zlib libxml libgcrypt libtool bison pcre-devel pcre-devel ntp gcc-c++ ncurses-devel cmake make perl gcc autoconf automake zlib libxml libgcrypt libtool bison sendmail vim gperftools-devel perl-ExtUtils-Embed libxslt-devel gd gd-devel
````
####安装开发工具 补救 落下文件
````
yum groupinstall "Development Libraries" "Development Tools"
````
具体看自己编译的nginx
````
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/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-threads --with-http_addition_module --with-http_geoip_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
````
###编译完成后make一下即可注意千万不要make install否则会覆盖歇菜的 编译好后覆盖原来的nginx 文件同时给上权限
###nginx配置修改
vi /etc/nginx/nginx.conf 在http{}之间添加配置来读取数据
````
http {
geoip_country /etc/nginx/GeoIP.dat;
geoip_city /etc/nginx/GeoLiteCity.dat;
include /etc/nginx/mime.types; ##必须在include上面配置
````
server配置
````
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
if ($geoip_country_code != CN) {
return 403;
}
root /usr/share/nginx/html;
index index.html index.htm;
}
location /myip {
default_type text/plain;
return 200 "$remote_addr $geoip_country_name $geoip_country_code $geoip_city";
}
````
###实例2
````
server {
…
if (geoip_country_code = CN) {
rewrite ^(.*)geoip_country_code = CN) {
rewrite ^(.*) http://baidu.com;
}
if (geoip_country_code != CN) {
rewrite ^(.*)geoip_country_code != CN) {
rewrite ^(.*) http://qq.com;
}
…
}
````
- 第一章:Centos快捷操作
- EPEL 存储库
- 第二章:系统命令
- 第三章:Consul搭建配置
- consul-客户端配置win
- 第四章:Mongodb3.20搭建配置
- 1、mongodb-bak
- 第五章:Centos 7搭建Gitlab服务器
- yum-install
- 第六章:Mysql5.6主从搭建
- 第七张:Vmware-exsi6.5
- 第八章:Jumpserver 堡垒机
- 第九章:OCS Inventory NG-CMDB”
- 第十章:zabbix监控部署
- 第十一章:ELK快速搭建
- 第十二章:docker安装
- 第十三章:rabbitmq集群安装
- 第十四章:Nginx相关配置
- 1、Nginx禁止访问某个目录或某个后缀文件
- 2、Nginx-GeoIP部署
- 3、Nginx的lua-nginx-module模块
- 4、lua-resty-redis
- 5、nginx删除不安全的请求头
- 6、Nginx支持HTTPS并且支持反爬虫
- 7、nginx配置location总结及rewrite规则写法
- 7、nginx安装和监控
- 第十五章:racktables资产管理
- 第十六章:缓存服务安装
- memcache
- 第十七章:自动化管理
- ansible-playbook
- ansible-playbook-firewalld
- 第十八章:redis集群部署
- 第十九章:yapi配置ldap
- yapi
- 第二十章:winlogbeat
- win配置winlogbeat
