39. Nginx概述

1.nginx概述

网络IO模型
  1.1 同步 # 领导等待回复 /主动询问
  1.2 异步 # 主动向领导汇报工作
  1.3 阻塞 # 等待完成才会做下一项工作
  1.4 非阻塞 #挂起工作,利用空闲做些其他工作
  异步非阻塞—>nginx

Nginx采用Epool网络模型,Apache采用Select模型
Select: 当用户发起一次请求,select模型就会进行一次遍历扫描,从而导致性能低下。
Epool: 当用户发起请求,epool模型会直接进行处理,效率高效,并无连接限制。

最核心的区别在于apache是同步多进程模型,一个连接对应一个进程;nginx是异步的,多个连接(万级别)可以对应一个进程

1.1nginx基本介绍
  nginx是一个web服务器。 开源 高性能(善于处理高并发,并且小号资源少) 高可靠。

1.2nginx优势
  1.高性能,近w个连接请求时,他的响应要比其他的web要快。
  2.高扩展性:nginx功能模块化。官方提供了很多的优秀模块。
  3.高可靠性:9999 99999
  4.热部署:不停止服务的情况下进行升级。
1.3互联网公司都选择Nginx
  代理、缓存、负载均衡静态资源处理、动静分离、LNMP、LNMT、LNMG 架构。

1.4nginx基本组件

  • nginx二进制文件
  • nginx.conf配置文件
  • nginx access日志
  • nginx error日志
    在这里插入图片描述

1.5nginx应用场景

  • web服务
  • 负载均衡
  • 代理缓存
  • 安全服务 Https Lua
  • 动静分离 Nginx+Tomcat
  • 静态资源服务

1.6nginx安装
  使用YUM安装 <–简单
  使用源码编译

http://nginx.org/packages/centos/7/x86_64/ nginx版本
http://nginx.org/en/linux_packages.html 官方源

nginx命令参数说明:

-V  --- 显示nginx程序配置信息(加载功能模块信息)
-t  --- 检查配置文件语法信息
-T  --- 检查配置文件语法信息 将所有配置内容进行输出显示
-q  --- 检查配置文件语法信息时 非错误消息进行抑制输出
-s  --- 发送信号使程序停止或者平滑重启  -s reload -s stop
-p  --- 指定nginx主程序目录
-c  --- 识别主配置文件信息

1.7.nginx目录结构
[root@web01 ~]# rpm -ql nginx

/etc/logrotate.d/nginx
/etc/nginx
/etc/nginx/conf.d #nginx配置文件 全局配置( 作用于整个Nginx )
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/modules
/etc/nginx/nginx.conf #主配置文件 ( 驾驶员 )
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/etc/nginx/win-utf
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service
/usr/lib64/nginx
/usr/lib64/nginx/modules
/usr/libexec/initscripts/legacy-actions/nginx
/usr/libexec/initscripts/legacy-actions/nginx/check-reload
/usr/libexec/initscripts/legacy-actions/nginx/upgrade
/usr/sbin/nginx #编译出来的二进制文件 ( 汽车 )
/usr/sbin/nginx-debug
/var/cache/nginx
/var/log/nginx #访问日志和错误日志

1.8. nginx配置文件
[root@web01 ~]# grep -Ev "#|^$" nginx.conf.bak >nginx.conf && cat nginx.conf
#全局配置( 作用于整个Nginx )

user  nginx;                
	    --- 指定服务work进程管理用户信息  提升服务安全性
       (nginx: master process  --- 控制服务运行状态)
     (nginx: worker process  --- 处理用户访问请求)
   
    worker_processes  4;		
     --- 定义服务worker进程数量   服务器硬件性能CPU总核心数 >=worker进程数量
    error_log  /var/log/nginx/error.log warn;   
        --- 错误日志保存路径信息 定义错误记录级别
    pid        /var/run/nginx.pid;              
        --- 记录系统程序pid文件
  events 
        worker_connections  1024;               
            --- 记录服务并发连接数量  进程数*连接数 <= 系统最大打开文件
    }
     http {
      include       /etc/nginx/mime.types;	
            --- 加载扩展配置文件  
      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  /var/log/nginx/access.log  main;
        ---调用设置好格式 指定访问日志路径信息
        keepalive_timeout  65;  --- 超时时间设置
        include /etc/nginx/conf.d/*.conf; --- server配置文件  server==网站

1.9 nginx中的http、server、location之间的关系是?
  http:主要用来解决用户的请求和响应。 www test tt
  erver:主要用来响应具体的某一个网站。
  ocation:用于匹配网站的uri路径。

http{} 下面可以有多个  
  Server{} 每个server{}    #一个server对应着一个网站
    又可以有多个 location {}   # server和location如何成对应的关系???

2 nginx运行一个游戏网站

2.1准备一个游戏的源码 ( qq下载的 )
准备一个目录,存放我们的游戏
[root@web01 ~]# mkdir /code
[root@web01 ~]# cd /code
[root@web01 ~]# unzip h5game.zip

2.2.配置Nginx
  配置监听的端口
  配置网站的域名
  配置用户访问网站路径规则 location
  配置默认返回的页面

[root@web01 ~]#cat /etc/nginx/conf.d/game.oldxu.com.conf

server {
	listen 80;
	server_name game.oldxu.com;

location / {
    root /code;
     index index.html;
	}
}

2.3检查nginx的语法 {} 成对出现 ; 每一条指令的结尾都有
[root@web01 code]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

2.4重启服务,让配置生效
[root@web01 code]# systemctl restart nginx

2.5nginx访问网站(详细过程)
game.oldxu.com 如何访问到网站的?

第一步:浏览器请求 game.oldxu.com
第二步:浏览会将请求的 game.oldxu.com --> http://game.oldxu.com/
第三步:浏览器对 域名进行解析 DNS解析 ( 我们域名是假的,所以,我们配置
hosts 劫持 )
第四步:浏览器通过随机端口,像服务端 80 端口建立TCP连接
第五步:浏览器发起HTTP的请求
第六步:请求被80端口对应的Nginx应用所接受,会交给http层,发现请求的域名是game.oldxu.com
第七步:接下来检查所有配文件,看是否有配置文件 满足用户请求的域名。 server_name
第八步:满足域名匹配之后,检查用户请求的路径, / 就会被location / 所匹配
第九步:返回结果,/code下面的index.html 给用户
第十步:nginx应用程序像内核发送请求,获取磁盘中的某个文件,磁盘将数据拷贝至内核的缓存区,然后在 拷贝到nginx应用进程的缓存区
第十一步:nginx应用进程封装数据报文,回传给客户端浏览器。

对应真实服务器磁盘的路径在那里?
/code 目录下


3. nginx 编译安装

  1. 安装Nginx所依赖的库文件或开发包
    yum install gcc redhat-rpm-config \ libxslt-devel gd-devel perl-ExtUtils-Embed \ geoip-devel gperftools-devel pcre-devel openssl-devel -y

  2. 下载软件、解压
    [root@nfs ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
    [root@nfs ~]#tar xf nginx-1.14.2.tar.gz
    [root@nfs ~]# cd nginx-1.14.2/

  3. 编译 可nginx -V参考安装好的模块信息
    ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_perl_module=dynamic --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v2_module --with-cc-opt=’-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic’

make
make install


2.请给Nginx1.14版本添加第三方模块?
nginx_upstream_check_module

[root@nfs ~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
[root@nfs ~]# unzip master.zip
[root@nfs ~]# cd nginx-1.14.2/
[root@nfs nginx-1.14.2]# patch -p1 <../nginx_upstream_check_module-master/check_1.14.0+.patch

./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_perl_module=dynamic --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v2_module --add-module=/root/nginx_upstream_check_module-master --with-cc-opt=’-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic’

make
make install
验证下该模块是否可用


[root@nfs nginx-1.14.2]# cat /etc/nginx/conf.d/upstream_check.conf

upstream blog.oldxu.com {
    server 172.16.1.7:80;
    server 172.16.1.8:80;
    check interval=5000 rise=2 fall=3 timeout=1000 type=tcp;
    #interval检测间隔时间,单位为毫秒
    #rsie表示请求2次正常,标记此后端的状态为up
    #fall表示请求3次失败,标记此后端的状态为down
    #type  类型为tcp
    #timeout为超时时间,单位为毫秒
}


upstream webserver {
    server 172.16.1.7:80;
    server 172.16.1.8:80;
    check interval=5000 rise=2 fall=3 timeout=1000 type=tcp;
}

upstream php {
    server 172.16.1.7:80;
    server 172.16.1.8:80;
    check interval=5000 rise=2 fall=3 timeout=1000 type=tcp;
}


server {
    listen 8888;

    location / {
        proxy_pass http://blog.oldxu.com;
    }

    location /upstream_status {
        check_status;     #开启upstream状态页面
    }
}

4.Nginx平滑升级原理

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值