首页 > 系统相关 >nginx基础篇

nginx基础篇

时间:2024-03-17 14:45:46浏览次数:19  
标签:web http -- 基础 module nginx root

web服务器介绍

Unix和Linux平台下的常用Web服务器常见有:

Apache
Nginx
Lighttpd
Tomcat
IBM WebSphere
其中最为广泛的是Nginx,在Windows平台上最常用的是微软的IIS(Internet Information Server,互联网信息服务)是 Windows 系统中默认的 Web 服务程序。

nginx 介绍

nginx是什么

1.nginx是一个高性能的HTTP服务器、反向代理服务器。
2.主要特点
- 开源源代码
- 高性能,并发性能、处理tcp连接性能极高
- 可靠,服务稳定,得到了全世界的验证。

nginx重要特性

1.官网直接获取源码,免费用,讲道理这种高性能的软件,是很贵的。
2.高性能,官网提供测试数据,性能残暴,1秒内能支持5万个tcp连接
3.消耗资源很低,有数据证明在生产环境下3万左右的并发tcp连接,开启10个nginx进程消耗不到150M。
4.有能力可以自己对nginx进行二次开发,如淘宝的tengine。
5.模块化管理,nginx有大量的模块(插件),运维根据公司的业务需求,按需编译安装设置即可。
模块插件是为了提供额外的功能,如
- url重写,根据域名、url、客户端的不同,转发http请求到不同的机器
- https证书的支持
- 支持静态资源压缩
- 支持热部署、无须重启,更新配置文件
- 支持二次开发新插件

在线自动生成nginx配置文件

https://www.digitalocean.com/community/tools/nginx?global.app.lang=zhCN
可以自由选择所需的应用,生成nginx配置作为参考。
根据你的业务需求,自动生成复杂的nginx配置文件,提供你作为参考,非常好用

nginx企业用它干啥

1.提供静态页面展示,网页服务  
2.提供多个网站、多个域名的网页服务
====================================================


3.提供反向代理服务(结合动态应用程序)

4.提供简单资源下载服务(密码认证) ftp服务


5.用户行为分析(日志功能)

nginx的运行架构

nginx运行后,有多少个干活的工人,多进程,调用多个cpu去解析用户的请求

在linux中进行多进程开发,开辟多个进程,调用多个cpu,当然也会消耗更多的机器资源,内存,cpu资源,给服务器带来更大的压力
- 不是说进程越多,干活越快,合理的分配,才能达到最高效的处理效率

=====
有一个屋子要装修,请1个工人干活,还是请5个工人干活,请500个工人干活

哪一个效率是最高的呢? 最合适的?
===
关于nginx的优化设置,nginx默认应该启动多少个进程去工作呢?
默认就是根据cpu的核数去设置进程数即可。

master进程

包工头进程,管理nginx的数据,创建worker工作进程。

1.启动时检查nginx.conf是否正确,语法错误;
2.根据配置文件的参数创建、且监控worker进程的数量和状态;
3.监听socket,接收client发起的请求,然后worker竞争抢夺链接,获胜的可以处理且响应请求。
4.接收运维超哥发送的管理nginx进程的信号,并且将信号通知到worker进程。
5.如果运维超哥发送了reload命令,则读取新配置文件,创建新的worker进程,结束旧的worker进程。

worker工作进程原理

1.实际处理client网络请求的是worker
2.master根据nginx.conf决定worker的数量
3.有client用户请求到达时,worker之间进程竞争,获胜者和client建立连接且处理用户请求;
4.接收用户请求后,若需要代理转发给后端,则后端处理完毕后接收处理结果,再响应给用户
5.接收并处理master发来的进程信号,如启动、重启、重载、停止。

nginx处理http请求流程


nginx重要模块

[root@yuchao-tx-server ~]#nginx -V 该命令可见nginx已安装的模块,也是默认比较重要的模块

--prefix=/usr/share/nginx
--sbin-path=/usr/sbin/nginx
--modules-path=/usr/lib64/nginx/modules
--conf-path=/etc/nginx/nginx.conf
--error-log-path=/var/log/nginx/error.log
--http-log-path=/var/log/nginx/access.log
--http-client-body-temp-path=/var/lib/nginx/tmp/client_body
--http-proxy-temp-path=/var/lib/nginx/tmp/proxy
--http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi
--http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi
--http-scgi-temp-path=/var/lib/nginx/tmp/scgi
--pid-path=/run/nginx.pid
--lock-path=/run/lock/subsys/nginx
--user=nginx
--group=nginx
--with-compat
--with-debug
--with-file-aio
--with-google_perftools_module
--with-http_addition_module
--with-http_auth_request_module
--with-http_dav_module
--with-http_degradation_module
--with-http_flv_module
--with-http_gunzip_module
--with-http_gzip_static_module
--with-http_image_filter_module=dynamic
--with-http_mp4_module
--with-http_perl_module=dynamic
--with-http_random_index_module
--with-http_realip_module
--with-http_secure_link_module
--with-http_slice_module
--with-http_ssl_module
--with-http_stub_status_module
--with-http_sub_module
--with-http_v2_module
--with-http_xslt_module=dynamic
--with-mail=dynamic
--with-mail_ssl_module
--with-pcre
--with-pcre-jit
--with-stream=dynamic
--with-stream_ssl_module
--with-stream_ssl_preread_module
--with-threads
--with-cc-opt=-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic
--with-ld-opt=-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E

模块解释

Nginx模块名称    模块作用
ngx_http_access_module    四层基于IP的访问控制,可以通过匹配客户端源IP地址进行限制
ngx_http_auth_basic_module    状态页,使用basic机制进行用户认证,在编译安装nginx的时候需要添加编译参数--withhttp_stub_status_module,否则配置完成之后监测会是提示语法错误
ngx_http_stub_status_module    状态统计模块
ngx_http_gzip_module    文件的压缩功能
ngx_http_gzip_static_module    静态压缩模块
ngx_http_ssl_module    nginx 的https 功能
ngx_http_rewrite_module    重定向模块,解析和处理rewrite请求
ngx_http_referer_module    防盗链功能,基于访问安全考虑
ngx_http_proxy_module    将客户端的请求以http协议转发至指定服务器进行处理
ngx_stream_proxy_module    tcp负载,将客户端的请求以tcp协议转发至指定服务器处理
ngx_http_fastcgi_module    将客户端对php的请求以fastcgi协议转发至指定服务器助理
ngx_http_uwsgi_module    将客户端对Python的请求以uwsgi协议转发至指定服务器处理
ngx_http_headers_module    可以实现对头部报文添加指定的key与值
ngx_http_upstream_module    负载均衡模块,提供服务器分组转发、权重分配、状态监测、调度算法等高级功能
ngx_stream_upstream_module    后端服务器分组转发、权重分配、状态监测、调度算法等高级功能
ngx_http_fastcgi_module    实现通过fastcgi协议将指定的客户端请求转发至php-fpm处理
ngx_http_flv_module    为flv伪流媒体服务端提供支持

模块官网

官网文档
https://nginx.org/en/docs/ 所有模块都在这了
核心模块如下

重定向模块

公司使用php作为后端,nginx反向代理php的模块

反向代理模块

负载均衡模块

nginx部署实践

1.安装方式

  • 源代码编译安装,优点
    • 版本,可以获取官网最新的软件包,甚至最新测试版,都可以直接编译安装
    • 还有稳定版本
    • 自由定义,安装路径自由定义,
    • 自由定义第三方插件
    • 缺点,安装步骤繁琐,耗时太长,看你要装多少个模块,编译添加的模块多,安装的就更久
  • rpm安装
    • 得提前准备好nginx本身的rpm包,以及相关依赖的rpm包
    • 用于离线安装nginx的环境
  • yum安装,你会用哪些形式的仓库?
    • 阿里云第三方仓库(centos-base.repo,epel.repo)
      • 这个其实都不靠谱。
    • 自建yum仓库(得提前准备好nginx本身的rpm包,以及相关依赖的rpm包)
    • nginx官网仓库(获取官网最新稳定版的yum源仓库)
      • yum一键安装,省心省事,版本也是有一定的保障的,rpm的安全性也是有保障的
        编译安装
        官网

https://nginx.org/en/docs/configure.html

文档

1.创建用户
groupadd www -g 666
useradd www -u 666 -g 666 -M -s /sbin/nologin

2.安装依赖环境
[root@web-9 ~]#yum install pcre pcre-devel openssl openssl-devel  zlib zlib-devel gzip  gcc gcc-c++ make wget httpd-tools vim -y



3.下载源码 https://nginx.org/download/,且解压缩
mkdir /yuchao-linux-data/ -p ;wget -O /yuchao-linux-data/nginx-1.19.0.tar.gz https://nginx.org/download/nginx-1.19.0.tar.gz

cd /yuchao-linux-data/ && tar -zxf nginx-1.19.0.tar.gz

开始编译,编译三部曲来了

0.查看编译参数帮助信息
[root@web-9 /yuchao-linux-data/nginx-1.19.0]#./configure --help


1.编译参数

[root@web-9 /yuchao-linux-data/nginx-1.19.0]#./configure --user=www --group=www --prefix=/opt/nginx-1-19-0 --with-http_stub_status_module --with-http_ssl_module --with-pcre

2.编译安装
[root@web-9 /yuchao-linux-data/nginx-1.19.0]#make && make install

创建软连接

[root@web-9 /yuchao-linux-data/nginx-1.19.0]#ln -s /opt/nginx-1-19-0/ /opt/nginx
[root@web-9 /yuchao-linux-data/nginx-1.19.0]#
[root@web-9 /yuchao-linux-data/nginx-1.19.0]#ll  -d /opt/nginx
lrwxrwxrwx 1 root root 18 May  7 17:12 /opt/nginx -> /opt/nginx-1-19-0/

默认的nginx安装目录

[root@web-9 /yuchao-linux-data/nginx-1.19.0]#ls /opt/nginx
conf  html  logs  sbin

3.检查语法

[root@web-9 /opt]#/opt/nginx/sbin/nginx -t
nginx: the configuration file /opt/nginx-1-19-0/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx-1-19-0/conf/nginx.conf test is successful

4.启动nginx

先不着急配置PATH,先绝对路径启动

-c 参数指定配置文件启动

[root@web-9 /opt]#/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf
[root@web-9 /opt]#ps -ef|grep nginx
root      50976      1  0 17:26 ?        00:00:00 nginx: master process /opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf
www       50977  50976  0 17:26 ?        00:00:00 nginx: worker process
root      50979  45214  0 17:26 pts/0    00:00:00 grep --color=auto nginx

5.停止nginx

[root@web-9 /opt]#/opt/nginx/sbin/nginx -s stop

yum源安装

1.配置阿里云yum epel源
2.yum install nginx -y

yum安装自建nginx.repo

1. 配置官网yum源,一键安装即可

cat > /etc/yum.repos.d/nginx.repo << 'EOF'
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
EOF

2.清空yum源,安装最新版nginx
[root@web-8 /etc/yum.repos.d]#yum clean all

[root@web-8 /etc/yum.repos.d]#yum install nginx -y

3.查看PATH变量
[root@web-8 /etc/yum.repos.d]#which nginx
/usr/sbin/nginx
[root@web-8 /etc/yum.repos.d]#ll /usr/sbin/nginx
-rwxr-xr-x 1 root root 1377720 Nov 16  2021 /usr/sbin/nginx


[root@web-8 /etc/yum.repos.d]#nginx -V
nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

nginx管理命令

nginx -t  # 检测nginx.conf语法
nginx -s  reload  # 重新读取nginx.conf
nginx -s stop   # 停止nginx  kill -15 nginx

nginx  # 默认是直接运行,前提是当前机器没运行nginx

#你通过yum安装的nginx请你用systemctl去管理


# 不能多次执行nginx二进制命令
[root@web-8 ~]#
[root@web-8 ~]#nginx
[root@web-8 ~]#nginx # 会报错


# nginx -s reload ,会发生什么

nginx -s reload是给master进程发信号,重新读取配置信息,导致worker重新生成,因此worker-pid发生了变化

但是master进程id不带变化的(包工头,一直没变,更换了手底下的干活的工人)
================================
配置文件变化,就好比 合同变化了(包工头还是他,但是工人更换了一批)



===========
只有你restart的时候,包工头,也会被更换

你只能先停止, 再重启
nginx -s stop # 不会报错的

==============================

如果出现如下错误,如何解决,其实是通过pid,管理nginx的进程
 ~]#nginx -s stop
nginx: [error] invalid PID number "" in "/var/run/nginx.pid"
[root@web-8 ~]#
[root@web-8 ~]#
[root@web-8 ~]#cat /var/run/nginx.pid
[root@web-8 ~]#
[root@web-8 ~]#
[root@web-8 ~]#
[root@web-8 ~]#ps -ef |grep nginx
root       3599      1  0 16:10 ?        00:00:00 nginx: master process nginx
nginx      3628   3599  0 16:12 ?        00:00:00 nginx: worker process
root       3677   3434  0 16:19 pts/0    00:00:00 grep --color=auto nginx
[root@web-8 ~]#
[root@web-8 ~]#
[root@web-8 ~]#echo 3599 > /var/run/nginx.pid 
[root@web-8 ~]#
[root@web-8 ~]#
[root@web-8 ~]#nginx -s stop
[root@web-8 ~]#
[root@web-8 ~]#
[root@web-8 ~]#!ps
ps -ef |grep nginx
root       3686   3434  0 16:19 pts/0    00:00:00 grep --color=auto nginx


# 明确,现在用systemctl去管理nginx了
[root@web-8 ~]#systemctl start nginx


查看状态,reload, restart nginx,查看进程id号

[root@web-8 ~]#systemctl status nginx

[root@web-8 ~]#systemctl reload nginx  # worker变化,master不变

[root@web-8 ~]#systemctl restart nginx  # 整个nginx进程变化

# 用什么命令启动的,就用什么方式去管理该进程

nginx配置文件详解

安装完了之后,后续nginx的所有功能,都是围绕着修改nginx配置文件生效了

看懂配置文件,运维来说,达到手写nginx配置文件,才是合格的。
通过官网yum仓库默认安装的nginx.conf

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
~       


标签:web,http,--,基础,module,nginx,root
From: https://www.cnblogs.com/btcm409181423/p/18078566

相关文章

  • Python基础语法
    Python目录-基础语法注释数据类型数据类型准换字面量变量数据输出数据输入标识符运算符判断语句循环语句注释单行注释#以#号开头,#号和注释内容建议以一个空格隔开多行注释"""以三个双引号引起来三个双信号之中写注释内容"""数据类型Python中常用......
  • 通信基础概念
    参考视频:https://www.bilibili.com/video/BV14o4y1Y7A1?p=1&vd_source=432ba293ecfc949a4174ab91ccc526d6并行通信:并行通信:计算机与I/O设备之间,通过多条传输线,可以同时传输多个bit位的信号。如下图所示:串行通信:串行通信:计算机与I/O设备之间,同一时刻,只能传输一个bit位的信号。......
  • 数组基础
    数组定义数组是一种容器,能存储同类型的多个值。Tip存储数据时,需要考虑隐式转换的问题例如:int(booleanbyteshortintdouble)其中double不能转换成boolean。建议具体数值与数据类型保持一致。静态初始化初始化:在内存中为数组容器开辟空间,并将数据存入容器......
  • Unity3D 基础 05 场景加载
    Unity3D基础系列(第五期)场景配置首先,在资源文件夹里创建一个LoadScene场景,摆放一些物体,用来做初始场景。然后创建一个Stage文件夹,并创建两个场景Stage1、Stage2。两个Stage场景也可以简单布置一下,方便区分。接着,打开BuildSettings,把需要加载的场景添加到配置......
  • 防火墙基础
    目录一、基本内容1、防火墙2、分类3、防火墙常见的功能4、防火墙策略二、防火墙发展史1、包过滤防火墙2、应用代理防火墙3、状态监测防火墙4、web应用防火墙(WAF)5、多合一网关防火墙6、下一代防火墙(NGFW)三、Windowsdefender防火墙1、常用命令(图形化界面不再赘述......
  • Java基础 --- 面向对象
    面向对象面向对象介绍介绍面向:拿、找对象:能干活的东西面向对象编程:拿东西过来坐对应的事情设计对象并使用类和对象类(设计图):是对象共同特征的描述;对象:是真实存在的具体东西在Java中,必须先设计类,才能获得对象。如何定义类publicclass类名{1.成员变量(代......
  • Spring基础
    beans.xml配置文件<!--不建议使用--><!--<beanid="user"class="com.jiang.pojo.user">--><!--<constructor-argindex="0"value="岳不群"/>--><!--</bean>--><!-......
  • Linux的基础
    linux的基础命令linux命令的基本形式:command【-options】【parameter】command:命令command:参数(可选,非必填)options:选项(可选,非必填)Ls:以平铺的形式列出当前目录下的所有内容。ls【-a,-l,-h】【parameter】(可选,非必填)。-a:表示all,即列出全部文件(包含隐藏的文件/文件夹)。-l:......
  • 【提升C语言基础知识】数据在内存中的存储
    1.整数在内存中的存储1.1原码,反码,补码整数(占4个字节,即32个bit)的二进制表示方法有3种,即原码,反码,补码,三种表示方法均有符号位和数值位两部分原码:直接将数值按照正负数的形式翻译成二进制得到的就是原码反码:将原码符号位(最高位,0为正,1为负)不变,其余位按位取反得到反码补码:反码......
  • Python《基础知识》
    1.列表:list列表内的元素通过方括号[]来表示,且可以修改例:list=[1,2,3,"fhdsj","sum"]有关list的函数:cmp(list1,list2)比较两个列表的元素len(list)返回列表元素个数max(list)返回列表元素最大值min(list)返回列表元素最小值list(seq)将元组转换为列表示例:list......