Tenginx安装及配置

环境:centos 6.8
虚拟服务器IP :192.168.1.203
本机windows ip:192.168.1.249
另一台windows ip :192.168.1.135

一.tenginx安装

安装yum依赖

yum install -y gcc gcc-c++ zlib zlib-devel openssl openssl-devel perl make elinks vim wget openssh-clients

下载安装包

wget http://tengine.taobao.org/download/tengine-2.0.3.tar.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz
wget
pcre下载地址:https://ftp.pcre.org/pub/pcre/

编译GeoIP library

gunzip GeoIP.tar.gz && tar -xvf GeoIP.tar && cd GeoIP-1.4.8
./configure && make && make install

解压pcre包

tar -zxf pcre-8.35.tar.gz
cd pcre-8.35
pwd
/root/pcre-8.35

解压tenginx包

tar -zxf tengine-2.2.2.tar.gz

编译安装

cd tenginx-2.2.2.
./configure --prefix=/usr/local/nginx --with-http_realip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_stub_status_module --without-select_module --without-poll_module --with-http_geoip_module --with-http_ssl_module --with-openssl-opt=enable-tlsext --with-pcre=/root/pcre-8.35

make && make install

配置GeoIP

gunzip GeoLiteCity.dat.gz && gunzip GeoIP.dat.gz

将这两个解压后的库文件移到nginx的conf目录下

mv GeoLiteCity.dat GeoIP.dat /usr/local/nginx/conf/

配置nginx.conf文件http{}

geoip_country /usr/local/nginx/conf/GeoIP.dat;
geoip_city /usr/local/nginx/conf/GeoLiteCity.dat;
#geoIP的白名单
geo $remote_addr $ip_whitelist {
default 0;
include ip.conf;
}
map $geoip_country_code $allowed_country {
default no;
CN yes;
HK yes;
}

在要使用geoIP的虚拟主机中的location中加入GeoIP配置,这里直接贴一个配置

location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#白名单配置
if ($ip_whitelist = 1) {
proxy_pass http://web;
break;
}
#屏蔽的国家返回403
if ($allowed_country = yes){
proxy_pass http://192.168.1.203;
return 403;
}

在conf下新建一个ip.conf作为Geoip的白名单,支持ip段,内容和格式为:

8.8.8.8 1;
8.8.8.8/24 1;

检查配置

/usr/local/nginx/sbin/nginx -t

如果是64位系统可能会报:

/nginx: error while loading shared libraries: libGeoIP.so.1: cannot open shared object file: No such file or directory

解决方法:

ln -s /usr/local/lib/libGeoIP.so* /lib64/

启动服务

/usr/local/nginx/sbin/nginx已完成安装!!!

二.白名单配置(分情况而定)

如果访问ip等于ip.conf文件中的ip,则,ip192.168.1.249和192.168.1.135都可访问

http {
include mime.types;
default_type application/octet-stream;
geoip_country /usr/local/nginx/conf/GeoIP.dat; #添加
geoip_city /usr/local/nginx/conf/GeoLiteCity.dat;#添加 #geoIP的白名单(添加)
geo $remote_addr $ip_whitelist {
default 0;
include ip.conf;
}
#location中添加
if ($ip_whitelist = 1) {
proxy_pass http://192.168.1.203;
break;
}

cat ip.conf (格式:IP/掩码 1;)

192.168.1.249/32 1;

1.)ip_whitelist 读取 ip.conf中ip,如果访问的ip=ip.conf中1,1指定相应的白名单IP,就可以访问。
日志.png

Tenginx安装及配置_第1张图片
页面.png
2.)如果break改为return 403 ,访问ip等于ip.conf文件中的ip,则,192.168.1.249返回403,则其余ip(192.168.1.135)都可访问
Tenginx安装及配置_第2张图片
日志对比.png

Tenginx安装及配置_第3张图片
iweb对比图.png
3.)如果break改为return 403 ,并且在判断等于1时改成,!=1,也就是不等于1,访问ip等于ip.conf文件中的ip时,则ip.conf中所有ip( 192.168.1.249)均可以访问,不存在ip.conf文件中的ip均(192.168.1.135)返回403,
Tenginx安装及配置_第4张图片
image.png

Tenginx安装及配置_第5张图片
image.png
屏蔽国家

在nginx.conf文件中http{添加}以下内容

#屏蔽国家
map $geoip_country_code $allowed_country {
default no;
CN yes;
HK yes;
}
在location添加以下内容
#屏蔽的国家返回403
if ($allowed_country = yes){
proxy_pass http://192.168.1.203;
return 403;
}

Tenginx安装及配置_第6张图片
image.png

nginx.conf

worker_processes 1;
worker_rlimit_nofile 65535;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
geoip_country /usr/local/nginx/conf/GeoIP.dat;
geoip_city /usr/local/nginx/conf/GeoLiteCity.dat;
#geoIP的白名单
geo $remote_addr $ip_whitelist {
default 0;
include ip.conf;
}
#屏蔽国家
map $geoip_country_code $allowed_country {
default no;
CN yes;
HK yes;
}
sendfile on;
keepalive_timeout 300;
server {
listen 80;
server_name 192.168.1.203;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
if ($ip_whitelist = 1) {
proxy_pass http://192.168.1.203;
break;
}
#屏蔽的国家返回403
if ($allowed_country = yes){
proxy_pass http://192.168.1.203;
return 403;
}
#if ($geoip_country_code ~ "(HK|TW|PH|MO|US|CN)") {
#proxy_pass http://192.168.1.203;
# return 403;
#}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

}

}
}

HTTP/1.1 304 Not Modified不是服务器发出的错误提示。而是服务器所承载的业务系统在开发时为了节省链路带宽和提升浏览器的体验对GET/js、css、image等执行了缓存机制。客户端在第一次对服务器业务发出GET请求后,客户端浏览器缓存了该页面,当客户端第二次对服务器发出同样的GET请求时,若客户端缓存中的If-Modified-Since过期,客户端将向服务器发出GET请求,验证If-Modified-Since和If-None-Match是否与WEB-server中信息一致,如果GET页面未做任何修改,服务器对客户端返回HTTP/1.1 304 Not Modified,客户端则直接从本地缓存中将页面调取。
u

报错信息:

2018/08/25 15:17:45 [crit] 13571#0: *3034 open() "/usr/local/nginx/html/50x.html" failed (24: Too many open files), client: 192.168.1.203, server: 192.168.1.203, request: "GET / HTTP/1.0", upstream: "http://192.168.1.203:80/", host: "192.168.1.203"
解决方法
在nginx.conf配置文件中开始位置添加:

worker_rlimit_nofile 65535;
#指定一个nginx进程可以打开的最多文件描述符数目

参考地址:https://www.52os.net/articles/configure-nginx-using-geoip-allow-whitelist.html

你可能感兴趣的:(Tenginx安装及配置)