一.配置nginx
重新配置一台新的虚拟机,创造纯净环境。
1.下载安装包并解压
nginx-1.14.0.tar.gz
[root@server1 ~]# tar zxf nginx-1.14.0.tar.gz
2.修改配置文件,并编译安装
[root@server1 ~]# cd nginx-1.14.0/src/core
[root@server1 core]# vim nginx.h
#define NGINX_VER "nginx"
[root@server1 ~]# cd nginx-1.14.0/auto/cc
[root@server1 cc]# vim gcc
# debug
#CFLAGS="$CFLAGS -g"
3.安装nginx插件,并编译安装
[root@server1 ~]# cd nginx-1.14.0
[root@server1 nginx-1.14.0]# yum install gcc openssl-devel pcre-devel -y
[root@server1 nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio
[root@server1 nginx-1.14.0]# make
[root@server1 nginx-1.14.0]# make install
[root@server1 nginx-1.14.0]# ln -s /usr/local/nginx/sbin/nginx /sbin/
4.添加文件
[root@server1 ~]# cd /usr/local/nginx/html
[root@server1 html]# vim test.html
<h1>www.westos.org</h1>
[root@server1 html]# ls
50x.html index.html test.html
#默认发布文件:index.html
5.开启服务
[root@server1 sbin]# nginx ##打开nginx服务
#nginx -s stop ##关闭服务
#nginx -t ##检测配置文件是否正确
测试:
[root@server1 nginx]# cd /usr/local/nginx/conf/
[root@server1 conf]# useradd -M -d /usr/local/nginx/ nginx
[root@server1 conf]# id nginx
uid=500(nginx) gid=500(nginx) groups=500(nginx)
修改限制文件
[root@server1 ~]# vim /etc/security/limits.conf
修改配置文件
[root@server1 conf]# vim /usr/local/nginx/conf/nginx.conf
2 user nginx nginx;
3 worker_processes 1;
12 events {
13 worker_connections 65535;
14 }
17 http {
18 upstream westos {
19 server 172.25.14.2:80 weight=2;
20 server 172.25.14.3:80;
21 }
22 include mime.types;
23 default_type application/octet-stream;
24
120 server {
121 listen 80;
122 server_name www.westos.org;
123
124 location / {
125 proxy_pass http://westos;
126 }
127 }
128 }
在物理机中添加解析
[root@foundation14 ~]# vim /etc/hosts
172.25.14.1 www.westos.org
测试:
nginx实现负载均衡
下载并解压
[root@server1 html]# nginx -s stop
[root@server1 ~]# ls
nginx-1.10.1.tar.gz nginx-sticky-module-ng.tar.gz
[root@server1 ~]# tar zxf nginx-1.10.1.tar.gz nginx-sticky-module-ng.tar.gz
[root@server1 ~]# ls
nginx-1.10.1 nginx-sticky-module-ng
[root@server1 ~]# cd /nginx-1.10.1
[root@server1 nginx-1.10.1]# ./configure --prefix=/opt/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --add-module=/root/nginx-sticky-module-ng
[root@server1 nginx-1.10.1]# make
[root@server1 nginx-1.10.1]# make install
[root@server1 nginx-1.10.1]# cd /opt/nginx/conf
[root@server1 nginx-1.10.1]# cp /usr/local/nginx/conf/nginx.conf .
[root@server1 nginx-1.10.1]# vim nginx.conf
17 http {
18 upstream westos {
19 sticky;
20 server 172.25.14.2:80;
21 server 172.25.14.3:80;
22 }
[root@server1 conf]# /opt/nginx/sbin/nginx
[root@server1 conf]# /opt/nginx/sbin/nginx -s reload
将server2和server3的httpd打开,并在/var/www/html写入index.html文件
测试: