nginx

nginx

1.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网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。

2.nginx 的特性与优点

2.1nginx的特性:

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

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

3.nginx的模块与工作原理
nginx由内核和模块组成。其中,内核的设计非常微小和简洁,完成的工作也非常简单,仅仅通过查找配置文件将客户端请求映射到一个location block(location是nginx配置中的一个指令,用于URL匹配),而在这个location中所配置的每个指令将会启动不同的模块去完成相应的工作。

3.1 nginx的模块分类
nginx的模块从结构上分为核心模块、基础模块和第三方模块

HTTP模块、EVENT模块和MAIL模块等属于核心模块
HTTP Access模块、HTTP FastCGI模块、HTTP Proxy模块和HTTP Rewrite模块属于基本模块
HTTP Upstream模块、Request Hash模块、Notice模块和HTTP Access Key模块属于第三方模块

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

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

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

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

4.nginx的安装与配置

4.1 nginx的安装
 # 关闭防火墙和selinux
[root@rl ~]# systemctl stop --now firewalld
[root@rl ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@rl ~]# setenforce 0
[root@rl ~]# vim /etc/selinux/config 
[root@rl ~]# cat /etc/selinux/config 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted


[root@rl ~]# 
# 创建系统用户
[root@rl ~]# useradd -r -M -s /sbin/nologin nginx

# 安装依赖包
[root@rl ~]#  yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ --allowerasing


# 创建日志存放目录
[root@rl ~]# mkdir -p /var/log/nginx
[root@rl ~]#  chown -R nginx.nginx /var/log/nginx


# 下载nginx
[root@rl src]# wget http://nginx.org/download/nginx-1.22.1.tar.gz
[root@rl src]# tar -xf  nginx-1.22.1.tar.gz 
[root@rl src]# ll
total 1052
drwxr-xr-x. 2 root root       6 Jun 22  2021 debug
drwxr-xr-x. 2 root root       6 Jun 22  2021 kernels
drwxr-xr-x. 8 1001 1001     158 Oct 19  2022 nginx-1.22.1
-rw-r--r--. 1 root root 1073948 Oct 19  2022 nginx-1.22.1.tar.gz

# 编译安装
[root@rl src]# cd nginx-1.22.1
[root@rl nginx-1.22.1]#  ./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@rl nginx-1.22.1]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
nginx安装后配置
# 配置环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@localhost ~]# . /etc/profile.d/nginx.sh


//服务控制方式,使用nginx命令
    -t  //检查配置文件语法
    -v  //输出nginx的版本
    -c  //指定配置文件的路径
    -s  //发送服务控制信号,可选值有{stop|quit|reopen|reload}

[root@localhost ~]# nginx
[root@localhost ~]# ss -antl
State    Recv-Q   Send-Q       Local Address:Port       Peer Address:Port   Process   
LISTEN   0        511                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 -s stop
[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=notify
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/usr/local/nginx/sbin/nginx -s reload

[Install]
WantedBy=multi-user.target
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl start nginx  

5 nginx的配置文件详解

主配置文件:/usr/local/nginx/conf/nginx.conf

  • 默认启动nginx时,使用的配置文件是:安装路径/conf/nginx.conf文件
  • 可以在启动nginx时通过-c选项来指定要读取的配置文件

nginx常见的配置文件及其作用

配置文件作用
nginx.confnginx的基本配置文件
mime.typesMIME类型关联的扩展文件
fastcgi.conf与fastcgi相关的配置
proxy.conf与proxy相关的配置
sites.conf配置nginx提供的网站,包括虚拟主机
//常见配置文件(如果是源码安装的nginx是没有proxy.conf和sites.conf的,yum装的就会有)
[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# ls
fastcgi.conf            koi-win             scgi_params
fastcgi.conf.default    mime.types          scgi_params.default
fastcgi_params          mime.types.default  uwsgi_params
fastcgi_params.default  nginx.conf          uwsgi_params.default
koi-utf                 nginx.conf.default  win-utf
5.1 nginx.conf配置详解

nginx.conf的内容分为以下几段:

  • main配置段:全局配置段。其中main配置段中可能包含event配置段

  • event {}:定义event模型工作特性

  • http {}:定义http协议相关的配置

    配置指令:要以分号结尾,语法格式如下:

    derective value1 [value2 ...];
    

    支持使用变量:

    • 内置变量:模块会提供内建变量定义
    • 自定义变量:set var_name value
5.2 用于调试、定位问题的配置参数
daemon {on|off};    //是否以守护进程方式运行nginx,调试时应设置为off
master_process {on|off};    //是否以master/worker模型来运行nginx,调试时可以设置为off
error_log 位置 级别;    //配置错误日志
示例: 

//关闭master/worker模式
//关闭前
[root@localhost ~]# head -5 /usr/local/nginx/conf/nginx.conf

user  root;
worker_processes  1;

#error_log  logs/error.log;
[root@localhost ~]# ps -ef | grep nginx
root       54876       1  0 15:59 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
root       55113   54876  0 16:26 ?        00:00:00 nginx: worker process
root       55119    1702  0 16:30 pts/0    00:00:00 grep --color=auto nginx

//添加master_process off;参数关闭
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost ~]# head -5 /usr/local/nginx/conf/nginx.conf

user  root;
master_process off;   #添加此行
worker_processes  1;

[root@localhost ~]# 

//改变工作模式后要重启服务,重新加载配置文件不会生效
[root@localhost ~]# nginx -s stop 
[root@localhost ~]# nginx 
[root@localhost ~]# ps -ef | grep nginx
root       55131       1  0 16:32 ?        00:00:00 nginx
root       55133    1702  0 16:32 pts/0    00:00:00 grep --color=auto nginx

error_log里的位置和级别能有以下可选项:

位置级别
file stderr syslog:server=address[,parameter=value] memory:sizedebug:若要使用debug级别,需要在编译nginx时使用–with-debug选项 info notice warn error crit alert emerg
示例: 
//打开日志功能
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost ~]# head -7 /usr/local/nginx/conf/nginx.conf
user  root;
worker_processes  1;

error_log  logs/error.log;    #取消注释,默认是error级别
#error_log  logs/error.log  notice;    #这是notice级别的
#error_log  logs/error.log  info;
[root@localhost ~]# nginx -s reload
[root@localhost ~]# ls /usr/local/nginx/logs/
error.log  nginx.pid          #有日志文件了
[root@localhost ~]# 



5.3 正常运行必备的配置参数
user USERNAME [GROUPNAME];    //指定运行worker进程的用户和组
pid /path/to/pid_file;    //指定nginx守护进程的pid文件
worker_rlimit_nofile number;    //设置所有worker进程最大可以打开的文件数,默认为1024
worker_rlimit_core size;    //指明所有worker进程所能够使用的总体的最大核心文件大小,保持默认即可
示例:
//默认是以nginx用户运行的
[root@localhost ~]# ps -ef | grep nginx
root       54876       1  0 15:59 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx      55006   54876  0 16:22 ?        00:00:00 nginx: worker process
root       55008    1702  0 16:22 pts/0    00:00:00 grep --color=auto nginx
[root@localhost ~]# 

//改为root用户
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost ~]# head -5 /usr/local/nginx/conf/nginx.conf

user  root;      #改为root
worker_processes  1;

#error_log  logs/error.log;
[root@localhost ~]# nginx -s reload
[root@localhost ~]# ps -ef | grep nginx
root       54876       1  0 15:59 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
root       55113   54876  0 16:26 ?        00:00:00 nginx: worker process
root       55115    1702  0 16:26 pts/0    00:00:00 grep --color=auto nginx
5.4 优化性能的配置参数
worker_processes n;    //启动n个worker进程,这里的n为了避免上下文切换,通常设置为cpu总核心数-1或等于总核心数
worker_cpu_affinity cpumask ...;    //将进程绑定到某cpu中,避免频繁刷新缓存
//cpumask:使用8位二进制表示cpu核心,如:
    0000 0001   //第一颗cpu核心
    0000 0010   //第二颗cpu核心
    0000 0100   //第三颗cpu核心
    0000 1000   //第四颗cpu核心
    0001 0000   //第五颗cpu核心
    0010 0000   //第六颗cpu核心
    0100 0000   //第七颗cpu核心
    1000 0000   //第八颗cpu核心
timer_resolution interval;    //计时器解析度。降低此值,可减少gettimeofday()系统调用的次数
worker_priority number;    //指明worker进程的nice值
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值