nginx
Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器
Nginx 是一个很强大的高性能Web和反向代理服务,它具有很多非常优越的特性: 在连接高并发的情况下,Nginx是Apache服务不错的替代品。能够支持高达 50,000 个并发连接数的响应
Nginx作为负载均衡服务:Nginx 既可以在内部直接支持 Rails 和 PHP 程序对外进行服务,也可以支持作为 HTTP代理服务对外进行服务。
一、nginx作为web服务器
1 编译安装nginx
参考:源码编译安装nginx
2 编写发布目录
nginx的发布目录: /usr/local/lnmp/nginx/html/
echo toto4 > index.html
3 使用物理主机进行测试:
[root@foundation13 ~]# curl 172.25.13.140
toto4
[root@foundation13 ~]# curl 172.25.13.140
toto4
[root@foundation13 ~]# curl 172.25.13.140
toto4
4 、设置虚拟主机,不同域名对应不同的发布目录
修改配置文件:
vim /usr/local/lnmp/nginx/conf/nginx.conf
在配置文件末尾添加
server {
listen 80;
server_name www.toto.com;
location / {
root /www;
index index.html;
}
}
server {
listen 80;
server_name music.toto.com;
location / {
root /music;
index index.html;
}
}
server {
listen 80;
server_name news.toto.com;
location / {
root /news;
index index.html;
}
}
nginx -s reload # 重新加载配置
创建各个虚拟主机的发布目录以及发布文件
[root@toto4 nginx]# mkdir /www
[root@toto4 nginx]# echo www.toto.com > /www/index.html
[root@toto4 nginx]# mkdir /music
[root@toto4 nginx]# echo music.toto.com > /music/index.html
[root@toto4 nginx]# mkdir /news
[root@toto4 nginx]# echo news.toto.com > /news/index.html
使用物理主机进行测试:需要在物理主机添加本地域名解析
[root@foundation13 ~]# curl www.toto.com
www.toto.com
[root@foundation13 ~]# curl music.toto.com
music.toto.com
[root@foundation13 ~]# curl news.toto.com
news.toto.com
[root@foundation13 ~]# curl 172.25.13.140
toto4
虚拟主机设置成功。使用不同的域名访问不同的资源。
二、nginx作为负载均衡
172.25.13.140 nginx 负载均衡
172.25.13.120 httpd 后端服务器
172.25.13.110 httpd 后端服务器
1 设置nginx配置文件
cd /usr/local/lnmp/nginx/conf
vim nginx.conf
更改如下:
http {
include mime.types;
default_type application/octet-stream;
upstream toto {
server 172.25.13.110:80;
server 172.25.13.120:80;
}
将之前设定的网页虚拟主机注释
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://toto;
}
}
}
2 两个后端服务器。安装并开启httpd服务,并设置发布页面。
3 使用物理主机,进行测试:curl 172.25.13.140
[root@foundation13 ~]# curl 172.25.13.140
toto2
[root@foundation13 ~]# curl 172.25.13.140
toto1
[root@foundation13 ~]# curl 172.25.13.140
toto2
[root@foundation13 ~]# curl 172.25.13.140
toto1
实现负载均衡