服务管理-Nginx

nginx优势 select,epoll模型

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

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,一个典型的读操作流程大概是这样的:

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

  所以,blocking IO 的特点就是在IO执行的两个阶段都被block了。

 非阻塞IO(nonblocking IO)

Linux下,可以通过设置socket使其变为non-blocking。当对一个non-blocing socket执行读操作时,整个流程是这个样子的:

  当用户进程发出read操作时,如果kernel中的数据还没有准备好,那么它并不会block用户进程,而是立刻返回一个error,从用户进程角度讲,它发起一个read操作后,并不需要等待,二十马上就得到了一个结果。用户进程判断结果是一个error时,他就知道啥数据还没有准备好,于是它可以再次发送read操作。一旦kernel中的数据准备好了,并且又再次收到了用户进程的system call,那么它马上就将数据拷贝到了用户内存,然后返回。

  所以,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/

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

猜你喜欢

转载自www.cnblogs.com/52-qq/p/9668135.html
今日推荐