nginx

  • nginx已关闭评论
  • 114 次浏览
  • A+
所属分类:linux技术
摘要

nginx(发音同engine x)是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like协议下发行。


nginx


nginx简介

nginx(发音同engine x)是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like协议下发行。

nginx由俄罗斯的程序设计师Igor Sysoev所开发,最初供俄国大型的入口网站及搜寻引擎Rambler使用。

第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。

nginx的特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。

nginx的特性

nginx是一个很牛的高性能Web和反向代理服务器,它具有很多非常优越的特性:

在高连接并发的情况下,nginx是Apache服务器不错的替代品,能够支持高达50000个并发连接数的响应
使用epoll and kqueue作为开发模型
nginx作为负载均衡服务器:nginx既可在内部直接支持和PHP程序对外进行服务,也可支持作为HTTP代理服务器对外进行服务
nginx采用C进行编写,不论系统资源开销还是CPU使用效率都比Perlbal要好很多

nginx的工作原理

nginx的模块直接被编译进nginx,因此属于静态编译方式。

启动nginx后,nginx的模块被自动加载,与Apache不一样,首先将模块编译为一个so文件,然后在配置文件中指定是否进行加载。

在解析配置文件时,nginx的每个模块都有可能去处理某个请求,但是同一个处理请求只能由一个模块来完成。

nginx的进程架构:
启动nginx时,会启动一个Master进程,这个进程不处理任何客户端的请求,主要用来产生worker线程,一个worker线程用来处理n个request。
nginx

工作方式
在工作方式上,Nginx分为单工作进程和多工作进程两种模式。在单工作进程模式下,除主进程外,还有一个工作进程,工作进程是单线程的;在多工作进程模式下,每个工作进程包含多个线程。Nginx默认为单工作进程模式。

Nginx在启动后,会有一个master进程和多个worker进程。

下图展示了nginx模块一次常规的HTTP请求和响应的过程
nginx

下图展示了基本的WEB服务请求步骤
nginx

部署nginx

nginx官网
在官网里面找Stable version(稳定版本)

[root@localhost ~]# systemctl stop firewalld.service  //关闭防火墙 [root@localhost ~]# vim /etc/selinux/config  SELINUX=disabled [root@localhost ~]# setenforce 0 [root@localhost ~]# systemctl disable --now firewalld.service  Removed /etc/systemd/system/multi-user.target.wants/firewalld.service. Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. [root@localhost ~]# useradd -r -M -s /sbin/nologin nginx //创建系统用户nginx [root@localhost ~]# dnf -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ wget make //安装依赖包 [root@localhost ~]# mkdir -p /var/log/nginx [root@localhost ~]# chown -R nginx.nginx /var/log/nginx //创建日志存放目录 [root@localhost ~]# cd /usr/src/ [root@localhost src]# wget http://nginx.org/download/nginx-1.22.0.tar.gz           //下载nginx [root@localhost src]# ls debug  kernels  nginx-1.22.0.tar.gz [root@localhost src]# tar xf nginx-1.22.0.tar.gz  [root@localhost src]# cd nginx-1.22.0/ [root@localhost nginx-1.22.0]# ./configure    //进行编译 > --prefix=/usr/local/nginx  > --user=nginx  > --group=nginx  > --with-debug  > --with-http_ssl_module  > --with-http_realip_module  > --with-http_image_filter_module  > --with-http_gunzip_module  > --with-http_gzip_static_module  > --with-http_stub_status_module  > --http-log-path=/var/log/nginx/access.log  > --error-log-path=/var/log/nginx/error.log [root@localhost nginx-1.22.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) //统计系统里面有几个cpu。然后进行安装 [root@localhost nginx-1.22.0]# ls CHANGES  CHANGES.ru  LICENSE  Makefile  README  auto  conf  configure  contrib  html  man  objs  src [root@localhost nginx-1.22.0]# ls objs/     //编译存放的地方 Makefile      nginx    ngx_auto_config.h   ngx_modules.c  src autoconf.err  nginx.8  ngx_auto_headers.h  ngx_modules.o [root@localhost nginx-1.22.0]# file objs/nginx objs/nginx: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=146c123f448d84a21258affd355418734db34b42, with debug_info, not stripped //主程序文件 [root@localhost nginx-1.22.0]# make install  //安装 [root@localhost nginx-1.22.0]# cd /usr/local/nginx/ [root@localhost nginx]# ls conf  html  logs  sbin [root@localhost nginx]# /usr/local/nginx/sbin/nginx  //开启ngix [root@localhost nginx]# ss -antl   //查看80端口 State      Recv-Q     Send-Q          Local Address:Port           Peer Address:Port     Process      LISTEN     0          128                   0.0.0.0:80                  0.0.0.0:*                     LISTEN     0          128                   0.0.0.0:22                  0.0.0.0:*                     LISTEN     0          128                      [::]:22                     [::]:*     [root@localhost nginx]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh //配置环境变量 [root@localhost nginx]# source /etc/profile.d/nginx.sh //使其生效 [root@localhost nginx]# cd [root@localhost ~]# which nginx  //现在就是可以直接用nginx启动服务 /usr/local/nginx/sbin/nginx [root@localhost ~]# nginx -s stop  //停止服务 [root@localhost ~]# ss -antl State      Recv-Q     Send-Q          Local Address:Port           Peer Address:Port     Process      LISTEN     0          128                   0.0.0.0:22                  0.0.0.0:*                     LISTEN     0          128                      [::]:22                     [::]:*                     [root@localhost ~]# nginx   //开启服务 [root@localhost ~]# ss -antl State      Recv-Q     Send-Q          Local Address:Port           Peer Address:Port     Process      LISTEN     0          128                   0.0.0.0:80                  0.0.0.0:*                     LISTEN     0          128                   0.0.0.0:22                  0.0.0.0:*                     LISTEN     0          128                      [::]:22                     [::]:*   //服务控制方式,使用nginx命令     -t  //检查配置文件语法     -v  //输出nginx的版本     -c  //指定配置文件的路径     -s  //发送服务控制信号,可选值有{stop|quit|reopen|reload}                                   

访问:
nginx
将其添加到systemd服务里面

[root@localhost ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/nginx.service [root@localhost ~]# vim /usr/lib/systemd/system/nginx.service [root@localhost ~]# cat /usr/lib/systemd/system/nginx.service [Unit] Description=nginx server daemon After=network.target  [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecStop=/usr/local/nginx/sbin/nginx -s stop ExecReload=/bin/kill -HUP $MAINPID  [Install] WantedBy=multi-user.target [root@localhost ~]# nginx -s stop //先停止nginx [root@localhost ~]# systemctl daemon-reload  //加载配置 [root@localhost ~]# systemctl enable --now nginx  //设置开机自启 [root@localhost ~]# systemctl status nginx.service  //查看状态 ● nginx.service - nginx server daemon    Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)    Active: active (running) since Mon 2022-10-10 21:49:52 CST; 1min 56s ago   Process: 322790 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)  Main PID: 322791 (nginx)     Tasks: 2 (limit: 12221)    Memory: 2.3M    CGroup: /system.slice/nginx.service            ├─322791 nginx: master process /usr/local/nginx/sbin/nginx            └─322792 nginx: worker process  Oct 10 21:49:52 localhost systemd[1]: Starting nginx server daemon... Oct 10 21:49:52 localhost systemd[1]: Started nginx server daemon. [root@localhost ~]# ss -antl //查看端口 State      Recv-Q     Send-Q          Local Address:Port           Peer Address:Port     Process      LISTEN     0          128                   0.0.0.0:80                  0.0.0.0:*                     LISTEN     0          128                   0.0.0.0:22                  0.0.0.0:*                     LISTEN     0          128                      [::]:22                     [::]:*                     

访问:
nginx

  • 版权声明:本站原创文章,于2022年10月10日22:27:49,由 发表,共 5301 字。
  • 转载请注明:nginx - 张拓的天空