首页 > 系统相关 >NGINX的stream模块以及实操问题

NGINX的stream模块以及实操问题

时间:2023-04-05 19:32:16浏览次数:48  
标签:module stream nginx 192.168 echo NGINX dvwa 实操


NGINX的stream模块以及实操问题

前言:

nginx从1.9.0开始,新增加了一个stream模块,用来实现四层协议的转发、代理或者负载均衡等。

nginx作为一个优秀的web服务器软件,毫无疑问,功能是十分强大的,但学习这种软件,如果脱离了实际操作(应用场景落地),那么就是一种耍流氓的行为。

(1)关于stream域的模块有哪些?

目前官网上列出的第三方模块、简直就是http模块的镜像、比如access模块访问控制ip和ip段,map模块实现映射、 geo模块实现地理位置映射、等等。使用这些模块的时候一定要看是哪个版本才支持的、比如log模块,只有在nginx-1.11.4才支持。

NGINX的stream相关模块有如下(有些模块特定版本才有,才支持,比如,log模块是NGINX的1.11.4版本):

ngx_stream_core_module
ngx_stream_access_module
ngx_stream_geo_module
ngx_stream_geoip_module
ngx_stream_js_module
ngx_stream_limit_conn_module
ngx_stream_log_module
ngx_stream_map_module
ngx_stream_proxy_module
ngx_stream_realip_module
ngx_stream_return_module
ngx_stream_split_clients_module
ngx_stream_ssl_module
ngx_stream_ssl_preread_module
ngx_stream_upstream_module
ngx_stream_upstream_hc_module

(2)

NGINX的这些功能的实际意义是哪些?首先,我们应该知道,一般网站服务器,是指驻留于因特网上某种类型计算机的程序,可以处理浏览器等Web客户端的请求并返回相应响应,也可以放置网站文件,让全世界浏览;可以放置数据文件,让全世界下载。目前最主流的三个Web服务器是Apache、 Nginx 、IIS。

web服务器可以发布网络资源(包括静态和动态),而NGINX不仅仅可以做这些,更有重写URL,负载均衡,访问控制,日志控制,正反向代理发布资源等等拓展功能。这是NGINX功能强大的一个体现(也就是说,不仅仅是发布网站资源,更可以以自己的需求更加优雅的,方便的,快捷安全的发布各类资源)。

(3)

综上所述,我们可以得出这么个结论,脱离了浏览器客户端,脱离了实际的应用场景,学习nginx的各种设置就是一种耍流氓的行为了。因此,我们既要讲tcp协议,也需要讲http1.0/1.1/2.0这些协议,还需要了解网站编码知识,ssl/tls协议等等各种的协议,浏览器的各种版本和纷争,结合各种实际的常出现的应用场景,来学习web服务器。

一,本文的目的

结合实际使用dvwa靶机,详细讲述nginx这类web服务器的搭建,发布,正反代理,什么样的需求对应什么样的技术(什么时候用什么手段,时机的选择)。本文主要讲解正反代理这一实用技术。

二,实验环境介绍

首先,是web应用dvwa的项目部署发布,其次,使用nginx正反代理dvwa项目,再次,探讨应该在什么时候使用正向代理,什么时候使用反向代理,正反代理对于我们的实际生产活动具有什么意义。

nginx正反代理以及端口映射

ip地址

所安装的服务

开放的端口

系统版本

网络情况

192.168.0.16/192.168.88.11

httpd服务,dvwa项目

80

centos7.2

vm虚拟机桥接模式

192.168.0.17/192.168.88.12

编译安装的nginx1.9.0

8080

centos7.2

vm虚拟机桥接模式

192.168.0.18/192.168.88.13

dvwa后台数据库mariadb

3306

centos7.2

vm虚拟机桥接模式

三,dvwa项目部署(192.168.0.16) 

请参考本人的博客极速安装dvwa。只是需要注意一点,这里我们采用的是前后端分离架构,因此,mariadb在这个服务器上不要安装。

四,nginx编译安装(192.168.0.17)

请参考本人的博客   这里需要更改一下脚本,将版本更换为1.9.0版本并编译时添加 --with-stream 选项。如有不会更改的,请直接使用一下脚本,该脚本使用先决条件有两个:1,可访问互联网,2有可用的仓库服务。

#!/bin/bash
#开始NGINX编译前的环境准备工作,请等候
yum install openssl openssl-devel pcre pcre-devel zlib zlib-devel -q -y
yum install -y -q autoconf automake apr  apr-devel  apr-util apr-util-devel \
                 bison  bzip2-devel cpp fontconfig-devel freetype-devel  gcc gcc-c++  compat-dapl \
                 compat-db-headers  compat-db47   compat-gcc-44 compat-gcc-44-c++  compat-glibc  \
                 compat-glibc-headers compat-libcap1 compat-libf2c-34  compat-libgfortran-41 \
                 compat-libtiff3 compat-openldap ncurses-deveal wget met-tools  && echo $?
useradd -M -s /sbin/nologin nginx
wget http://nginx.org/download/nginx-1.9.0.tar.gz
echo "============================================================================="
echo "如果提前下载了NGINX的安装包,请将上面的下载注释"
tar zxf nginx-1.9.0.tar.gz -C /usr/local/src/
cd /usr/local/src/nginx-1.9.0/
echo $PWD
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx \
--without-http_memcached_module \
--with-http_gzip_static_module \
--with-http_dav_module \
--with-threads \
--with-http_ssl_module \
--with-http_stub_status_module --with-stream && make 2>&1 | tee /root/nginx.log && make install

echo "编译完成,日志记录在/root/nginx.log内,如果出错,请查找原因,下面开始启动NGINX"
ln -s /usr/local/nginx/sbin/nginx /usr/bin/
nginx -t -c /usr/local/nginx/conf/nginx.conf
nginx && nginx -s reload
#生成启动脚本,启动方式为service nginx start|restart|stop|status
echo "#!/bin/bash
# nginx - this script starts and stops the nginx daemon
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ \"\$NETWORKING\" = \"no\" ] && exit 0
nginx=\"/usr/local/nginx/sbin/nginx\"
prog=\$(basename \$nginx)
NGINX_CONF_FILE=\"/usr/local/nginx/conf/nginx.conf\"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=\`\$nginx -V 2>&1 | grep \"configure arguments:\" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -\`
        if [ -z \"\`grep \$user /etc/passwd\`\" ]; then
                useradd -M -s /bin/nologin \$user
        fi
options=\`\$nginx -V 2>&1 | grep 'configure arguments:'\`
for opt in \$options; do
        if [ \`echo \$opt | grep '.*-temp-path'\` ]; then
                value=\`echo \$opt | cut -d \"=\" -f 2\`
                if [ ! -d \"\$value\" ]; then
                        # echo \"creating\" \$value
                        mkdir -p \$value && chown -R \$user \$value
                fi
        fi
done
}
start() {
[ -x \$nginx ] || exit 5
[ -f \$NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n \$\"Starting \$prog: \"
daemon \$nginx -c \$NGINX_CONF_FILE
retval=\$?
echo
[ \$retval -eq 0 ] && touch \$lockfile
return \$retval
}
stop() {
echo -n \$\"Stopping \$prog: \"
killproc \$prog -QUIT
retval=\$?
echo
[ \$retval -eq 0 ] && rm -f \$lockfile
return \$retval
}
restart() {
#configtest || return \$?
stop
sleep 1
start
}
reload() {
#configtest || return \$?
echo -n \$\"Reloading \$prog: \"
killproc \$nginx -HUP
RETVAL=\$?
echo
}
force_reload() {
restart
}
configtest() {
\$nginx -t -c \$NGINX_CONF_FILE
}
rh_status() {
status \$prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case \"\$1\" in
start)
        rh_status_q && exit 0
        \$1
        ;;
stop)
        rh_status_q || exit 0
        \$1
        ;;
restart|configtest)
\$1
;;
reload)
        rh_status_q || exit 7
        \$1
        ;;
force-reload)
        force_reload
        ;;
status)
        rh_status
        ;;
condrestart|try-restart)
        rh_status_q || exit 0
        ;;
*)
echo \$\"Usage: \$0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}\"
exit 2
esac

">/etc/init.d/nginx
chmod +x /etc/init.d/nginx
echo "启动方式为service nginx start|restart|stop|status,热重启方式为: nginx -s reload"
chkconfig --add nginx && chkconfig nginx on && service nginx restart

五,mariadb的安装(192.168.0.18)

参考第三条的dvwa极速安装的博客,安装即可。都是基本操作,就不解释过多了。

六,dvwa项目是否正常测试

此时,输入192.168.0.16/install.php 应该能够进入项目初始化,初始化完成后,应该在浏览器输入192.168.0.16即可进入dvwa靶机。效果如下图:

NGINX的stream模块以及实操问题_java

七,此时的架构分析

这一时期的架构大体总结一下:前端由192.168.0.16这个服务器通过httpd服务发布dvwa项目,开通的端口为默认的80端口,后端也就是数据库部署在192.168.0.18这个服务器上(由于我换电脑了,下图是另一个电脑的IP),数据库的作用是用户密码验证。其实,这个数据库十分简单,总共就一个dvwa库,库里有两个表,表的内容如下:

MariaDB [dvwa]> use dvwa;
Database changed
MariaDB [dvwa]> show tables;
+----------------+
| Tables_in_dvwa |
+----------------+
| guestbook      |
| users          |
+----------------+
2 rows in set (0.00 sec)

MariaDB [dvwa]> select * from users;
+---------+------------+-----------+---------+----------------------------------+-------------------------------------------------+---------------------+--------------+
| user_id | first_name | last_name | user    | password                         | avatar                                          | last_login          | failed_login |
+---------+------------+-----------+---------+----------------------------------+-------------------------------------------------+---------------------+--------------+
|       1 | admin      | admin     | admin   | 5f4dcc3b5aa765d61d8327deb882cf99 | http://192.168.88.11/hackable/users/admin.jpg   | 2021-08-23 22:08:43 |            0 |
|       2 | Gordon     | Brown     | gordonb | e99a18c428cb38d5f260853678922e03 | http://192.168.88.11/hackable/users/gordonb.jpg | 2021-08-23 22:08:43 |            0 |
|       3 | Hack       | Me        | 1337    | 8d3533d75ae2c3966d7e0d4fcc69216b | http://192.168.88.11/hackable/users/1337.jpg    | 2021-08-23 22:08:43 |            0 |
|       4 | Pablo      | Picasso   | pablo   | 0d107d09f5bbe40cade3de5c71e9e9b7 | http://192.168.88.11/hackable/users/pablo.jpg   | 2021-08-23 22:08:43 |            0 |
|       5 | Bob        | Smith     | smithy  | 5f4dcc3b5aa765d61d8327deb882cf99 | http://192.168.88.11/hackable/users/smithy.jpg  | 2021-08-23 22:08:43 |            0 |
+---------+------------+-----------+---------+----------------------------------+-------------------------------------------------+---------------------+--------------+
5 rows in set (0.00 sec)

MariaDB [dvwa]> select * from guestbook;
+------------+-------------------------+------+
| comment_id | comment                 | name |
+------------+-------------------------+------+
|          1 | This is a test comment. | test |
+------------+-------------------------+------+
1 row in set (0.00 sec)

 可以看到 dvwa有5个用户,用户密码是md5加密,可以通过www.cmd5.com这个网站查询到密码,如图所示:

NGINX的stream模块以及实操问题_nginx_02

稍作总结,用户1:admin  密码:password

用户2:gordonb 密码:abc123

用户3:1337 密码:charley

用户4:pablo 密码:letmein

用户5:smithy 密码:这个不需要查,和admin是一样的md5值,也就是一样是password

这5个用户,任选一个,打开浏览器输入192.168.0.16,即可登录dvwa项目,比如,我们使用1337这个用户,密码使用charley,即可成功登录dvwa,从以上的数据库表我们可以得出,什么用户都是一样的,并没有用户权限的设计。

啰嗦一句,修改登录密码,比如,已经使用1337/charley 登录后,如下图所示即可修改1337这个用户的密码:

NGINX的stream模块以及实操问题_http_03

8,关于浏览器的选择(浏览器适配问题) 

对于dvwa这个小的PHP项目来说,基本没有什么浏览器适配问题,可以选择使用QQ浏览器,也可以360浏览器,也可以谷歌浏览器,但,如果使用谷歌以外的浏览器会时不时的出现警告甚至错误,比如下面的两张图对比(一个是QQ浏览器,一个是谷歌浏览器,打开f12开发工具后所看到的警告):

NGINX的stream模块以及实操问题_http_04

 

NGINX的stream模块以及实操问题_web_05

可以看到谷歌浏览器会更适配,没有任何警告,当然了,web项目两大难点第一是浏览器的适配,第二是项目的编码,这些都是开发人员需要关注的地方。题外话,就不说了。360浏览器也挺不错,如果是自研web项目,可能会出的问题也会更严重一些。

9,nginx代理dvwa项目

前面已经说过了,在192.168.0.17这个服务器上安装了nginx,那么,可以代理局域网内其它服务器的web项目吗?答案是肯定的,

events {
    worker_connections  1024;
}
stream {
server {
listen 8080;
proxy_pass 192.168.0.16:80;
}
}

stream模块通常写在events模块下面即可,如上所示。service nginx restart 后,即可浏览器通过192.168.0.17:8080登录dvwa项目。如下图所示(电脑更换了,所以截图不是192.168.17)

 

NGINX的stream模块以及实操问题_linux_06

未完待续~~~~

 

标签:module,stream,nginx,192.168,echo,NGINX,dvwa,实操
From: https://blog.51cto.com/u_15966109/6171430

相关文章

  • 46 openEuler搭建Nginx服务器-管理Nginx
    46openEuler搭建Nginx服务器-管理Nginx46.1概述通过systemctl工具,可以对nginx服务进行管理,包括启动、停止、重启服务,以及查看服务状态等。本节介绍nginx服务的管理操作。46.2前提条件为了能够使用nginx服务,请确保您的系统中已经安装nginx服务。若未安装,可参考安装进行安装......
  • [FAQ] Pytorch PytorchStreamReader failed reading zip archive
     比如:rm-rf~/.cache/huggingface  Link:https://www.cnblogs.com/farwish/p/17290240.html......
  • Nginx容器安装vim命令
    dockerpullnginx:1.22.1dockerrun-itdnginx:1.22.1进入nginx容器apt-getupdateapt-getinstall-yvim ......
  • Nginx
    轻量级web服务器、反向代理服务器内存占用少、启动快、高并发过程:客户端访问-->(负载均衡/)Nginx-->tomcat/-->SOA反向代理:客户端访问-->转发-->代理到内网。反向代理"代理"的是服务器端Master-Worker模式启动Nginx-->80端口启动了Socket服务进行监听Master:读取......
  • 通过阿里云ECS服务器公网ip访问tomcat,nginx
    一、概述1、操作系统:centos72、安装nginx方法:https://www.cnblogs.com/boonya/p/7907999.html,亲测可用。3、tomcat版本:apache-tomcat-7.0.92二、操作步骤1、启动tomcat;2、进入阿里云控制台,选择安全组,配置安全组规则(添加80,8080端口规则);    3、按图示配置完成,点击......
  • How to Configure Nginx reverse proxy the domain
    未测试过,自己记录待用http{resolver8.8.8.8;upstreamexample{serverhttp://example.comresolve[use_last]...;keepalive1024;}第二种负载均衡upstreammytarget{serveraaa.tar.com:443max_fails=3fail_timeout=60s;serverbbb.tar.com:443backup;}server......
  • nginx 配置虚拟目录
     需求配置静态资源目录访问 location/npm{alias/usr/npm;indexindex.html;autoindexon;#开启目录访问access_logoff;expires30d;} 多级子目录下 [email protected]\dist\axios.min.js      注......
  • Java中的 Stream 流02
    1、流的创建1.1、使用集合对象的stream()方法创建流数组对象,创建流时需要使用Arrays.stream()方法;集合类对象,可以在对象后直接使用.stream()方法转换为流;Map对象不能直接转换为流,但是可以对Map对象中的key、value、entrySet分别转换为流方便后续使用。//数组对象in......
  • 45 openEuler搭建Nginx服务器-Nginx概述和安装
    45openEuler搭建Nginx服务器-Nginx概述和安装45.1概述Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,其特点是占用内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好,支持FastCGI、SSL、VirtualHost、URLRewrite、Gzip等......
  • Nginx——Nginx生产环境压测与性能优化实战
    摘要nginx的主要作用有三个方面:1、作为Web服务器;2、负载均衡服务器;3、邮件代理服务器等三个方面。其特点是占有内存少,并发能力强,给使用者带来了很多的便利。nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like协议下发行。但是在日常的工作......