定义:
Nginx 是一个高性能的HTTP和反向代理web服务器,核心特点是占内存少
,并发能力强
应用场景:
- Http服务器(web服务器):性能高,注重效率,能经受高负载的考验。
- 反向代理服务器:浏览器发的送请求先到Nginx服务器,由Nginx选择原始服务器提供服务响应结果。
- 负载均衡服务器:当某个应用的每天要处理的请求数量非常多,需要多个服务器时,Nginx可以将服务器组织起来,一起工作;并且能够通过一些策略平衡各个服务器的访问压力。
- 动静分离:将静态资源和动态资源分别放到不同类型的服务器上(擅长静态资源处理的:ngxin,apache;擅长动态资源的tomcat),各自发挥自己的优势点,提高响应速度。
Nginx特点:
- 跨平台:可以在大多数类Unix操作系统上编译运行。
- 上手容易,配置也比较简单
- 高并发,性能好
- 稳定性好,宕机概率低
Centos 上Nginx的安装:
- 下载nginx安装包文件到linux平台,此处以1.18为例
wget http://nginx.org/download/nginx-1.18.0.tar.gz
- 安装Nginx依赖,pcre、openssl、gcc、zlib
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
- 解压Nginx软件包(推荐解压到
/opt
[第三方软件安装位置]文件夹下)
tar -zxvf nginx-1.18.0.tar.gz -C /opt/
- 进入到解压后的目录下,依次执行
./configure
,make
,make install
执行完毕后,会在/usr/local/
(/usr/local
目录主要存放那些手动安装的软件)下产生一个nginx目录 - 进入
/usr/local
->sbin
目录,执行nginx
命令 - 通过浏览器访问该服务器的ip即可看到nginx的欢迎页(nginx服务器默认监听80端口)
Nginx主要命令:
./nginx
启动nginx./nginx -s stop
终止nginx./nginx -s reload
重新加载nginx.conf
配置文件
Nginx配置文件解读:
- 全局块
- 影响nginx服务器的整体运行,比如worker进程数(一般可cpu数相等)、错误日志位置等内容
- event块
- 主要影响nginx服务器与用户的网络连接
⽐如worker_connections 1024,标识每个workderprocess⽀持的最⼤连接数为1024
- 主要影响nginx服务器与用户的网络连接
- http块
- 配置最频繁的部分,配置虚拟机,监听端口配置,请求转发,负载均衡等
http { # 引入mime类型定义文件 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压缩 #gzip on; server { # 监听的端口 listen 80; # 使用localhost访问 server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; # 默认请求 location / { root html; #默认的网站是根目录 index index.html index.htm; #欢迎页 } # 定义404错误页面位置 #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; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # 反向代理配置(可以配置多个根绝location 后的语法进行匹配) #location \abc { # proxy_pass http://127.0.0.1:8080; #} #location \def { # proxy_pass http://127.0.0.1:8081; #} } }
-
反向代理:在机器上配置两个tomcat,修改nginx.conf文件如下
location \abc { proxy_pass http://127.0.0.1:8080; } location \def { proxy_pass http://127.0.0.1:8081; }
location 语法
location [=|~|~*|^~] /uri/ { … }
1)正则匹配 location ~ /lagou { }
2)不区分⼤⼩写的正则匹配 location ~* /lagou { }
3)匹配路径的前缀 location ^~ /lagou { }
4)精确匹配 location = /lagou { }
5)普通路径前缀匹配 location /lagou { }
优先级: 4 > 3 > 2 > 1 > 5 -
负载均衡配置
- 轮询,upstream中不配置内容即为轮询:
upstream lagouServer{ server 111.229.248.243:8080; server 111.229.248.243:8082; } location /abc { proxy_pass http://lagouServer/; }
- weight权重
upstream lagouServer{ server 111.229.248.243:8080 weight=1; server 111.229.248.243:8082 weight=2; }
- ip_hash:请求按照ip的hash结果分配,每⼀个客户端的请求会固定分配到同⼀个⽬标服务器处理(无session问题)
upstream lagouServer{ ip_hash; server 111.229.248.243:8080; server 111.229.248.243:8082; }
- 轮询,upstream中不配置内容即为轮询:
-
动静分离
# 静态资源处理,直接去nginx服务器目录中加载 location /static/{ # staticData可以是一个配置好的upstream 也可以是nginx 本地的一个文件夹 root staticData; }