Nginx基础入门
1 Nginx简介
1.1 为什么要使用Nginx
如果将前端项目资源也放置在tomcat中部署,那么在访问一些静态资源的时候就需要访问单tomcat,增加服务器压力(本身tomcat服务器处理能力有限),所以我们需要一台更高效的静态资源服务器
随着项目访问量的增加,tomcat并不能满足要求,tomcat 的并发量大概在200左右。这时候我们需要搭建集群,来处理并发问题。所以我们需要一台高效的反向代理服务器
1.2 什么是Nginx
Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。
1.3 Nginx特点
- 稳定性极强,可以实现7*24不间断运行
- 提供简洁、直观的配置
- 占用内存很小、并发能力很强(5w+)
2 Nginx的相关概念
2.1 正向代理
正向代理,意思是一个位于客户端和原始服务器(origin server)之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原始服务器转交请求并将获得的内容返回给客户端。客户端才能使用正向代理。
2.2 反向代理
向代理服务器位于用户与目标服务器之间,但是对于用户而言,反向代理服务器就相当于目标服务器,即用户直接访问反向代理服务器就可以获得目标服务器的资源。同时,用户不需要知道目标服务器的地址,也无须在用户端作任何设定。反向代理服务器通常可用来作为Web加速,即使用反向代理作为Web服务器的前置机来降低网络和服务器的负载,提高访问效率。
2.3 动静分离
随着前后端分离开发, 我们也可以实现前后端项目的分离部署,即将前端项目部署到Nginx上直接进行访问,后端资源通过Nginx反向代理进行访问
2.4 负载均衡
通过负载均衡策略,分发请求到集群中的不同tomcat服务器上
3 Nginx安装
3.1 Linux安装
3.1.1 安装Nginx所需环境
1、安装gcc
yum install -y gcc-c++
2、安装PCRE pcre-devel
yum install -y pcre pcre-devel
3、安装zlib
yum install -y zlib zlib-devel
4、安装Open SSL
yum install -y openssl openssl-devel
3.1.2 安装Nginx
下载地址:http://nginx.org/en/download.html
1、下载并解压
# 将nginx-1.18.0.tar.gz上传到linux中
# 将nginx-1.18.0.tar.gz解压到 /usr/local下
tar -zxvf nginx-1.18.0.tar.gz -C /usr/local
2、编译安装
# 进入nginx目录
cd /usr/local/nginx-1.18.0
# 生成makefile文件
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre
# 编译 安装
make && make install
3、启动运行
# 进入nginx目录
cd /usr/local/nginx/sbin
# 查看nginx版本
./nginx -v
#启动nginx
./nginx
# 停止nginx
./nginx -s stop
#重新加载nginx.conf配置文件
./nginx -s reload
4、测试访问
http://ip(默认端口为80) |
---|
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XRmJ4jrN-1684291911230)(image\image-20210905210221704.png)] |
3.2 window安装
1、下载nginx window版本
http://nginx.org/en/download.html
2、执行根目录下
nginx.exe
启动
3、测试访问 http://localhost
4 Nginx配置文件
4.1 配置文件
#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;
#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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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 html;
}
}
}
4.2 全局配置
#user nobody;
# 设置worker进程数(默认为1 一般设置为CPU核心数)
worker_processes 1;
# 配置nginx运行日志文件
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
# 配置Nginx进程的进程号的存储文件
#pid logs/nginx.pid;
4.3 events配置
# 配置nginx连接数,数值越大并发能力越强
events {
worker_connections 100000; # 默认1024
}
worker_connections不是随便设置的,而是与两个指标有重要关联,一是内存,二是操作系统级别的“进程最大可打开文件数”。
内存:每个连接数分别对应一个read_event、一个write_event事件,一个连接数大概占用232字节,2个事件总占用96字节,那么一个连接总共占用328字节。
通过数学公式可以算出100000个连接数大概会占用 31M = 100000 * 328 / 1024 / 1024,当然这只是nginx启动时,connections连接数所占用的nginx。
进程最大可打开文件数:进程最大可打开文件数受限于操作系统,可通过 ulimit -n 命令查询,以前是1024,现在是65535,nginx提供了worker_rlimit_nofile指令,这是除了ulimit的一种设置可用的描述符的方式。 该指令与使用ulimit对用户的设置是同样的效果。此指令的值将覆盖ulimit的值,如:worker_rlimit_nofile 20960;
nginx的并发数计算公式:
- nginx的并发数 = worker_processes*worker_connections/(4或者2)
- 动态资源除以4
- 静态资源除以2
4.4 http配置
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
# nginx监听端口
listen 80;
# nginx服务名称
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# 路径映射匹配
location / {
# 设置根目录
root html;
# 设置默认访问的网页
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
# 指定错误页面 /aaaa
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}