nginx 系列 1 linux下安装以及配置IIS分发

一. 安装

  操作系统: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
configure由shell脚本编写,该命令做了大量“幕后”工作,包括检测操作系统内核和已经安装的软件,参数的解析,中间目录的生成以及根据各种参数生成一些C源码文件,makefile文件等
configure后面的参数分四大类型,1.路径相关的参数,2.编译相关的参数。3.依赖软件的相关参数。4模块相关的参数
  

  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端口)。

   二进制文件路径为/usr/local/nginx/sbin/nginx, 配置文件路径为/usr/local/nginx/conf/nginx.conf。

   直接执行/usr/local/nginx/sbin/nginx会读以默认路径下的配置文件/usr/local/nginx/conf/nginx.conf

  实际上,没有显示提定nginx.conf配置文件路径时,将打开在configure命令执行时使用-conf-path=PATH指定的nginx.conf文件。上面使用-c  指定了配置文件路径。

 

--查看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。 也可以在浏览器里输入地址查看状态,如下:

    

  1.5 停止服务

      快速停止服务与kill一样

         /usr/local/nginx/sbin/nginx  -s  stop

      优雅停止服务

      /usr/local/nginx/sbin/nginx  -s  quit

   1.6 日志文件回滚

    /usr/local/nginx/sbin/nginx  -s  reopen

    先把当前日志文件改名或转移,再使用命令重新打开日志文件,这个功能使得日志文件不至于过大

 

 二.  配置IIS分发

  2.1 编辑nginx.conf文件(可以先备份一下)

    (1) 首先注释掉nobdy用户,添加 nginx用户用户组,如下脚本

2 #user  nobody;
3 user nginx nginx;
4 worker_processes  1;

    (2) 添加分发策略
    在http模块里找到server 节点,里面的location节点,Location 是网站的根目录, 在location节点里,添加分发策略, 如下图(注意if与 大括号{之间要有空隔), location会根据用户的请求中的url,来匹配url表达式

 44         location / {
 45             root   html;
 46             index  index.html index.htm;
 47         if ($request_uri ~* \.html$) {
 48                 proxy_pass http://htmlservers;
 49            }
 50          if ($request_uri ~* \.aspx$){
 51                proxy_pass http://aspxservers;
 52            }
 53            proxy_pass http://picservers;
 54         }

    注意上面的 uri 跟url一样,统一资源占位符例如如果进来是已.html结尾, 全部转到 http://htmlservers服务器上,还有aspxservers服务器,最后是picservers服务器。(名字自己启,相当一个服务器池)。 最后picservers相当于else。 


    (3) 定义负载设备的ip(这里是IIS服务器上的50,51 ip),添加位置在http模块里的最后一行,后期根据自己的实际ip,进行更改,保存nginx.conf文件退出

126        upstream  htmlservers{
127           server 192.168.2.50:80;
128           server 192.168.2.51:80;
129       }
130       upstream  aspxservers{
131           server 192.168.2.50:80;
132           server 192.168.2.51:80;
133       }
134       upstream   picservers{
135           server 192.168.2.50:80;
136           server 192.168.2.51:80;
137       }
138 
139 }

     (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

posted on 2018-10-14 11:55  花阴偷移  阅读(1128)  评论(0编辑  收藏  举报

导航