[root@tiandong63 ~]#yum groupinstall "Development Tools" "Development Libraries" -y
[root@tiandong63 ~]#yum install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre* -y
[root@tiandong63 ~]#tar -zxvf nginx-1.9.4.tar.gz -C /usr/local/src/
[root@tiandong63 ~]#cd /usr/local/src/
[root@tiandong63 src]#cd nginx-1.9.4/
[root@tiandong63 nginx-1.9.4]#./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module
[root@tiandong63 nginx-1.9.4]#make && make install
[root@tiandong63 nginx-1.9.4]#useradd -M -u 8001 -s /sbin/nologin nginx
[root@tiandong63 ~]# /usr/local/nginx/sbin/nginx 启动nginx
[root@tiandong63 ~]# netstat -antup|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 42350/nginx
测试nginx
配置文件:
2 user nginx nginx;
43 location / {
44 root html;
45 index index.html index.htm;
46 if ($request_uri ~* \.html$){
47 proxy_pass http://htmlservers;
48 }
49 if ($request_uri ~* \.php$){
50 proxy_pass http://phpservers;
51 }
52 proxy_pass http://picservers;
53 }
123 upstream htmlservers { #定义负载均衡服务器组名称
124 server 192.168.199.4:80;
125 server 192.168.199.5:80;
126 }
127 upstream phpservers{
128 server 192.168.199.4:80;
129 server 192.168.199.5:80;
130 }
131 upstream picservers {
132 server 192.168.199.4:80;
133 server 192.168.199.5:80;
134 }
135 }
[root@tiandong63 ~]# ln -s /usr/local/nginx/sbin/* /usr/bin/
[root@tiandong63 ~]# 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@tiandong63 ~]# nginx -s reload
配置后端服务器(tiandong64和tiandong65上面配置一样):
[root@tiandong64 ~]# yum install php httpd -y
[root@tiandong65 ~]# yum -y install php httpd -y
[root@tiandong64 ~]# cd /var/www/html/
[root@tiandong64 html]# more index.html
192.168.199.4
[root@tiandong64 html]# more test.php
192.168.199.4
<?php
phpinfo();
?>
[root@tiandong64 html]#
[root@tiandong64 html]# ll
total 12
-rw-r--r-- 1 root root 14 Mar 20 00:24 index.html
-rw-r--r-- 1 root root 790 Dec 24 2018 pic.PNG
-rw-r--r-- 1 root root 35 Mar 20 00:25 test.php
重启Apache服务:
[root@tiandong64 ~]# /etc/init.d/httpd restart
[root@tiandong65 ~]# /etc/init.d/httpd restart
测试一下html页面:
[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.5
[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.4
[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.5
[root@tiandong66 ~]# curl 192.168.199.3
192.168.199.4
测试php页面: