一. 安装
操作系统: CentOS 7 ,nginx 版本 1.12.2,Windows server 2008 iis
1.1 确认 nginx 所依赖的工具
Zlib: nginx 提供 gzip 模块, 需要 zlib 库支持, Openssl: nginx 提供 ssl 功能, Pcre: 支持地址重写 rewrite 功能. 如果没有安装, 在 root 下使用 yum 来安装.
-- 检测是否安装依赖工具包
- [root@xuegod64 hsr]# yum list | grep zlib
- [root@xuegod64 hsr]# yum list | grep openssl
- [root@xuegod64 hsr]# yum list | grep pcre
1.2 安装 nginx
下载了 nginx-1.12.2.tar.gz 包后, 放在了 Linux 系统目录 / home/hsr / 下.
-- 解压
- [root@xuegod64 hsr]# tar -xzvf nginx-1.12.2.tar.gz
- [root@xuegod64 hsr]# cd nginx-1.12.2
-- 编译
[root@xuegod64 nginx-1.12.2]# ./configure
-- 安装
- [root@xuegod64 nginx-1.12.2]# make
- [root@xuegod64 nginx-1.12.2]# make install
1.3 生成系统运行的 nginx 用户
- /*
- 使用 - u 来生成用户 ID 8000,
- -s 指定用户登入后所使用的 shell,
- nginx 是指系统用户
- */
- [root@xuegod64 ~]# useradd -u 8000 -s /usr/ sbin/nologin nginx
-- 查看 nginx 用户信息
- [root@xuegod64 sbin]# cat /etc/passwd
- nginx:x:8000:8000::/home/nginx:/sbin/nologin
1.4 启动 nginx
- [root@xuegod64 sbin]# pwd
- /usr/local/nginx/sbin
- [root@xuegod64 sbin]# ./nginx -c /usr/local/nginx/conf/nginx.conf
上面 nginx 服务已经启动成功, 后面可以把 / usr/local/nginx/sbin 目录添加到 path 全局中, 就不用在定位到 / usr/local/nginx/sbin 下. 默认端口是 80(确认防火墙已开通 80 端口).
-- 查看 nginx 进程
[root@xuegod64 sbin]# ps -ef | grep nginx
最后查看 nginx 的运行状态
[root@xuegod64 sbin]# curl http://192.168.2.101:80
如果没有出现 welcome to nginx, 需要查看 nginx 的日志 (/usr/local/nginx/logs), 在要分发的客户端 (iis 服务) 拼通 telnet 192.168.2.101 80. 也可以在浏览器里输入地址查看状态, 如下:
二. 配置 IIS 分发
2.1 编辑 nginx.conf 文件(可以先备份一下)
(1) 首先注释掉 nobdy 用户, 添加 nginx 用户用户组, 如下脚本
- #user nobody;
- user nginx nginx;
- worker_processes 1;
(2)添加分发策略
在 http 模块里找到 server 节点, 里面的 location 节点, Location 是网站的根目录, 在 location 节点里, 添加分发策略, 如下图(注意 if 与 大括号{之间要有空隔)
- location / {
- root html;
- index index.HTML index.htm;
- if ($request_uri ~* \.HTML$) {
- proxy_pass http://htmlservers;
- }
- if ($request_uri ~* \.aspx$){
- proxy_pass http://aspxservers;
- }
- proxy_pass http://picservers;
- }
注意上面的 uri 跟 url 一样, 统一资源占位符例如如果进来是已. HTML 结尾, 全部转到 http://htmlservers 服务器上, 还有 aspxservers 服务器, 最后是 picservers 服务器.(名字自己启, 相当一个服务器池). 最后 picservers 相当于 else.
(3) 定义负载设备的 ip(这里是 IIS 服务器上的 50,51 ip), 添加位置在 http 模块里的最后一行, 后期根据自己的实际 ip, 进行更改, 保存 nginx.conf 文件退出
- upstream htmlservers{
- server 192.168.2.50:80;
- server 192.168.2.51:80;
- }
- upstream aspxservers{
- server 192.168.2.50:80;
- server 192.168.2.51:80;
- }
- upstream picservers{
- server 192.168.2.50:80;
- server 192.168.2.51:80;
- }
- }
(4) 检查配置是否正确, 在重新加载配置
- [root@xuegod64 sbin]# pwd
- /usr/local/nginx/sbin
- [root@xuegod64 sbin]# ./nginx -t
- nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
- nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
- [root@xuegod64 sbin]# ./nginx -s reload
来源: https://www.cnblogs.com/MrHSR/p/9785428.html