1.安装nginx需要的依赖库
yum install -y gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
2.下载源码安装包
cd /opt
wget -c https://nginx.org/download/nginx-1.9.6.tar.gz
3.解压
tar -zxvf nginx-1.12.0.tar.gz #解压在/opt目录下
4..释放编译文件 开启nginx状态监测功能
cd /opt/nginx-1.12.0
./configure --prefix=/opt/nginx196 --with-http_ssl_module --with-http_stub_status_module
5.编译安装
6.启动nginx
cd /opt/nginx112/sbin
./nginx #启动
/opt/nginx196/sbin/nginx ##启动nginx
#启动了,然后在浏览器输入 192.168.11.73就能访问到 welcome to nginx页面,
./nginx -s stop #关闭
./nginx -s reload # 平滑重启 ,修改了nginx.conf之后,可以不重启服务,加载新的配置
7.nginx配置文件所在位置
Nginx主配置文件/etc/nginx/nginx.conf是一个纯文本类型的文件,整个配置文件是以区块的形式组织的。一般,每个区块以一对大括号{}来表示开始与结束。
8.nginx配置相关文档
worker_processes 5;
#error_log logs/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name www.qishi5douyu.com;
location / {
root /opt/web_server/douyu;
index index.html index.htm;
}
#error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.qishi5huya.com;
location /{
root /opt/web_server/huya;
index index.html;
}
}
server {
listen 80;
server_name www.qishi5zhanqi.com;
location /{
root /opt/web_server/zhanqi;
index index.html;
}
}
}
9.为了配合第8步的使用,需要创建一些文件
cd /opt
rm -rf web_server
mkdir -p web_server/huya
mkdir -p web_server/douyu
mkdir -p web_server/zhanqi
touch web_server/huya/index.html
touch web_server/huya/index.html
touch web_server/huya/index.html
分别打开这三个index.html文件
vim index.html
分别添加三个直播网站的源代码内容
然后保存退出
10.修改host(window下)
c:\windowssystem32driversetchosts
192.168.1.197 www.qishi5douyu.com
192.168.1.197 www.qishi5huya.com
192.168.1.197 www.qishi5zhanqi.com
#然后再重启
/opt/nginx196/sbin/nginx -s reload
11.nginx实现负载均衡
# 在http代码块里面,添加配置
upstream qishi_upstream {
server 192.168.12.200;
server 192.168.12.253;
}
在location代码块内,添加
location / {
proxy_pass http://qishi_upstream;
}