Nginx网站服务 升级

一、简介

1、Nginx概述

Nginx:

Nginx是一个高性能的HTTP和反向代理服务器。是一款轻量级的高性能的web服务器/反向代理服务器/电子邮件(IMAP/POP3)代理服务器,单台物理服务器可支持30 000~50 000个并发请求。

Apache:

Apache是以进程为基础的结构,进程要比线程消耗更多的系统开支,不太适用于多处理器环境,因此,在一个apache Web站点扩容时,通常是增加服务器或扩充群集节点而不是增加处理器。

2、Nginx和Apache的优缺点比较

(1)nginx相对于apache的优点:
轻量级,同样起web服务,比apache占用更少的内存及资源抗并发,nginx处理请求是异步非阻塞的,而apache是阻塞型的在高并发下,nginx能保持低资源低消耗高性能。高度模块化的设计,编写模块相对简单。

(2)apache相对于nginx的优点∶

Rewrite比nginx的rewrite强大 (rewrite的主要功能就是实现统一资源定位符URL的跳转)

模块多,基本想到的都可以找到、少bug, nginx的bug相对较多、超稳定。

一般来说,需要性能的web服务,用nginx。若不需要性能只求稳定,就选用apache。

3、Nginx和Apache最核心的区别

apache是同步多进程模型,一个连接对应一个进程,nginx是异步的,多个连接可以对应一个进程。

Nginx处理静态文件好, 耗费内存少,只适合静态和反向。

Apache在处理动态有优势。

nginx并发性比较好, CPU占用内存低,如果rewrite频繁,选用apache最佳。

二、Linux中的I/O

I/O在计算机中指Input/Output,lOPS (Input/Output Per Second)即每秒的输入输出量(或读写次数),是衡量磁盘性能的主要指标之一。IOPS是指单位时间内系统能处理的I/O请求数量,一般以每秒处理的IO请求数量为单位,I/O请求通常为读或写数据操作请求。

同步/异步:注的是消息通信机制,即调用者在等待一件事情的处理结果时,被调用者是否提供完成状态的通知。

同步: synchronous,被调用者并不提供事件的处理结果相关的通知消息,需要调用者主动询问事情是否处理完成

异步: asynchronous,被调用者通过状态、通知或回调机制主动通知调用者被调用者的运行状态。

阻塞/非阻塞: 关注调用者在等待结果返回之前所处的状态。

阻塞: blocking,指I/O操作需要彻底完成后才返回到用户空间,调用结果返回之前,调用者被挂起,干不了别的事情。

非阻塞: nonblocking,指IO操作被调用后立即返回给用户一个状态值,而无需等到IO操作彻底完成,在最终的调用结果返回之前,调用者不会被挂起,可以去做别的事情。

三、Nginx编译安装详细

1、关闭防火墙、安装依赖关系

1. #关闭防火墙
[root@localhost opt]#systemctl stop firewalld
[root@localhost opt]#setenforce 0
 
2. #安装依赖关系包
[root@localhost opt]#yum -y install gcc pcre-devel openssl-devel zlib-devel

2、新建用户nginx便于管理

#新建用户 nginx 服务程序默认 以 nobody 身份运行,建议为其创建专门的用户账户,以便更准确的控制访问权限
[root@localhost opt]#useradd -M -s /sbin/nologin nginx

3、将压缩包传入到/opt目录下,编译安装

4.#切换至opt目录,将下载好的压缩包传进来
[root@localhost opt]#cd /opt
[root@localhost opt]#ls
nginx-1.18.0.tar.gz
 
5.#解压文件
[root@localhost opt]#tar -zxf nginx-1.18.0.tar.gz 
[root@localhost opt]#ls
nginx-1.18.0  nginx-1.18.0.tar.gz
 
6.#切换至解压后的文件夹编译
[root@localhost opt] mkdir /apps/nginx -p
[root@localhost opt] cd /nginx-1.18.0
[root@localhost nginx-1.18.0]#
./configure --prefix=/apps/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module
 
//解释
--prefix=/usr/local/nginx \
#安装路径
--user=nginx \
#指定用户名
--group=nginx \
#指定用户组
--with-http_stub_status_module
#启用此模块支持状态统计
//
 
7.#安装
[root@localhost nginx-1.18.0]#make && make install -j4

4、做软连接并启动nginx

8.  #修改权限
chown -R nginx.nginx /apps/nginx 

9. #做软连接,让系统识别nginx的操作命令
[root@localhost sbin]#ln -s /apps/nginx/sbin/nginx /usr/sbin/
 
10. #检查配置文件是否配置 正确 
[root@localhost sbin]#nginx -t

5、创建nginx自启动文件

1.vim /usr/lib/systemd/system/nginx.service
#建立文件
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/apps/nginx/run/nginx.pid
#注意文件位置,如果不对 启动不了
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
#注意启动文件位置
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target


2.[root@localhost sbin]#mkdir /apps/nginx/run/
#创建目录
3.[root@localhost sbin]#vi /apps/nginx/conf/nginx.conf
#修改配置文件
pid   /apps/nginx/run/nginx.pid;
#找到 pid的位置修改
4.[root@localhost opt]#nginx -t
#测试配置是否有问题
5.[root@localhost sbin]#systemctl daemon-reload
#重新加载配置 
6.[root@localhost sbin]#systemctl restart nginx.service
 #重新启动ngin服务

6、验证

在这里插入图片描述

四、Nginx访问控制

1、nginx服务的主配置文件

[root@localhost system]#vim /usr/local/nginx/conf/nginx.conf

2、全局配置

#user  nobody;                     ##运行用户
worker_processes 1;                ##工作进程数,可配置成服务器内核数*2,如果网站访问量不大,一般设为1就够用了

#error_log  logs/error.log;        ####错误日志文件的位置
#pid        logs/nginx.pid;        ####PID文件的位置

在这里插入图片描述

3、I/O事件配置

events {
    use epoll;                 #使用 epoll 模型以提高性能,2.6 以上版本建议使用
    worker_connections  4096;   #每个进程处理4096个连接
}

在这里插入图片描述
epoll(socket描述符)是Linux内核]为处理大批量文件描述符而作了改进的poll,是Linux下多路复用IO接口select/poll的增强版本,它能显著提高程序在大量并发连接中只有少量活跃的情况下的系统CPU利用率
若工作进程数为 8,每个进程处理 4 096 个连接,则允许 Nginx 正常提供服务的连接数 已超过 3 万个(4 096×8=32 768),当然具体还要看服务器硬件、网络带宽等物理条件的性 能表现。
如提高每个进程的连接数还需执行"ulimit -n 65535"命令临时修改本地每个进程可以同时打开的最大文件数。
在Linux平台.上,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统为每个TCP连接都要创建一个socket句柄,每个socket句柄同时也是一个文件句柄)。
可使用ulimit -a命令查看系统允许当前用户进程打开的文件数限制。

#查看系统允许当前用户进程打开的文件数
[root@localhost conf]#ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 6911
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 6911
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

在这里插入图片描述

#临时修改本地每个进程可以同时打开的最大文件数
[root@localhost conf]#ulimit -n 6000
#查看修改后的
[root@localhost conf]#ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 6911
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 6000
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 6911
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

4、http配置

使用“http { }”界定标记,包括访问日志、HTTP 端口、网页目录、默认字符集、连接保 持,以及后面要讲到的虚拟 Web 主机、PHP 解析等一系列设置,其中大部分配置语句都包 含在子界定标记“server { }”内

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;				##支持文件发送(下载)
    ##此选项允许或禁止使用socket的TCP cORK的选项(发送数据包前先缓存数据),此选项仅在使用sendfile的时候使用
    #tcp_nopush     on;
	##连接保持超时时间,单位是秒
    #keepalive_timeout  0;
    keepalive_timeout  65;
	
    #gzip  on;			##gzip模块设置,设置是否开启gzip压缩输出
    
server {
        listen       80;					##监听地址及端口
        server_name  www.zzt.com;			##站点域名,可以有多个,用空格隔开
 
        #charset utf-8;						#网页的默认字符集
 
        #access_log  logs/host.access.log  main;		
 
        location / {						##根目录配置
            root   html;					##网站根目录的位置/usr/local/nginx/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;
        }

在这里插入图片描述
在这里插入图片描述

5、隐藏版本号

[root@localhost conf]#vim /apps/nginx/conf/nginx.conf   #进入配置文件进行修改
http {
    include       mime.types;
    default_type  application/octet-stream;
    server_tokens off;     #关闭版本号
}
[root@localhost conf]#nginx -s reload    #重新加载配置文件
[root@localhost conf]#nginx -t      #检查语法

在这里插入图片描述

五、Nginx平滑升级1.18–1.20

方案一:

1、上传所需软件包

在这里插入图片描述

2、修改进程数字,并且查看nginx进程

[root@master opt]#vim /usr/local/nginx/conf/nginx.conf

在这里插入图片描述
在这里插入图片描述

3、重新编译,不安装

[root@localhost nginx-1.20.2]#./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4、备份旧版本,用新版本覆盖旧版本

[root@localhost nginx-1.20.2]#mv /apps/nginx/sbin/nginx /apps/nginx/sbin/nginx.bak  #将低版本的nginx主程序改名
[root@localhost nginx-1.20.2]#cp objs/nginx /apps/nginx/sbin/  #编译后会生成一个objs文件,将新版本 拷入进去

在这里插入图片描述

[root@localhost nginx-1.20.2]#/apps/nginx/sbin/nginx -t  #检查下语法问题
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful

在这里插入图片描述

5、验证

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
方案二:

1、查看已安装版本

[root@master opt]#nginx -V
或者
/apps/nginx/sbin/nginx -V

在这里插入图片描述

2、上传所需更新包至服务器

[root@master opt]#tar -zxf nginx-1.20.2.tar.gz  #解压,进行配置
[root@master opt]#cd nginx-1.20.2/
[root@master nginx-1.20.2]#./configure --prefix=/apps/nginx

在这里插入图片描述

[root@master nginx-1.20.2]#make &>/dev/null
进行编译,切记千万不要输入 make install
[root@master nginx-1.20.2]#cp -f /apps/nginx/sbin/nginx /apps/nginx/sbin/nginx20240104.bak
  #对旧版本进行备份
[root@master nginx-1.20.2]#cp /opt/nginx-1.20.2/objs/nginx /apps/nginx/sbin/nginx
将编译好的 objs目录下的 nginx文件,复制到/apps/nginx/sbin目录。
[root@master nginx-1.20.2]#make upgrade

在这里插入图片描述

六、访问状态统计配置

1、判断Nginx -V是否包含http_stub_status模块

方法一:IP访问

[root@localhost nginx]#/apps/nginx/sbin/nginx -V   #查看已安装的Nginx是否包含HTTP_STUB_STATUS模块
[root@localhost nginx]#cat /opt/nginx-1.20.2/auto/options |grep YES |head #可查看nginx已安装的所有模块

在这里插入图片描述
在这里插入图片描述

2、修改主配置文件,指定访问位置并添加stub_status配置

[root@localhost nginx]#cd conf/
[root@localhost conf]#cp nginx.conf nginx.conf.bak				#复制备份
[root@localhost conf]#vim /apps/nginx/conf/nginx.conf		#编辑主配置文件
......
http {
......
	server {
		listen 80;
		server_name www.zhantai.com;
		charset utf-8;
		location / {
			root html;
			index index.html index.php;
		}
		##添加 stub_status 配置##
		location /status { 					#访问位置为/status
			stub_status on; 				#打开状态统计功能
			access_log off; 				#关闭此位置的日志记录
		}
	}
}

在这里插入图片描述

3、验证测试

在这里插入图片描述
方法二:域名访问

1、修改主配置文件

[root@localhost conf]#vim /apps/nginx/conf/nginx.conf		#编辑主配置文件
server {
        listen       80;
        server_name  www.zhantai.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        location /status {
            stub_status;
        }
[root@localhost conf]#nginx -t           #检查语法
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@localhost conf]#nginx -s reload   #重新加载配置文件

在这里插入图片描述

2、还需修改Linux和Windows的hosts文件

#######Linux中的hosts########
[root@localhost conf]#cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.10.130   www.zhantai.com
:保存退出

#########Windows中的hosts#######
 C:\Windows\System32\drivers\etc\hosts 文件,增加以下内容
192.168.10.130   www.zhantai.com
保存

3、验证测试

在这里插入图片描述

七、基于授权的访问控制

1、生成用户密码认证文件

[root@localhost conf]#yum install -y httpd-tools
[root@localhost conf]#htpasswd -c /apps/nginx/passwd.db zhangsan
[root@localhost conf]#chown nginx /apps/nginx/passwd.db
[root@localhost conf]#chmod 400 /apps/nginx/passwd.db

在这里插入图片描述
在这里插入图片描述

2、修改主配置文件对应的目录,添加认证配置

[root@localhost conf]#vim /apps/nginx/conf/nginx.conf
 location / {
            root   html;
            index  index.html index.php;
            auth_basic "secret";
            auth_basic_user_file /apps/nginx/passwd.db;
        }

在这里插入图片描述

3、重新加载配置文件,检查语法并登录

在这里插入图片描述
在这里插入图片描述

八、基于客户端的访问控制

[root@localhost conf]#vim /apps/nginx/conf/nginx.conf
......
	server {
		location / {
			......
			##添加控制规则——从上向下读取##
			deny 192.168.10.130; 					#拒绝访问的客户端 IP
			allow all;								#允许其它IP客户端访问
		}
	}
 
[root@localhost conf]#nginx -s reload  

九、基于域名的访问控制

1、添加虚拟网卡

[root@localhost conf]#ifconfig ens33:0 192.168.10.122 netmask 255.255.255.0

在这里插入图片描述

2、修改配置文件

server {
        listen       192.168.10.130:80;
        server_name  www.zhantai.com;

        charset utf-8;

        access_log  logs/www.zhantai.access.log;

        location / {
            root   /apps/nginx/html/zhantai;
            index  index.html index.php;
        }
}
        
server {
        listen       192.168.10.122:80;
        server_name  www.zzt.com;

        charset utf-8;

        access_log  logs/www.zzt.access.log;

        location / {
            root   /apps/nginx/html/zzt;
            index  index.html index.php;
        }
}

在这里插入图片描述
在这里插入图片描述

3、创建不同IP地址访问的目录

[root@localhost conf]#mkdir -p /apps/nginx/html/zhantai
[root@localhost conf]#echo "<h1>www.zhantai.com</h1>" >/apps/nginx/html/zhantai/index.html
[root@localhost conf]#mkdir -p /apps/nginx/html/zzt
[root@localhost conf]#echo "<h1>www.zzt.com</h1>" > /apps/nginx/html/zzt/index.html
[root@localhost conf]#nginx -s reload
[root@localhost conf]#systemctl restart nginx

在这里插入图片描述

4、网页验证

在这里插入图片描述

十、基于端口的访问控制

1、修改端口

 server {
        listen       192.168.10.130:9090;
        server_name  www.zhantai.com;

        charset utf-8;

        access_log  logs/www.zhantai.access.log;

        location / {
            root   /apps/nginx/html/zhantai;
            index  index.html index.php;
        }
}
server {
        listen       192.168.10.122:8080;
        server_name  www.zzt.com;

        charset utf-8;

        access_log  logs/www.zzt.access.log;

        location / {
            root   /apps/nginx/html/zzt;
            index  index.html index.php;
        }
}

在这里插入图片描述
在这里插入图片描述

2、网页验证

在这里插入图片描述

总结

Nginx支持的最大并发量取决于CPU与系统允许进程最大读取文件数。
Nginx的单线程处理数不能大于系统允许进程最大读取文件数。
Nginx可以配置多种类型的虚拟主机,分别是基于 IP 的虚拟主机、基于域名的虚拟主机、基于端口的虚拟主机。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!如果您在升级后端服务后遇到了Nginx无响应的问题,有几个可能的原因和解决方法可以尝试: 1. 确认后端服务是否正常:首先,请确保您的后端服务已经成功升级并且正常运行。您可以检查后端服务的日志文件或使用其他工具来验证其是否在预期的端口上监听并提供服务。 2. 检查Nginx配置文件:请仔细检查Nginx的配置文件,确保您已经正确配置了后端服务的代理设置。确保代理设置中的目标地址、端口和路径等信息是正确的,并且没有其他配置错误导致请求无法正常转发到后端服务。 3. 检查代理超时设置:如果后端服务响应较慢或处理时间较长,可能会导致Nginx的代理超时时间被触发而导致无响应。您可以尝试增加Nginx配置文件中的代理超时设置,以便给后端服务更多的时间来处理请求。 4. 调整连接池大小:Nginx默认使用一定数量的连接池来管理与后端服务的连接。如果连接池满了,可能会导致无法建立新的连接,从而导致无响应。您可以尝试增加Nginx配置文件中的连接池大小,以便提供更多的连接数给后端服务使用。 5. 检查系统资源:如果Nginx所在的服务器的系统资源(如CPU、内存等)出现问题,可能会导致Nginx无法正常处理请求。请确保服务器的资源充足,并且没有其他进程或服务占用了过多的资源。 以上是一些常见的解决方法,希望对您有帮助。如果问题仍然存在,请提供更多详细信息和错误日志,以便我们能够更准确地帮助您解决问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值