• nginx


    nginx

    概述

    web服务器,方向代理,负载均衡,邮件代理,运行时需要的系统资源比较少。

    比较轻量级。

    nginx服务器软件,俄罗斯(Igor Sysoev)人用c语言开发的,开源。号称处理百万级别的并发。热部署,高度模块化设计。自由许可证。

    第三方业务模块可以用(c++开发)。

    高并发 linux epoll技术。 windows IOCP

    内涵内存池,进程池,线程池,事件驱动。

    安装 nginx

    安装前提:

    1,查看内核版本,因为要用到epoll技术

    cwl@cwl-PC:~$ uname -a
    Linux cwl-PC 4.15.0-29deepin-generic #31 SMP Fri Jul 27 07:12:08 UTC 2018 x86_64 GNU/Linux
    

    2,通过编译来安装(gcc and g++)

    3,pcre库(函数库); 支持解析正则表达式

    sudo apt-get install libpcre3-dev
    

    4,zlib库:压缩解压缩的功能

    sudo apt-get install libz-dev
    

    5,openssl库,ssl相关的库,用于网站加密通信

    sudo apt-get install libssl-dev
    

    正式安装:

    官网 http://nginx.org

    mainline版本:版本号奇数,更新快,稳定性差一点,更新快。

    stable稳定版:版本偶数,经过了长时间的测试,比较稳定。

    legacy版本:老版本

    正式安装:

    方法一:apt-get (不太灵活)

    方法二:编译源码

    下载源码

    wget http://nginx.org/download/nginx-1.14.2.tar.gz
    tar -xzvf xxx
    

    目录结构

    总用量 760
    drwxr-xr-x 8 cwl cwl   4096 12月  4 22:52 .
    drwxr-xr-x 3 cwl cwl   4096 3月  10 15:51 ..
    drwxr-xr-x 6 cwl cwl   4096 3月  10 15:51 auto
    -rw-r--r-- 1 cwl cwl 288742 12月  4 22:52 CHANGES
    -rw-r--r-- 1 cwl cwl 440121 12月  4 22:52 CHANGES.ru
    drwxr-xr-x 2 cwl cwl   4096 3月  10 15:51 conf
    -rwxr-xr-x 1 cwl cwl   2502 12月  4 22:52 configure
    drwxr-xr-x 4 cwl cwl   4096 3月  10 15:51 contrib
    drwxr-xr-x 2 cwl cwl   4096 3月  10 15:51 html
    -rw-r--r-- 1 cwl cwl   1397 12月  4 22:52 LICENSE
    drwxr-xr-x 2 cwl cwl   4096 3月  10 15:51 man
    -rw-r--r-- 1 cwl cwl     49 12月  4 22:52 README
    drwxr-xr-x 9 cwl cwl   4096 3月  10 15:51 src
    
    

    首先要执行configure脚本

    ./configure
    

    可以发现执行后多了objs目录,中间文件,和Makefile

    make
    

    然后在objs中就能看到nginx的可执行文件

    然后安装

    sudo make install
    

    默认安装到/usr/local/nginx

    到这样就安装完了。

    简单使用

    查看是否使用了nginx

    ps -ef | grep nginx
    

    在sbin目录下执行nginx文件

    cwl@cwl-PC:/usr/local/nginx/sbin$ ps -ef | grep nginx
    cwl      13012 17816  0 19:14 pts/0    00:00:00 grep nginx
    cwl@cwl-PC:/usr/local/nginx/sbin$ ls
    nginx
    cwl@cwl-PC:/usr/local/nginx/sbin$ sudo ./nginx 
    cwl@cwl-PC:/usr/local/nginx/sbin$ ps -ef | grep nginx
    root     13017     1  0 19:14 ?        00:00:00 nginx: master process ./nginx
    nobody   13018 13017  0 19:14 ?        00:00:00 nginx: worker process
    cwl      13024 17816  0 19:14 pts/0    00:00:00 grep nginx
    
    

    现在就可以用ip:端口访问网页了,把html中的index.html发送

    nginx的整体结构

    启动后,看到了master进程和worker进程

    root     13017     1  0 19:14 ?        00:00:00 nginx: master process ./nginx
    
    nobody   13018 13017  0 19:14 ?        00:00:00 nginx: worker process
    
    cwl      15110 17816  0 19:20 pts/0    00:00:00 grep nginx
    

    第一列: 用户id

    第二列: 进程id

    第三列: 父进程id

    nginx进程模型

    一个master进程,1到多个worker进程来提供对外服务。来实现稳定灵活的提供服务。

    • master进程:监控进程,不处理业务,专门监控worker进程。
    • worker进程: 来做主要的活,里面有竞争策略
    • master进程和worker进程之间要通讯
    • worker进程挂掉,那么master就会fork出新的进程。

    work进程几个合适呢?

    公认的做法,让每个worker运行在一个单独的核上,减少cpu进程切换的成本

    grep -c processor /proc/cpuinfo
    

    查看当前linux的核心数

    另外可以在/usr/local/nginx/conf/nginx.conf修改配置文件修改worker进程的数目

    nginx进程模型初步

    • nginx 热部署重载配置文件(灵活)
      • 修改配置文件
      • 不需要停止nginx,
      • sudo ./nginx -s reload
      • 例如修改worker进程的数量,会杀死原理的,重新fork出来,进程通讯
    • nginx的升级
      • 如果升级,不需要关闭服务,热升级。升级后还可以回退。热回滚
    • 关闭nginx
      • ./nginx -s stop
      • stop强制退出,quit服务完在退出
      • nginx -s发信号 ./nginx -?可以查看帮助

    总结:多线程共享内存,一个报错,那么会影响其他线程。有弊端。nginx多进程,相互不影响。

    后面我们开始源码的学习

  • 相关阅读:
    头文件#ifndef #define #endif使用
    Django框架【form组件】
    数据库【mysql】之pymysql
    数据库【mysql篇】典型的一些练习题目
    Python开发【socket篇】解决粘包
    Python开发【内置模块篇】os模块
    Python开发【内置模块篇】日志模块
    Python开发【内置模块篇】configparser
    Python开发【内置模块篇】collections
    Python开发【内置模块篇】datetime
  • 原文地址:https://www.cnblogs.com/Q1143316492/p/10506795.html
Copyright © 2020-2023  润新知