Nginx基础

Nginx基础

目录

Nginx基础

Nginx简介

Nginx特性

Nginx的优点

Nginx工作原理

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的优点

高并发连接:官方测试能够支撑5万并发连接,在实际生产环境中跑到2-3万并发连接数 
内存消耗少:在3万并发连接下,开启的10个nginx进程才消耗150M内存(15M*10=150M) 
配置文件非常简单:风格跟程序一样通俗易懂 
成本低廉:nginx为开源软件,可以免费使用。而购买F5 BIG-IP、NetScaler等硬件负载均衡交换机则需要十多万至几十万人民币 
支持Rewrite重写规则:能够根据域名、URL的不同,将HTTP请求分到不同的后端服务器群组 
内置的健康检查功能:如果Nginx Proxy后端的某台Web服务器宕机了,不会影响前端访问 
节省带宽:支持GZIP压缩,可以添加浏览器本地缓存的Header头 
稳定性高:用于反向代理,宕机的概率微乎其微 
模块化设计:模块可以动态编译 
外围支持好:文档全,二次开发和模块较多 
支持热部署:可以不停机重载配置文件 
支持事件驱动、AIO(AsyncIO,异步IO)、mmap(Memory Map,内存映射)等性能优化

Nginx工作原理

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

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

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

Nginx安装配置


//修改主机名关闭防火墙和selinux

[root@zzh ~]# hostnamectl set-hostname nginx

[root@zzh ~]# bash

[root@nginx ~]# systemctl disable --now firewalld

Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.

Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

[root@nginx ~]# setenforce 0

[root@nginx ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config

//安装依赖包

[root@nginx ~]# dnf -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++

//创建nginx用户

[root@nginx ~]# useradd -rMs /sbin/nologin nginx

//下载nginx软件包并解压

[root@nginx ~]# wget http://nginx.org/download/nginx-1.20.2.tar.gz

--2022-10-10 11:51:42-- http://nginx.org/download/nginx-1.20.2.tar.gz

Resolving nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5704::6, ...

Connecting to nginx.org (nginx.org)|3.125.197.172|:80... connected.

HTTP request sent, awaiting response... 200 OK

Length: 1062124 (1.0M) [application/octet-stream]

Saving to: 'nginx-1.20.2.tar.gz'


nginx-1.20.2.tar.gz 100%[=======================================================================================================>] 1.01M 226KB/s in 4.8s


2022-10-10 11:51:48 (215 KB/s) - 'nginx-1.20.2.tar.gz' saved [1062124/1062124]


[root@nginx ~]# tar -xf nginx-1.20.2.tar.gz

[root@nginx ~]# cd nginx-1.20.2/

[root@nginx nginx-1.20.2]#

//进入目录配置编译安装

[root@nginx nginx-1.20.2]# ./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@nginx nginx-1.20.2]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install

//安装完成

[root@nginx nginx-1.20.2]# cd /usr/local/nginx/

[root@nginx nginx]# ls

conf html logs sbin

//配置环境变量

[root@nginx ~]# echo "export PATH=$PATH:/usr/local/nginx/sbin" > /etc/profile.d/nginx.sh

[root@nginx ~]# source /etc/profile.d/nginx.sh

//启动服务

[root@nginx ~]# nginx

[root@nginx ~]# 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 0.0.0.0:80 0.0.0.0:*

LISTEN 0 128 [::]:22 [::]:*

//编写service文件管理服务

[root@nginx ~]# cat > /usr/lib/systemd/system/nginx.service << EOF

> [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

> EOF

//重载配置加入开机自启

[root@nginx ~]# systemctl daemon-reload

[root@nginx ~]# systemctl enable --now nginx

Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.

[root@nginx ~]# 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 [::]:*

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值