Nginx学习笔记
2020-07-14 05:19
204 查看
简介
是什么?
是一个高性能的HTTP和反向代web服务器,同时也提供了IMAP/POP3/SMTP服务。 是一款轻量级的Web服务器/反向代理服务器及电子邮件代理服务器,再BSD-like协议下发性。其特点是占有内存少,并发能力强。
Nginx的安装
在官网上下载压缩包放入Linux/opt目录下 http://nginx.org/en/download.html
相关依赖
安装前可以先检查一下是否已经安装 借鉴博客:https://www.cnblogs.com/xxoome/p/5866553.html 安装依赖:根据自己检查删减安装 yum -y install gcc pcre-devel zlib-devel openssl openssl-devel
解压Nginx压缩包
tar -zxvf nginx-1.18.0.tar.gz
进入Nginx文件
cd nginx-1.18.0
./configure产生Makefile文件
执行make如果没有发生错误就执行make install
进入/usr/local
启动Nginx
在 /usr/local/nginx/sbin/ 目录下 ./nginx
查看进程
常用Nginx命令
注意:必须进入cd /usr/local/nginx/sbin/目录才能使用命令 1.查看nginx版本号 ->./nginx -v 2.启动nginx ->./nginx 3.关闭nginx ->./nginx -s stop 4.重新加载nginx ->./nginx -s reload
配置文件
位置
第一部分
从配置文件开始到events块之间的内容, 主要会设置一些影响nginx服务器整体运行的配置指令, 主要包括配置运行Nginx服务器的用户(组), 允许生成的worker process数, 进程PID存放路径、 日志存放路径和类型以及配置文件的引入数
#user nobody; #这是Nginx服务器并发处理服务的关键配置,值越大,可以支持的并发处理量也越多,但是会收到硬件软件等设备的制约 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块涉及的指令主要影响Nginx服务器与用户的网络连接, 常用的设置包括是否开启对多work process下的网络连接进行序列化, 是否允许同时接受多个网络连接, 选取哪种事件驱动模型来处理连接请求, 每个word process 可以同时支持的最大连接数 这部分的配置对Nginx的性能影响较大,在实际中应该灵活配置
events { #work process 支持最大连接数1024 worker_connections 1024; }
第三部分
代理,缓存和日志定义等绝大多数功能和第三方模块的配置都在这里。 注意:http块也可以包括http全局快、server块 http全局块:包括文件引入、MIME-TYPE定义、日志自定义、连接超时事件、单连接请求数上线 server块:这块和虚拟主机有密切关系,虚拟主机从用户角度看,和一台独立的硬件主机是完全一样的,该技术的产生是为了姐生互联网服务器硬件成本。每个http块可以包括多个server块,而每个server块就相当于一个虚拟主机,而每个server块也分为全局server块,以及可以同时包含多个location块 1.全局server块:最常见的配置是本虚拟机主机的监听配置和本虚拟主机的名称或IP配置 2.location块:最主要的作用是基于Nginx服务器接收到的请求字符串,对虚拟主机名称(也可以是IP别名)之外的字符串(例如/uri-string)进行匹配,对特定的请求进行处理,地址定向,数据缓存和应答控制等功能,还有许多第三方模块的配置也在这里进行
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 { # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #}
正向代理
反向代理
反向代理,其实客户端对代理是无感知的,因为客户端不需要任何配置就可以访问,我们只需要将请求发送到反向代理服务器,由反向代理服务器去选择目标服务器获取数据后,在返回给客户端,此时反向代理服务器和目标服务器对外就是一个服务器,暴露的是代理服务器地址,隐藏了真实服务器IP地址。
配置
实现效果: 1.打开浏览器,浏览器输入地址,跳转到linuxTomca主页面
具体实现: 1.在windows系统的host文件进行域名和ip对应关系的映射->ip 域名 2.在nginx进行请求转发的配置(反向代理) 1)server_name 改成ip地址 2)location块加上 跳转地址 proxy_pass http://ip:port
如果有多个反向代理实例
负载均衡
单个服务器解决不了, 我们增加服务器的数量, 然后将请求分发到各个服务器上, 将原先请求集中到单个服务器的情况改为将请求分发到多个服务器上, 将负载分发到不同的服务器上。
实现负载均衡
负载均衡策略
1.轮询(默认):每个请求按时间顺序逐一分配到不同的后端服务器,如果dowm掉,会自动剔除 2.weight:权重,值越高被分配的客户越多。指轮询几率。 3.ip_hash:每个请求按方位ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session问题 4.fair:按后端服务器的响应时间来分配请求,响应时间短的优先分配
动静分离
为了加快网站的解析速度,可以把动态页面和静态页面由不同的服务器来解析,加快解析速度。降低原来单个服务器的压力。
配置
高可用集群
1.需要多台Nginx服务器 2.需要keepalived 3.需要虚拟IP
安装keepalived
yum install keepalived -y 在etc生成keepalived目录,有配置文件
修改keepalivec.conf配置文件
global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.17.129 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_http_port { script "/usr/local/src/nginx_check.sh" interval 2 #(检测脚本执行的间隔) weight 2 } vrrp_instance VI_1 { state BACKUP # 备份服务器上将 MASTER 改为 BACKUP interface ens33 //网卡 virtual_router_id 51 # 主、备机的 virtual_router_id 必须相同 priority 90 # 主、备机取不同的优先级,主机值较大,备份机值较小 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.17.50 // VRRP H 虚拟地址 } }
脚本代码
脚本文件 #!/bin/bash A=`ps -C nginx –no-header |wc -l` if [ $A -eq 0 ];then /usr/local/nginx/sbin/nginx sleep 2 if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then killall keepalived fi fi
启动Nginx和keepalived
systemctl start keepalived.service
高可用配置文件
keepalivec.conf
#全局配置 global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.17.129 smtp_connect_timeout 30 router_id LVS_DEVEL ->访问到主机 #在etc/host文件中 127.0.0.1 名字 } #检测脚本配置 vrrp_script chk_http_port { script "/usr/local/src/nginx_check.sh" #脚本文件地址 interval 2 #(检测脚本执行的间隔) weight 2 #权重 } #虚拟IP配置 vrrp_instance VI_1 { state BACKUP # 备份服务器上将 MASTER 改为 BACKUP interface ens33 //网卡 ->ifconfig可以查看 virtual_router_id 51 # 主、备机的 virtual_router_id 必须相同 priority 90 # 主、备机取不同的优先级,主机值较大,备份机值较小 advert_int 1 #每隔多少时间检测心跳 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.17.50 // VRRP H 虚拟地址 } }
脚本文件
#!/bin/bash A=`ps -C nginx –no-header |wc -l` if [ $A -eq 0 ];then /usr/local/nginx/sbin/nginx #启动脚本的位置 sleep 2 if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then killall keepalived fi fi
Nginx原理
Work工作
多个work的好处
(1)可以使用 nginx –s reload 热部署,利用 nginx 进行热部署操作 (2)每个 woker 是独立的进程,如果有其中的一个 woker 出现问题,其他 woker 独立的, 继续进行争抢,实现请求过程,不会造成服务中断
设置多少个worker合适
和CPU数一致最佳
连接数work_connection
发送一个请求,占用了2或4个work连接数
相关文章推荐
- 学习笔记(05):高并发下的Nginx性能优化实战-Nginx负载均衡(二)
- nginx 学习笔记(一)nginx 编译安装
- nginx 源码学习笔记(十九)—— nginx启动过程函数调用图
- nginx 源码学习笔记(八)——基本容器——array数组
- nginx学习笔记之一:nginx介绍及其编译安装
- nginx学习笔记一
- 学习笔记0516----nginx配置
- nginx学习笔记
- 【知了堂学习笔记】Nginx+Tomcat实现简单负载均衡
- nginx 学习笔记(三)基本数据结构
- nginx 学习笔记(三)基本数据结构
- nginx学习笔记
- nginx 学习笔记(二)nginx 配置相关
- nginx 学习笔记(2) nginx新手入门
- Nginx学习笔记——进程代码片段(一)
- 【学习笔记】nginx静态页面部署
- nginx 源码学习笔记(七)
- nginx 源码学习笔记(二)——nginx精粹-模块
- nginx 源码学习笔记(六)——nginx基本数据结构
- nginx 源码学习笔记(八)——基本容器——array数组