openresty 重启nginxnginx 支持ipv6 吗

使用Nginx+Lua代理Hadoop HA-Hadoop-@大数据资讯
你好,游客
使用Nginx+Lua代理Hadoop HA
来源:博客园_Florian&
一、& HA&的&Web&页面访问
Hadoop开启HA后,会同时存在两个Master组件提供服务,其中正在使用的组件称为Active,另一个作为备份称为Standby,例如HDFS的NameNode、YARN 的ResourceManager。HDFS的web页面只有通过Active的NameNode才能正常访问,同样地,YARN的web页面也只有通过Active的ResouceManager才能正常访问。
(1) HDFS HA的Web访问
正常使用Nginx的proxy_pass代理单一的Web服务地址时非常简单(参考博文最简反向代理配置),而面对Hadoop HA这样的多Web服务地址时就会有点麻烦。
(2) HDFS HA的Web代理
虽然Nginx的upstream支持配置多个Web地址,默认会随机将Web请求随机转发到任意一个Web地址,只有某个web地址被认为不可达后,才会被Nginx列入黑名单。而Hadoop HA的Active和Standby节点都是一直服务的,只是同一个时刻,最多只有一个节点的Web访问是有效的,这就要求Nginx对upstream中的Web地址更细致地检查,而非粗略地判断是否可达。
二、&Nginx&的&upstream&健康检查
对upstream的地址有效性检查称为健康检查。通过定期的调用检查逻辑,对upstream配置的Web地址进行标记,不健康的Web地址会被临时列入黑名单内,直到该地址被标记为健康状态时,才会有新的Web请求转发到该地址上。
(1)Nginx本身对upstream的健康检查支持并不强大,做不到对检查逻辑的自由定制。
(2)开源项目&nginx_upstream_check_module&以Nginx补丁的方式扩展了Nginx的upstream语法,支持自定义HTTP请求的方式检查Web服务的健康状态。但在实际使用过程中,遇到一个很不方便的地方。
upstream resourcemanagers {
server 192.168.0.1:8084;
server 192.168.0.2:8084;
check interval=30000 rise=1 fall=3 timeout=5000 type=
check_http_send "HEAD / HTTP/1.0\r\n\r\n";
check_http_expect_alive http_3
keepalive 2000;
nginx_upstream_check_module使用check命令定义健康检查的基本属性,使用check_http_send自定义HTTP请求,check_http_expect_alive定义期望的健康状态HTTP code。这里使用http_3xx是该模块定义的内置匹配语法,表示以3开头的HTTP code。想必大家已经想到,这种定义方式是无法精确区分301、302、307报文的。当然正常情况下,3xx的报文应该是同类型的报文,不需要如此精确的区分,但是不巧的是Hadoop2.7.2版本的Active ResourceManager和Standby ResourceManager分别返回的是302和307报文!
(3)以上两种方案并不是解决Nginx upstream健康检查的完美方案,真正完美的方案是&OpenResty&的&lua-resty-upstream-healthcheck&。OpenResty内置了大量的Lua库,可以自由扩展、定制Nginx的功能。其中healthcheck.lua模块用于upstream的健康检查。
不过我希望在Nginx的基础上只扩展upstream健康检查的功能,而非用OpenResty代替Nginx,因此需要使用Nginx的lua-upstream-nginx-module模块。
三、编译安装扩展&Nginx
(1)由于lua-upstream-nginx-module是使用Lua脚本对Nginx进行扩展,因此必须安装Lua解释器。&LuaJIT&是Lua语言的即时编译器,效率更高。
$ wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz
$ tar zxvf LuaJIT-2.0.4.tar.gz
$ cd LuaJIT-2.0.4
$ make install
$ export LUAJIT_LIB=/usr/local/lib
$ export LUAJIT_INC=/usr/local/include/luajit-2.0
$ rm /usr/local/lib/libluajit-5.1.so*
导出环境变量LUAJIT_LIB和LUAJIT_INC是为了后续编译lua-nginx-module模块使用。删除libluajit的所有动态链接库是为了保证后续编译时是静态链接,否则默认为动态链接。
(2)准备好Lua环境后,接下来下载Nginx的Lua模块&lua-nginx-module&、Nginx开发包&ngx_devel_kit&、Nginx upstreamLua模块&lua-upstream-nginx-module&、&pcre库&和&openssl库&、&Nginx源码&。解压后的文件列表如下:
./lua-nginx-module-0.10.5
./lua-upstream-nginx-module-0.05
./nginx-1.10.1
./ngx_devel_kit-0.3.0
./openssl-OpenSSL_1_0_1t
./pcre-8.38
执行命令编译Nginx:
$ cd nginx-1.10.1
$ ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --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=root --group=root --with-http_ssl_module --with-http_realip_module --with-http_addition_module --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_stub_status_module --with-http_auth_request_module --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_v2_module --with-http_v2_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' --with-pcre=../pcre-8.38 --with-openssl=../openssl-OpenSSL_1_0_1t --add-module=../ngx_devel_kit-0.3.0 --add-module=../lua-nginx-module-0.10.5 --add-module=../lua-upstream-nginx-module-0.05
$ make && make install
(3) 安装完毕后,Nginx的配置文件为/etc/nginx/nginx.conf,可执行文件为/usr/sbin/nginx。执行Nginx启动命令:
访问&http://127.0.0.1:8080&即可看到Nginx主页。
(4) 添加Lua测试链接,测试Lua模块是否正常工作。
location /lua {
set $test "hello, world.";
content_by_lua '
ngx.header.content_type = "text/plain";
ngx.say(ngx.var.test);
更新Nginx配置:
$ nginx -s reload
访问http://127.0.0.1:8080/lua即可看到&hello,world.&。
四、&Nginx&代理&Hadoop HA
(3) Nginx代理Hadoop HA
虽然安装了lua-upstream-nginx-module模块,但是仍需要使用OpenResty的healthcheck.lua模块才能完成upstream的健康检查功能。
(1) 下载最新版本的&OpenResty&代码。执行如下命令:
make && make install
ls /usr/local/openresty/lualib/resty/upstream/healthcheck.lua
其中healthcheck.lua脚本就是我们需要的健康检查模块。
(2) 配置nginx.conf:
upstream resourcemanagers {
server 192.168.0.1:8084;
server 192.168.0.2:8084;
keepalive 2000;
upstream namenodes {
server 192.168.0.1:50070;
server 192.168.0.2:50070;
keepalive 2000;
lua_package_path "/usr/local/openresty/lualib/?.;";
lua_shared_dict healthcheck 1m;
lua_socket_log_
init_worker_by_lua_block {
local hc = require "resty.upstream.healthcheck"
local ok, err = hc.spawn_checker {
shm = "healthcheck",
upstream = "resourcemanagers ",
type = "http",
http_req = "GET / HTTP/1.0\r\n\r\n",
interval = 2000,
timeout = 5000,
valid_statuses = {302},
concurrency = 1,
if not ok then
ngx.log(ngx.ERR, "=======& failed to spawn RM health checker: ", err)
local ok, err = hc.spawn_checker {
shm = "healthcheck",
upstream = "namenodes ",
type = "http",
http_req = "GET /webhdfs/v1/?op=LISTSTATUS HTTP/1.0\r\n\r\n",
interval = 2000,
timeout = 5000,
valid_statuses = {200},
concurrency = 1,
if not ok then
ngx.log(ngx.ERR, "=======& failed to spawn NameNode health checker: ", err)
location /yarn/ {
proxy_pass http://resourcemanagers/;
location /hdfs/ {
proxy_pass http://namenodes/;
更新Nginx配置:
$ nginx -s reload
访问&http://127.0.0.1:8080/hdfs&或http://127.0.0.1:8080/yarn即可看到HDFS或YARN的Web页面。
综上,使用Lua扩展Nginx的功能十分强大,且十分容易定制,这也是OpenResty的功能如此强大的原因。虽然OpenResty已经提供了lua-resty-upstream-healthcheck模块完成upstream的健康检查功能,不过我们仍在社区版的Nginx上亲自扩展了该功能。希望这篇文章能帮助大家快速的配置Nginx+Lua的环境,并方便地开发自己的Nginx扩展功能。
相关新闻 & & &
& (07月18日)
& (07月11日)
& (07月06日)
& (07月18日)
& (07月10日)
   同意评论声明
   发表
尊重网上道德,遵守中华人民共和国的各项有关法律法规
承担一切因您的行为而直接或间接导致的民事或刑事法律责任
本站管理人员有权保留或删除其管辖留言中的任意内容
本站有权在网站内转载或引用您的评论
参与本评论即表明您已经阅读并接受上述条款nginx配合modsecurity实现WAF功能
一.准备工作
系统:centos 6.5 64位、 ngx_openresty-1.7.10.1, modsecurity 2.9.0
openresty:
modsecurity for Nginx:
OWASP规则集:
依赖关系:
modsecurty依赖的包:pcre httpd-devel libxml2 apr
yum install httpd-devel apr apr-util-devel apr-devel
pcre pcre-devel
libxml2 libxml2-devel
openresty依赖的包:pcre 、zlib、 openssl
yum install zlib zlib-devel openssl openssl-devel
pcre pcre-devel
二.启用standalone模块并编译
下载modsecurity for nginx 解压,进入解压后目录执行:
./autogen.sh
./configure --enable-standalone-module --disable-mlogc
三.openresty添加modsecurity模块
在编译standalone后,openresty编译时可以通过"--add-module"添加modsecurity模块:
./configure --prefix=/opt/openresty
--with-pcre-jit
--with-ipv6 --without-http_redis2_module --with-http_iconv_module
-j2 --add-module=../modsecurity-2.9.0/nginx/modsecurity/
make && make install
四.添加规则
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
1.下载OWASP规则:
git clone /SpiderLabs/owasp-modsecurity-crs
owasp-modsecurity-crs /opt/openresty/nginx/conf/
cd /opt/openresty/nginx/conf/owasp-modsecurity-crs/ && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
2.启用OWASP规则:
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
mv modsecurity.conf-recommended /opt/openresty/nginx/conf/modsecurity.conf
cp unicode.mapping /opt/openresty/nginx/conf/
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
sed -i 's/^SecRuleEngine.*/SecRuleEngine On/' /opt/openresty/nginx/conf/modsecurity.conf
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用。
需要启用的规则使用Include到modsecurity.conf即可。
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
五.配置nginx
在需要启用modsecurity的主机的location下面加入下面两行即可:
ModSecurityE
ModSecurityConfig modsecurity.
下面是几个示例配置,php虚拟主机:
server_name test.net www.test.
location ~ \.php$ {
ModSecurityE
ModSecurityConfig modsecurity.
root /web/
index index.php index.html index.
fastcgi_pass
127.0.0.1:9000;
fastcgi_index
fastcgi_param
SCRIPT_FILENAME
$Document_root$fastcgi_script_
upstream负载均衡:
upstream online {
server 192.168.1.100:8080;
server 192.168.1.101:8080
listen 80;
server_name test.net www.test.
location / {
ModSecurityE
ModSecurityConfig modsecurity.
proxy_pass http://
proxy_set_header Host $
proxy_set_header X-Real-IP $remote_
proxy_set_header
X-Forwarded-For $proxy_add_x_forwarded_
泛域名解析,反向代理方式:
upstream real_webserver {
server 192.168.0.12;
server 192.168.0.13;
server_name
location {
ModSecurityE
ModSecurityConfig modsecurity.
proxy_set_header
proxy_set_header
proxy_set_header
X-Forwarded-For $proxy_add_x_forwarded_
proxy_pass http://real_
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
phpinfo();
在浏览器中访问:
http://www.52os.net/phpinfo.php?id=1 正常显示。
http://www.52os.net/phpinfo.php?id=1 and 1=1
http://www.52os.net/phpinfo.php?search=&scritp&alert('xss');&/script&
说明sql注入和xss已经被过滤了
Copyright (C) , All Rights Reserved.
版权所有 闽ICP备号
processed in 0.064 (s). 13 q(s)ntop──监视网络使用情况 - zvv
ntop──监视网络使用情况
July 18, 2017
跟 top 监视系统活动状况相似,ntop 是一个用来实时监视网络使用情况的工具。由于 ntop 具有 Web 界面模式,因此无论是配置还是使用都很容易在短时间之内快速上手。当你在安装并运行 ntop 之后,可以通过下面的地址在浏览器中打开它:在 ntop 的 Web 界面中,我们可以直观的查看到全局网络流量统计、Host 信息、网络负载统计、所有协议及 IP 的流量情况。此外,ntop 还提供相应工具,让你能够转存数据。&via。https://linuxtoy.org/archives/ntop.html
添加新评论你的位置: >
> nginx配置http2无效不起作用
最近博客打算做https顺便把http2也做上去,但是测试的时候发现还是http/1.1,问题出来哪里?
nginx -V 查看编译参数也带有 –with-http_v2_module 默认情况下http_v2_module是自动带着的Google 了一下发现是 OpenSSL 版本的问题OpenSSL 1.0.1e的版本不支持ALPN,所以无法开启 HTTP2
问题已经找到开始怎么解决问题
更新openssl
wget --no-check-certificate https://www.openssl.org/source/openssl-1.0.2j.tar.gz
tar zxvf openssl-1.0.2j.tar.gz
cd openssl-1.0.2j
./config shared zlib
make && make install
mv /usr/bin/openssl /usr/bin/openssl.old
mv /usr/include/openssl /usr/include/openssl.old
ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl
ln -s /usr/local/ssl/include/openssl /usr/include/openssl
echo "/usr/local/ssl/lib" && /etc/ld.so.conf
ldconfig -v
查看openssl版本
openssl version
安装之后目标的版本是
重新编译nginx
目前nginx中的openssl还没有修改过了,需要重新编译一下,保持上面图中的configure arguments,也就是编译参数,重新编译的时候需要用到
我的nginx版本是1.10.0,从官网上下载
wget http://nginx.org/download/nginx-1.10.0.tar.gz
tar zxvf nginx-1.10.0.tar.gz
cd nginx-1.10.0
修改加载openssl 方式
vi auto/lib/openssl/conf
CORE_INCS="$CORE_INCS $OPENSSL/.openssl/include"
CORE_DEPS="$CORE_DEPS $OPENSSL/.openssl/include/openssl/ssl.h"
CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libssl.a"
CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libcrypto.a"
CORE_INCS="$CORE_INCS $OPENSSL/include"
CORE_DEPS="$CORE_DEPS $OPENSSL/include/openssl/ssl.h"
CORE_LIBS="$CORE_LIBS $OPENSSL/lib/libssl.a"
CORE_LIBS="$CORE_LIBS $OPENSSL/lib/libcrypto.a"
重新编译nginx
上面编制的参数中添加–with-openssl参数
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --with-openssl=/usr/local/ssl
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
service nginx stop
PS.如果不停止nginx没有办法覆盖,会提示
cp -f ./objs/nginx /usr/local/nginx/sbin/
service nginx start
最后查看一下nginx openssl是否已经更新
QQ交流群:如无特别说明,本站文章皆为原创,若要转载,务必请注明以下原文信息:转载保留版权:&
本文链接地址:赏
与本文相关的文章
木有头像就木有JJ!按步骤申请Gravatar头像吧!

我要回帖

更多关于 openresty和nginx 的文章

 

随机推荐