什么是nginx?
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发,供俄国大型的入口网站及搜索引擎Rambler(俄文:Рамблер)使用。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好.中国大陆使用nginx网站用户有:新浪、网易、 腾讯等。
Nginx的应用场景概括
1、http服务器。Nginx是一个http服务可以独立提供http服务。可以做网页静态服务器。
2、虚拟主机。可以实现在一台服务器虚拟出多个网站,例如个人网站使用的虚拟机。
3、反向代理,负载均衡。当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可以使用nginx做反向代理。并且多台服务器可以平均分担负载,不会应为某台服务器负载高宕机而某台服务器闲置的情况。
Nginx实现虚拟机
可以实现在同一台服务运行多个网站,而且网站之间互相不干扰。
同一个服务器可能有一个ip,网站需要使用80端口。网站域名不同。
区分不通过的网站有三种方式:ip区分,端口区分,域名区分。
ip区分:即一个网卡分配多个ip;即复制网卡文件更改网卡名称和ip地址。然后更改nginx的配置文件nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name 192.168.21.199;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://rest.taotao.com;
index index.html index.htm;
}
}
server {
listen 80;
server_name 192.168.21.200;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html; #指定根目录
index index.html index.htm;
}
}
}
基于端口的虚拟主机:
server {
listen 80;
server_name 192.168.21.199;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://rest.taotao.com;
index index.html index.htm;
}
}
server {
listen 81;
server_name 192.168.21.199;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html; #指定根目录
index index.html index.htm;
}
}
基于域名的虚拟主机(最常用,一个域名只能可以绑定一个ip,一个ip可以被多个域名绑定)
更改nginx配置
server {
listen 80;
server_name rest.taotao.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://rest.taotao.com;
index index.html index.htm;
}
}
server {
listen 80;
server_name sso.taotao.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html; #指定根目录
index index.html index.htm;
}
}
可以根据修改本机的host文件来测试。限于篇幅,我们下一篇再继续介绍。