一。什么是Nginx
是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,官方数据支持5万并发,占用资源少,C语言开发。
二。有什么用
个人经历:在公司项目上线本人负责项目生产环境的部署,稍微正规点的局方都会要求对生产环境进行管控,不允许我们平时的办公环境直连,但是如果通过linux操作生产环境进行项目调试是很不方便,比如你想测试web前端界面,你还需要登入局方的权限管理平台验证后登入,因为测试环境可以连接生产环境,办公环境又可以连接测试环境,所以我在测试环境搭建了nginx服务器,对生产上部署的web工程和http接口进行反向代理,方便生产的项目调试。
(1)作为普通的http服务器,像tomcat一样
(2)虚拟主机:开启多个端口提供访问服务
(3)反向代理:区别反向和正向代理
(4)负载均衡:多集群进行负载均衡,一般公司的软件负载均衡都是使用的这个。
三。安装部署
(1)注意事项
前面说了nginx是C语言开发的,linux肯定需要gcc编译的,在有些现场环境局方是不会提供root用户,如果服务器维护人员没有安装这些基本的工具那将是一件非常头疼的事,我就遇到那种什么都不给你装的主机。最后你装一个软件就报一个缺少依赖的错,一般主机还不允许连外网,所以你也没办法下,只能本地下好再上传服务器,我会在另一篇文章里专门介绍linux非root用户下的软件安装。
(2)依赖
pcre(www.pcre.org):正则表达式库,pcre来解决C语言中使用正则表达式的问题。 10之后的版本名称变成pcre2了,所以nginx没有对应的就会报错 fatal error: pcre.h: No such file or directory #include "pcre.h"
zlib(www.zlib.org):lib 是通用的压缩库,提供了一套 in-memory 压缩和解压函数,安装软件几乎都要用。
openssl(www.openssl.org):OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。这个库是可选的,当你使用https时就需要使用了,普通http可以不需要。
(3)安装
root用户下ngnx的安装:https://blog.youkuaiyun.com/t8116189520/article/details/81909574 (写得简单明了)
非root用户下nginx的安装:https://www.cnblogs.com/zzw-zyba/p/8820160.html(基本问题写得很明了)
windos系统的安装:https://www.cnblogs.com/jiangwangxiang/p/8481661.html
nginx平滑添加stream模块:https://www.cnblogs.com/crysmile/p/9565048.html
(可能有部分人有代理tcp的需求,一般代理kafka,storm,zookeeper服务使用的是TCP协议,很多教程是不安装stream模块的所以无法代理,建议在安装的时候就加上stream模块,因为使用反向代理时很可能会使用的到 ,安装时在configure 后面携带 --with-stream参数,上面链接是如何不影响当前使用的使用的情况下平滑添加stream模块)
安装中使用的命令解说:https://www.cnblogs.com/tinywan/p/7230039.html
1.下载或上传nginx安装包
2.解压安装包并检查安装环境,
tar -zxvf nginx安装包
cd nginx解压后的路径
使用以下检测安装环境和缺少的库
./configure --with-http_stub_status_module --prefix=nginx安装目录(必须是存在的目录) --with-pcre=pcre解压目录 --with-zlib=zlib解压安装目录
3.缺少依赖库就安装
报错
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
比如pcre包的非root权限的安装,其他的zlib等依赖库的安装参考pcre
下载上传pcre的安装包
pcre-8.10.zip
cd pcre-8.10
./configure --prefix=pcre的安装目录 (自定义)
make
make install
4.安装好依赖包之后再次进入nginx的解压目录检测nginx安装的依赖环境
./configure --with-http_stub_status_module --prefix=nginx安装目录(必须是存在的目录) --with-pcre=pcre解压目录 --with-zlib=zlib解压安装目录
checking for openat(), fstatat() ... found
checking for getaddrinfo() ... found
checking for zlib library ... found
creating objs/Makefile
Configuration summary
+ using PCRE library: /opt/aspire/product/bdctool/nginx/zzw_other/pcre-8.10
+ OpenSSL library is not used --- OpenSSL 可选
+ using system zlib library --- 其实是要依赖zlib包的,但系统已安装,所以没有额外安装,如果系统没安装,要自行如pcre一样安装一下,并且在configure时加 --with-zlib=(自行安装的zlib的目录)
nginx path prefix: "/opt/aspire/product/bdctool/nginx"
nginx binary file: "/opt/aspire/product/bdctool/nginx/sbin/nginx"
nginx modules path: "/opt/aspire/product/bdctool/nginx/modules"
nginx configuration prefix: "/opt/aspire/product/bdctool/nginx/conf"
nginx configuration file: "/opt/aspire/product/bdctool/nginx/conf/nginx.conf"
nginx pid file: "/opt/aspire/product/bdctool/nginx/logs/nginx.pid"
nginx error log file: "/opt/aspire/product/bdctool/nginx/logs/error.log"
nginx http access log file: "/opt/aspire/product/bdctool/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
没什么大问题
5.编译,安装
make
make install
6.成功验证
nginx常用命令:nginx安装目录下的sbin目录,配置文件错了会有错误提示
启动 | ./nginx |
关闭 | ./nginx -s stop ./nginx -s quit |
重载配置(重启) | ./nginx -s reload |
启动成功:没有任何提示
使用浏览器访问:http://ip:80 (默认是启动80端口,但是非root不能启动1024之前的端口,所以可能会失败,把端口改成其他的,后面会详细介绍配置文件的内容)
四。怎么用
nginx配置文件路径:/nginx安装目录/conf/nginx.conf
修改配置之后就重载配置:./nginx -s reload
(1)作为普通服务器
这个和tomcat相同
1.配置文件:在配置文件的http下提交内容,
补充介绍:
index 表示默认访问该端地址的首页;
listen表示访问的端口号,http默认使用80;
server_name 表示访问的主机地址,一般使用localhost,使用本服务器的IP访问;
location / 表示该访问地址的默认访问配置 / 换成其他特定名称访问时需要携带该路径,一个server节点可以设置多个不同名的location;
(2)虚拟主机
通过使用多个端口模拟多态web服务器
1.通过端口号区分不同的主机
这种配置就是把(1)中的配置信息复制一份修改端口号,和root的目录
2.通过域名区分不同的主机
测试先修改本地的host文件,在文件中把域名和主机端口分别对应,这样在本地直接使用域名可以实现不同主机之间的访问
手动修改hosts文件比较麻烦,一般企业中使用hosts切换的工具:switchhosts 来方便切换hosts文件中的配置。使用时必须以管理员权限运行。
(3)反向代理
代理多个节点配置方式:
代理tcp协议的端口:在配置文件中stream和http是同级标签。
stream {
}
(4)负载均衡
1.配置文件:在上游节点配置中增加主机节点并设置均衡策略(不写默认是轮询)
2.均衡策略
配置参考:https://www.cnblogs.com/1214804270hacker/p/9325150.html