Nginx
一、什么是Niginx
高性能的HTTP
反向代理Web服务器
二、特点
占用内存少
并发能力强,支持高达50000个并发连接数的响应
三、功能
反向代理
正向代理:代理客户端(如VPN)
反向代理:代理服务器(如Niginx)
反向代理相当于是为目标服务器工作的,当你去访问某个网站时,你以为你访问问的是目标服务器,其实不然,当你访问时,其实是由一个代理服务器去接收你的请求
正向代理与反向代理最简单的区别:
正向代理隐藏的是用户,反向代理隐藏的是服务器
负载均衡
Nginx负载均衡策略有两种:
内置策略:轮询、加权轮询、IP hash
扩展策略
动静分离
动静分离,在我们的软件开发中,有些请求是需要后台处理的,有些情求是不需要经过后台处理的(如:cs、 html、 jpg、js等等文件),这些不需要经过后台处理的文件称为静态文件。让动态网站里的动态网页根据一定规则把不变的资源和经常变的资源区分开来,动静资源做好了拆分以后,我们就可以根据静态资源的特点将其做缓存操作。提高资源响应的速度。
四、下载(Windows版本)
网址:http://nginx.org/en/download.html
解压完成后,打开conf文件夹,点击nginx.conf,查看监听端口。
五、运行nginx
方法一:双击nginx.exe
方法二:打开cmd,输入nginx.exe
结果如图,运行nginx成功。
六、Nginx配置文件结构
文件路径:(conf/nginx.conf)
1、全局块
和Nginx运行相关的全局配置
worker_processes 1;
2、events块
和网络连接相关的配置
events {
worker_connections 1024;
}
3、http块
代理、缓存、日志记录、虚拟主机配置
注意:http块中可以配置多个Server块,每个Server块中可以配置多个location块
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
}
}
3.1 http全局块
include mime.types;
default_type application/octet-stream;
sendfile on;
3.2 Server块
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
3.2.1 Server全局块
listen 80;
server_name localhost;
3.2.2 location块
location / {
root html;
index index.html index.htm;
}
整体结构