1.什么是nginx
Nginx 是一个百性能的 wveb 和受向代理服务器,它具有有很多非常优趣的特性:
作为 Web 服方器:相比Apache,Nginx 纯用更少的资源,支持更多的并发连接,体现更高的效率,这点使 Nginx 尤其受到虚拟主机提供商的欢迎。能够支持高达 50,000 个并发连接数的响应。
作为负载均衡服务器:Nginx 既可以在内部直换支持 Rails 和PHP,也可以支持作为 HTTP代理服务器对外进行服务。Nginx 用C 编写,不论是系统资源开销还是 CPU 使用效率都比Perbal 要好的多
作为邮件代理服务器:Nginx 同时也是一个主常优秀的邮件代理服务器(最早开发这个产品的
目的之一也是作为邮件代理服务器 )Last.fm 描述了 成功并且美妙的使用经验。
Nginx 安装非常的简单,配置大件非常简洁(还能够支特perl语法)Bugs非常少的服务器:Nginx 启动特别容易,并是几乎可以做到7*24不间所运行,即使运行数个月也不需要重新启动,你还能够在不间断服务的情况下进行软件版本的升级。
Nginx 可以作为web服务器,但更多的时候,我们把它作为网关,因为它具备网关必备的功能
- 反向代理
- 负载均衡
- 动态路由
- 请求过滤
2.Nginx作为web服务器
1.web服务器的分类
web应用服务器,如:
- tomcat
- resin
- jetty
web服务器,如:
- Apache 服务器
- Nginx
- IIS
区分:web服务器不能解析jsp等页面,只能处理js、css、html等静态资源。
并发:web服务器的并发能力远高于web应用服务器。
3.Nginx作为反向代理
1.什么是反向代理?
- 代理:通过客户机的配置,实现让一台服务器代理客户机,客户的所有请求都交给代理服务器处 理。
- 反向代理:用一台服务器,代理真实服务器,用户访问时,不再是访问真实服务器,而是代理服务 器。
- 我们需要提前在nginx中配置好反向代理的规则,不同的请求,交给不同的真实服务器处理
- 当请求到达nginx,nginx会根据已经定义的规则进行请求的转发,从而实现路由功能
4.Nginx的安装(Windows)
1.解压压缩文件
- conf:配置目录
- contrib:第三方依赖
- html:默认的静态资源目录,类似于tomcat的webapps
- logs:日志目录
- nginx.exe:启动程序。可双击运行,但不建议这么做。
5.Nginx作为服务器的使用
1.编辑nginx.conf配置文件,添加如下配置
2.在本地E:/test下创建index.html
3.修改hosts文件
4.启动nginx
6.Nginx作为反向代理
1.先启动一个web项目
2.在hosts文件中添加一个配置
3.在nginx.conf文件中添加server配置
4.重启nginx进行测试
7.nginx作为负载均衡使用
1.一个web项目以不同的端口启动两个
2.修改nginx.conf的配置文件
8.Docker 安装 Nginx
1. 创建要挂载的配置目录
mkdir -p /mydata/nginx/conf
2. 启动临时nginx容器
docker run -p 80:80 --name nginx -d nginx:1.10
3. 拷贝出 Nginx 容器的配置
# 将nginx容器中的nginx目录复制到本机的/mydata/nginx/conf目录
docker container cp nginx:/etc/nginx /mydata/nginx/conf
# 复制的是nginx目录,将该目录的所有文件移动到 conf 目录
mv /mydata/nginx/conf/nginx/* /mydata/nginx/conf/
# 删除多余的 /mydata/nginx/conf/nginx目录
rm -rf /mydata/nginx/conf/nginx
4. 删除临时nginx容器
# 停止运行 nginx 容器
docker stop nginx
# 删除 nginx 容器
docker rm nginx
5. 启动 nginx 容器
docker run -p 80:80 --name nginx \
-v /mydata/nginx/html:/usr/share/nginx/html \
-v /mydata/nginx/logs:/var/log/nginx \
-v /mydata/nginx/conf/:/etc/nginx \
-d nginx:1.10
6. 设置 nginx 随 Docker 启动
docker update nginx --restart=always
7.配置代理
server {
listen 80;
server_name product.com;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
proxy_pass http://192.168.56.1:10001;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
8.反向代理:nginx 代理网关由网关进行转发
1.修改 http 块,配置上游服务器为网关地址
vi /mydata/nginx/conf/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
upstream product{
server 192.168.56.1:88;
}
include /etc/nginx/conf.d/*.conf;
}
2.修改配置文件
配置代理地址为上面配置的上游服务器名
server {
listen 80;
server_name product.com;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
proxy_set_header Host $host;
proxy_pass http://product;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}