点击蓝字关注我们
![00139e0a28fcbbe7d89cd4e0a19c4aea.png](https://img-blog.csdnimg.cn/img_convert/00139e0a28fcbbe7d89cd4e0a19c4aea.png)
NGINX的安装
概念:
Nginx以事件驱动的方式编写,所以有非常好的性能,同时也是一个非常高效的反向代理、负载平衡服务器。在性能上,Nginx占用很少的系统资源,能支持更多的并发连接,达到更高的访问效率;在功能上,Nginx是优秀的代理服务器和负载均衡服务器;在安装配置上,Nginx安装简单、配置灵活。
Nginx支持热部署,启动速度特别快,还可以在不间断服务的情况下对软件版本或配置进行升级,即使运行数月也无需重新启动。
Nginx的安装:
方法一
正式开始前,要先安装好编译环境gcc g++开发库#安装make
yum -y install gcc automake autoconf libtool make
#安装gcc++
yum install gcc gcc-c++
1.选择一个目录,用于保存安装的文件
cd /usr/local/src
2.安装PCRE库
cd /usr/local/srcwget https://ftp.pcre.org/pub/pcre/pcre-8.44.tar.gz tar -zxvf pcre-8.44.tar.gzcd pcre-8.44./configuremakemake install
3.安装zlib库
cd /usr/local/srcwget http://zlib.net/zlib-1.2.11.tar.gztar -zxvf zlib-1.2.11.tar.gzcd zlib-1.2.11./configuremakemake install
4.安装ssl
cd /usr/local/srcwget https://www.openssl.org/source/openssl-1.1.1g.tar.gztar -zxvf openssl-1.1.1g.tar.gz
5.开始Nginx的安装
cd /usr/local/srcwget http://nginx.org/download/nginx-1.18.0.tar.gztar -zxvf nginx-1.18.0.tar.gzcd nginx-1.18.0./configure --sbin-path=/usr/local/nginx/nginx \--conf-path=/usr/local/nginx/nginx.conf \--pid-path=/usr/local/nginx/nginx.pid \--with-http_gzip_static_module \--with-http_stub_status_module \--with-file-aio \--with-http_realip_module \--with-http_ssl_module \--with-pcre=/usr/local/src/pcre-8.44 \--with-zlib=/usr/local/src/zlib-1.2.11 \--with-openssl=/usr/local/src/openssl-1.1.1gmake -j2make install
安装成功后
会在/usr/local/nginx下产生一个目录
启动Nginx
/usr/local/nginx/nginx
查看是否启动成功
ps -ef | grep nginx
启动成功,会出现
root 32708 1 0 11:12 ? 00:00:00 nginx: master process /usr/local/nginx/nginxnobody 32709 32708 0 11:12 ? 00:00:00 nginx: worker processroot 32711 9607 0 11:13 pts/1 00:00:00 grep --color=auto nginx
或者在windows浏览器访问 ,Linux的IP地址,前提是防火墙放行
![c9e1360a3c8a489a42de3e0ac18135df.png](https://img-blog.csdnimg.cn/img_convert/c9e1360a3c8a489a42de3e0ac18135df.png)
Nginx的安装
方法二
直接使用docker下载
docker pull nginx/unit docker run -d nginx/unit
配置文件
#user nobody;
#工作进程数,通常等于CPU数量或两倍
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服务器的最大连接数为: # worker_processes * worker_connections worker_connections 1024; }设定http服务器,利用它的反向代理功能提供负载均衡支持http { # 设置请求包含的内容,存放在mime.types 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"'; # $remote_addr:获取到的是反向代理服务器的IP地址 # $http_user_agent:用来记录客户浏览器的相关信息 # 因为nginx是一个网关控制中心,如果我们开启了反向代理,那么我们是先去访问 # nginx,然后由nginx对请求进行转发,这样我们就获取不到客户的IP地址 # 可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址 # 开启了上面日志,就要开启这个配置,来存放日志 #access_log logs/access.log main; # 设定通过nginx上传文件的大小 client_max_body_size 300m; sendfile on; #tcp_nopush on; # keepalive超时时间。 #keepalive_timeout 0; keepalive_timeout 65; #后端服务器连接的超时时间_发起握手等候响应超时时间 proxy_connect_timeout 90; #连接成功后_等候后端服务器响应时间_ #其实已经进入后端的排队之中等候处理(也可以说是后端服务器处理请求的时间) proxy_read_timeout 180; #后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据 proxy_send_timeout 180; #这三个超时时间,就好比,你在网上购物, #proxy_connect_timeout:你买了商品,商家给你投递的时间 #prt:快递在途中运输的时间 #pst:退货时,快递运输的时间 #三者只要有其中一个超时,都会导致这场交易不能够顺利完成 #gzip on; #负载均衡 upstream name{ ip_hash; server 127.0.0.1:9090 down; server 127.0.0.1:8080 weight=2; server 127.0.0.1:6060; server 127.0.0.1:7070 backup; fair; } #格式 upstream 名字{ [负载均衡设备] [IP] } # name: 该服务的名称 # dowm: 该服务器不参与负载均衡 # weight: 开启权重模式,根据权重大小,来对指定访问服务器的比重 # ip_hash: 表示每个请求按访问ip的hash结果分配,相同的ip只能访问同一个后端服务器 # fair: 按后端服务器的响应时间来分配请求,响应时间短的优先分配 #四种分配策略: #1.默认轮询: 每个请求按时间顺序逐一分配到不同的后端服务器中,如果后端服务器宕机,会被自动剔除 #2.weight权重 #3.ip_hash #4.fair # 在需要负载均衡的server中添加proxy_pass http://upstream的服务名称/; # 配置服务 server { # 监听80端口 listen 80; # 监听的服务名称 server_name localhost; # 设置字节编码 #charset koi8-r; #access_log logs/host.access.log main; # 定义拦截规则 location / { root html; index index.html index.htm; } proxy_pass http://upstream的服务名称/; #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 ~ \.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; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #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; # } #}}
![ddf768913771f085dda1aae449a4c3ce.png](https://img-blog.csdnimg.cn/img_convert/ddf768913771f085dda1aae449a4c3ce.png)
Nginx高可用
Nginx高可用
1.准备多台虚拟机
2.安装nginx,keepalived
使用yum命令下载安装
yum install keepalived -y
3.会在/etc/keeplived生成keeplived.conf
! Configuration File for keepalived
global_defs { notification_email { #指定keeplived在发生切换时发送emaik到下列对象 acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc #指定邮件发送人 smtp_server localhost smtp_connect_timeout 30 router_id LVS_DEVEL}#vrrp_sync_group VG_1{ #监控多个网段的实例# group {# inside_network #实例名# outside_network# }# notify_master /path/xx.sh #指定当切换到master时,执行的脚本# notify_backup /path/xx.sh #指定当切换到backup时,执行的脚本# notify_fault "path/xx.sh VG_1" #故障时执行的脚本# notify /path/xx.sh# smtp_alert #使用global_defs中提供的邮件地址和smtp服务器发送邮件通知#}vrrp_script chk_http_port { #脚本配置 script "/usr/local/src/nginx_check.sh" #脚本所在的路径 interval 2 #执行脚本的间隔时间 weight 2 #该服务器的权重}vrrp_instance VI_1 { state MASTER #备份服务器修改成BACKUP interface eth33 #这个虚拟机使用的网卡 virtual_router_id 51 #主,备机的值必须一样,这个是它们的集群的唯一标识 priority 100 #优先级 主机需要大于从机 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_http_port #(调用检测脚本) } virtual_ipaddress { #虚拟ip 这个ip会与当前主机的ip进行一个绑定 vip 192.168.44.200 }}
脚本文件
#!/bin/bashA=`ps -C nginx --no-header |wc -l`if [ $A -eq 0 ];then /usr/local/webserver/nginx/sbin/nginx sleep 2 if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then killall keepalived fifi
启动keepalived,会执行脚本文件,会检测到nginx是否启动,未启动的情况下,启动nginx
systemctl start keepalived.service
使用 ps -ef | grep nginx
命令验证
启动时可能碰到的错误:
可能是脚本文件中的路径不对;权限不够;配置实例中缺少调用脚本命令解决方法:修改路径:修改脚本命令中的路径修改权限:进入到脚本路径下 执行chmod 755 脚本文件名添加调用脚本:track_script { chk_http_port #(调用检测脚本)}错误2:Disabling track script chk_http_port due to insecureglobal_defs { #添加 enable_script_security #开启脚本安全设置}错误3:setroubleshootd引起的内存不足解决方法:vi /etc/selinux/config#SELINUX=enforce #注释掉#SELINUXTYPE=targeted #注释掉SELINUX=disabled #增加#保存 重启生效
为方便五湖四海的Linux爱好者共同
学(吹)习(牛)讨(打)论(趣)
热烈欢迎大家加入专属QQ群
![c761ee9265cabc7ed98996a119a75559.png](https://img-blog.csdnimg.cn/img_convert/c761ee9265cabc7ed98996a119a75559.png)
群内定时上传资料还有老师全天答疑
入群即可免费获得
1、必备工具源码包
2、内部精选电子书
3、运维资料大礼包
欲知更多福利,欢迎你的加入
![6572080c8f5e06c08b5e11f1b0fbfcd1.png](https://img-blog.csdnimg.cn/img_convert/6572080c8f5e06c08b5e11f1b0fbfcd1.png)
![110f75688906a2f0cdf66baf39b14e0c.gif](https://img-blog.csdnimg.cn/img_convert/110f75688906a2f0cdf66baf39b14e0c.gif)
更多精彩内容“阅读原文”
右边给我一朵小花花
![88aa1bd41928fa184d092067ff407bff.gif](https://img-blog.csdnimg.cn/img_convert/88aa1bd41928fa184d092067ff407bff.gif)