Nginx学习笔记(三):常用模块

Nginx相关资料请参阅Nginx中文文档

Nginx模块

Nginx模块共有下面的这些:

  • ngx_http_core_module
  • ngx_http_access_module
  • ngx_http_addition_module
  • ngx_http_auth_basic_module
  • ngx_http_autoindex_module
  • ngx_http_browser_module
  • ngx_http_charset_module
  • ngx_http_dav_module
  • ngx_http_empty_gif_module
  • ngx_http_fastcgi_module
  • ngx_http_flv_module
  • ngx_http_geo_module
  • ngx_http_geoip_module
  • ngx_http_gunzip_module
  • ngx_http_gzip_module
  • ngx_http_gzip_static_module
  • ngx_http_headers_module
  • ngx_http_image_filter_module
  • ngx_http_index_module
  • ngx_http_limit_conn_module
  • ngx_http_limit_req_module
  • ngx_http_log_module
  • ngx_http_map_module
  • ngx_http_memcached_module
  • ngx_http_mp4_module
  • ngx_http_perl_module
  • ngx_http_proxy_module
  • ngx_http_random_index_module
  • ngx_http_realip_module
  • ngx_http_referer_module
  • ngx_http_rewrite_module
  • ngx_http_secure_link_module
  • ngx_http_split_clients_module
  • ngx_http_ssi_module
  • ngx_http_ssl_module
  • ngx_http_sub_module
  • ngx_http_upstream_module
  • ngx_http_userid_module
  • ngx_http_xslt_module
  • ngx_mail_core_module
  • ngx_mail_pop3_module
  • ngx_mail_imap_module
  • ngx_mail_smtp_module
  • ngx_mail_auth_http_module
  • ngx_mail_proxy_module
  • ngx_mail_ssl_module

模块使用举例

在这里我只介绍其中的四个模块,其他模块用法请参阅Nginx中文文档

ngx_http_access_module

模块 ngx_http_access_module 允许限制某些IP地址的客户端访问。也可以通过 密码来限制访问。 使用 satisfy 指令就能同时通过IP地址和密码来限制访问。
配置范例:
location / {
deny 192.168.0.21;
allow 192.168.0.0/24;
deny all;
}
规则按照顺序依次检测,直到匹配到第一条规则。 在这个例子里,IPv4的网络中只有192.168.0.0/24允许访问,但 192.168.0.21除外, 在规则很多的情况下,使用 ngx_http_geo_module 模块变量更合适。

192.168.0.21访问nginx返回403错误:
3-2-5-1

ngx_http_auth_basic_module

模块ngx_http_auth_basic_module 允许使用“HTTP基本认证”协议验证用户名和密码来限制对资源的访问。也可以通过 地址来限制访问。 使用satisfy 指令就能同时通过地址和密码来限制访问。
使用这个模块需要httpd htpasswd的支持,所以我们先安装httpd

[root@tengine-1 conf]# yum -y install httpd
[root@tengine-1 conf]# htpasswd -bcm /var/user yjscloud 123456   #创建访问前端访问的用户和密码
Adding password for user yjscloud
[root@tengine-1 conf]# cat /var/user
yjscloud:$apr1$G1Cf7JMP$cw0.ofGQ6w7dbaMqDJACs.

修改配置文件:
3-2-6

/etc/rc.d/init.d/nginx restart

前端查看:
3-2-7

ngx_http_proxy_module

什么是反向代理?

  • 通常的代理服务器,只用于代理内部网络对Internet的连接请求,客户机必须指定代理服务器,并将本来要直接发送到Web服务器上的http请求发送到代理服务器中由代理服务器向Internet上的web服务器发起请求,最终达到客户机上网的目的。
  • 而反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器

3-2-8

单点的反向代理配置:
首先我们要安装一个tomcat,将apache-tomcat-7.0.61.tar.gz安装包上传到/opt目录下,需要下载tomcat的小伙伴请点击这里

yum -y install java
cd /opt
tar zxf apache-tomcat-7.0.61.tar.gz
sh /opt/apache-tomcat-7.0.61/bin/startup.sh

修改配置文件:
3-2-9

/etc/rc.d/init.d/nginx restart   #重启nginx

多点反向代理配置:
已同样的方法在另外一个节点(192.168.0.21)安装tomcat
修改配置文件:
3-2-10

/etc/rc.d/init.d/nginx restart  #重启nginx

修改网页内容

[root@tengine-1 ]# cd /opt/apache-tomcat-7.0.61/webapps/ROOT/
[root@tengine-1 ]# echo "192.168.0.19~~~tomcat-111111" > index.jsp
[root@tengine-2 ]# cd /opt/apache-tomcat-7.0.61/webapps/ROOT/
[root@tengine-2 ]# echo "192.168.0.21~~~tomcat-222222" > index.jsp

修改后分别在两台机子上重启tomcat

sh /opt/apache-tomcat-7.0.61/bin/shutdown.sh
sh /opt/apache-tomcat-7.0.61/bin/startup.sh

前端访问:

3-2-11
3-2-12

ngx_http_upstream_module

这是tengine新添加的健康检查模块

在nginx.conf添加如下配置:
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
check_http_send "HEAD / HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;

3-2-13

etc/rc.d/init.d/nginx restart  #重启nginx

前端页面查看

3-2-14

关闭tengine-2

3-2-15
前端监控到主机掉线,启动tengine-2后前端监控将恢复正常。

|| 版权声明
作者:废权
链接:https://blog.yjscloud.com/archives/83
声明:如无特别声明本文即为原创文章仅代表个人观点,版权归《废权的博客》所有,欢迎转载,转载请保留原文链接。
THE END
分享
二维码
Nginx学习笔记(三):常用模块
Nginx相关资料请参阅Nginx中文文档 Nginx模块 Nginx模块共有下面的这些: ngx_http_core_module ngx_http_access_module ngx_http_addition_module ngx_http_……
<<上一篇
下一篇>>
文章目录
关闭
目 录