服务管理-Nginx

nginx优点 select,epoll模型

对于一次IO访问(以read举例),数据会先被拷贝到操做系统内核的缓冲区中,而后才会从操做系统内核的缓冲区拷贝到应用程序的地址空间。因此说。当一个read操做发生时,它会经历两个阶段:php

1.等待数据准备(waiting for the data to be ready)
2.将数据从内核拷贝到进程中,正是由于这两个阶段,Linux系统产生了下面的五种网络模:
    - 阻塞IO(blocking IO)
    - 非阻塞IO(nonblocking IO)
    - IO多路复用(IO multplexing)
    - 信号驱动IO(signal driven IO)
    - 异步IO(asynchronous IO)
    注:因为signal driven IO在实际中并不经常使用,因此只提剩下的四种IO模型

阻塞IO(blocking IO)

在Linux中,默认状况下全部的socket都是blocking,一个典型的读操做流程大概是这样的:html

  当用户进程调用了recvfrom这个系统调用,kernel就开始了IO的第一个阶段:准备数据(对于网络IO来讲,不少时候啥数据在一开始尚未到达。好比,尚未收到一个完整的udp包。这个时候kernel就要等待足够的数据到来)。这个过程须要等待,也就是说数据被拷贝到操做系统内核的缓冲区中是须要一个过程的。而在用户进程这边,整个进程都会被阻塞(固然,这个事进程本身选择的阻塞)。当kernel一直等到数据准备好了。他就会将数据从kernel中拷贝到用户内存,而后kernel返回结果。用户进程才接触blocking状态,从新运行起来。nginx

  因此,blocking IO 的特色就是在IO执行的两个阶段都被block了。git

 非阻塞IO(nonblocking IO)

Linux下,能够经过设置socket使其变为non-blocking。当对一个non-blocing socket执行读操做时,整个流程是这个样子的:web

  当用户进程发出read操做时,若是kernel中的数据尚未准备好,那么它并不会block用户进程,而是马上返回一个error,从用户进程角度讲,它发起一个read操做后,并不须要等待,二十立刻就获得了一个结果。用户进程判断结果是一个error时,他就知道啥数据尚未准备好,因而它能够再次发送read操做。一旦kernel中的数据准备好了,而且又再次收到了用户进程的system call,那么它立刻就将数据拷贝到了用户内存,而后返回。centos

  因此,nonblocking IO的特色是用户进程须要不断的主动询问kernel数据准备好了没有。浏览器

IO多路复用(IO multiplexing)

IO multiplexing 就是咱们说的select,poll,epoll,有些地方也称这总IO方式为事件驱动IO(event driven IO)。select/epoll的好处就在于单个process就能够同时处理多个网络链接的IO。他的基本原理就是select,poll,epoll这个function会不断的轮询所负责的全部socket,当某个socket有数据到达了,就通知用户进程。缓存

  当用户进程调用了select,那么这个进程会被block,而同时,kernel会“监视”全部select负责的socket,当任何一个socket中的数据准备好了,select就会返回,这个会后用户进程再调用read操做。将数据从kernel拷贝到用户进程。安全

  因此,IO多路复用的特色是经过一种机制,一个进程能同时等待多个文件描述符,而这些文件描述符(套接字描述符)其中的任意一个进入读就绪状态,select()函数就会返回。服务器

  这个图和blocking IO的图其实并无太大的不一样,事实上,还更差一些,由于这里须要使用两个system call (select 和recvform),而blocking IO 只调用了一个system call(recvform),可是,用select的有事在于它能够同时处理多个connection。

   因此,若是处理的连接数不是很高的话,使用select/epoll 的web server 不必定比使用multi-threading + blocking IO 的web server性能更好,可能延迟还更大。

  select/epoll 的优点并非对于单个链接能处理的更快,而是在于能处理更多的连接

在IO multiplexing model中,实际中,对于每个socket,通常都设置成为nonblocking,可是,如上图所示,这个用户的process实际上是一直被block的,只不过process是被select这个函数block,而不是被socket IO给block。

 异步IO(asynchronous IO)

Linux下的asynchronous IO其实用得不多,先看一下它的流程:

用户进程发起read操做以后,马上就能够开始作其它的事。而另外一方面,从kernel的角度,当它收到一个asynchronous以后,首先它会马上返回,因此不会对用户进程产生任何的block。而后,kernel会等待数据准备完成,而后将数据拷贝到用户内存,当这一切都完成以后,kernel会给用户进程发送一个signal,告诉它read操做完成了。

 --------------------------------------------------------------------------------------------------------------------------------

nginx安装

能够采用源码安装或者yum安装,具体方式请百度
nginx须要依赖gcc等依赖,建议先安装依赖

刚刚遇到一个问题,centos7安装nginx居然报错了。。

[root@localhost ~]# yum install nginx -y
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.cn99.com
 * updates: mirrors.163.com
没有可用软件包 nginx。
错误:无须任何处理
[root@localhost ~]# 

这个问题比较容易解决,能够采用epel的方式安装nginx

[root@localhost ~]# yum install epel-rpm-macros
[root@localhost ~]# yum update
[root@localhost ~]# yum install nginx
[root@localhost ~]# 

安装就到这里了,遇到问题自行百度就好。

[root@localhost ~]# systemctl stop nginx     # 中止服务
[root@localhost ~]# systemctl start nginx     # 启动服务
[root@localhost ~]# systemctl status nginx   # 查看状态
[root@localhost ~]# systemctl restart nginx   # 从新加载
[root@localhost ~]# 
[root@localhost ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

9月 18 11:36:42 localhost.localdomain systemd[1]: Unit nginx.service cannot be reloaded because it is...ve.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# systemctl stop nginx
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since 二 2018-09-18 11:38:58 CST; 2s ago
  Process: 66173 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 66170 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 66164 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 66175 (nginx)
    Tasks: 3
   CGroup: /system.slice/nginx.service
           ├─66175 nginx: master process /usr/sbin/nginx
           ├─66176 nginx: worker process
           └─66177 nginx: worker process

9月 18 11:38:57 localhost.localdomain systemd[1]: Starting The nginx HTTP and reverse proxy server...
9月 18 11:38:58 localhost.localdomain nginx[66170]: nginx: the configuration file /etc/nginx/nginx.co... ok
9月 18 11:38:58 localhost.localdomain nginx[66170]: nginx: configuration file /etc/nginx/nginx.conf t...ful
9月 18 11:38:58 localhost.localdomain systemd[1]: Started The nginx HTTP and reverse proxy server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# 

nginx默认使用80端口,已经启动了,能够从浏览器中进行访问了。

 nginx核心模块

https://nginx.org/en/docs/    能够进这个里面找你须要的看,

 nginx HTTP模块

https://nginx.org/en/docs/http/ngx_http_core_module.html#server

 

 Nginx虚拟主机

Nginx反向代理

反向代理 VS 正向代理

什么是正向代理?什么是反向代理?

正向代理:

假设在客户机与目标主机之间,只用户代理内部网络对Internet的连接请求,客户机必须指定代理服务器,并将原来要直接发送到web服务器上的HTTP请求发送到代理服务器中。
  1.提升访问速度
  2.防火墙做用
  3.经过代理服务器访问不能方案文的目标站点

反向代理:

服务器架设在服务器端,经过缓冲常常被请求的页面来环节服务器的工做量,将客户机请求转发给内部网络上的目标服务器;并将从服务器上获得的结果返回给Internet请求连接的客户端,此时代理服务器与目标主机一块儿对外表现为一个服务器。
  1.能够防止外网对内网服务器的恶性攻击
  2.缓存以减小服务器的压力
  3.访问安全控制以外
  4.能够进行负载均衡,将用户的请求分配给多个服务器
# proxy the PHP scripts to Apache listening on 127.0.0.1c80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
location ~ \.html$ {
  proxy_pass http://192.168.251.102c8080;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1c80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
location ~ \.html$ {
  proxy_pass http://webserver;
}
upstream webserver {
  server 192.168.251.102:8080;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1c80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
location ~ \.html$ {
  proxy_pass http://webserver;
  proxy_cache my-cache;
}
upstream webserver {
s  erver 192.168.251.102c8080;
}
server {
        listen 80;
        server_name xxx.xt.com;
        access_log /var/log/nginx/git.chjrt.access.log;
        error_log /var/log/nginx/git.chjrt.error.log;
        location / {
           proxy_set_header Host $host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_pass http://172.20.206.41:8089;
        }
}

https://nginx.org/en/

中文文档

 更多等用到了补充。目前就用了这么点。。

相关文章
相关标签/搜索