01 nginx简介
又称 engin x,是一个高性能的 HTTP和反向代理服务器
特点
- 内存少,专为性能优化开发
- 并发能力强,能经受高负载考验
02 nginx概念
正向和反向代理
正向代理
Nginx同样可以用作正向代理进行上网。
正向代理: 典型的就是VPN上网,需要在客户端配置代理服务器
反向代理
客户端对代理无感知,客户端无需做任何配置,
由反向代理服务器选择目标服务器,反向代理服务器和目标服务器就是一个服务器,
暴露代理服务器地址,隐藏目标服务器地址
负载均衡
单个服务器性能低,可以增加服务器的数量,将请求分发到各个服务器上,也就是负载均衡。
动静分离
为了加快网站的解析速度,可以把动态页面和静态页面由不同的服务器解析。
03 nginx的安装和使用
3.1 nginx的安装
3.2 nginx常用命令
前提条件: 必须要进入nginx目录。
具体目录位置:
命令:
- 查看版本号
./nginx -v
- 查看进程
ps -ef | grep nginx
- 启动nginx
./nginx
- 关闭nginx
./nginx -s stop
- 重新加载nginx(特别是修改nginx.conf之后)
./nginx -s reload
3.3 nginx配置文件
配置文件位置: /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/conf/nginx.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| worker_processes 1;
events { worker_connections 1024; }
http { include mime.types; default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server { listen 8080; server_name localhost;
location / { root html; index index.html index.htm; }
error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
}
|
从开始到events块之间的内容,
影响nginx服务器的整体运行
1 2 3 4 5 6 7 8 9
| #user nobody; worker_processes 1; # 表示nginx服务器并发处理的数量,越大表示处理并发量越多
#error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info;
#pid logs/nginx.pid;
|
影响Nginx服务器与用户网络的连接
1 2 3
| events { worker_connections 1024; }
|
nginx服务器中配置最频繁的部分,
代理、缓存、日志绝大多数功能和第三方模块都在这,
http块包括:
- http全局块
- 包括文件引入、MIME-TYPE定义、日志自定义、连接超时时间、单链接请求数上限等
- server块
- 和虚拟主机有密切关系
- 又包括全局server和location
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| http { include mime.types; default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server { listen 8080; server_name localhost;
location / { root html; index index.html index.htm; }
error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
}
|
04 nginx配置实例
4.1 反向代理
4.2 负载均衡
4.3 动静分离
05 nginx原理
本文产自🐙足八桑🐙肚子里了剩无几的墨水,转载请注明出处