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.1 nginx的特性

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

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

2.2 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,内存映射)等性能优化

3. nginx的功能及应用类别

3.1 nginx的基本功能

  • 静态资源的web服务器,能缓存打开的文件描述符
  • http、smtp、pop3协议的反向代理服务器
  • 缓存加速、负载均衡
  • 支持FastCGI(fpm,LNMP),uWSGI(Python)等
  • 模块化(非DSO机制),过滤器zip、SSI及图像的大小调整
  • 支持SSL

3.2 nginx的扩展功能

  • 基于名称和IP的虚拟主机
  • 支持keepalive
  • 支持平滑升级
  • 定制访问日志、支持使用日志缓冲区提高日志存储性能
  • 支持URL重写
  • 支持路径别名
  • 支持基于IP及用户的访问控制
  • 支持速率限制,支持并发数限制

3.3 nginx的应用类别

  • 使用nginx结合FastCGI运行PHP、JSP、Perl等程序
  • 使用nginx作反向代理、负载均衡、规则过滤
  • 使用nginx运行静态HTML网页、图片
  • nginx与其他新技术的结合应用

4. nginx的模块与工作原理

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

4.1 nginx的模块分类

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

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

用户根据自己的需要开发的模块都属于第三方模块。正是有了如此多模块的支撑,nginx的功能才会如此强大

nginx模块从功能上分为三类,分别是:

  • Handlers(处理器模块)。此类模块直接处理请求,并进行输出内容和修改headers信息等操作。handlers处理器模块一般只能有一个
  • Filters(过滤器模块)。此类模块主要对其他处理器模块输出的内容进行修改操作,最后由nginx输出
  • Proxies(代理器模块)。就是nginx的HTTP Upstream之类的模块,这些模块主要与后端一些服务比如fastcgi等操作交互,实现服务代理和负载均衡等功能

nginx模块分为:核心模块、事件模块、标准Http模块、可选Http模块、邮件模块、第三方模块和补丁等

  • nginx基本模块:所谓基本模块,指的是nginx默认的功能模块,它们提供的指令,允许你使用定义nginx基本功能的变量,在编译时不能被禁用,包括:
    • 核心模块:基本功能和指令,如进程管理和安全。常见的核心模块指令,大部分是放置在配置文件的顶部
    • 事件模块:在Nginx内配置网络使用的能力。常见的events(事件)模块指令,大部分是放置在配置文件的顶部
    • 配置模块:提供包含机制

具体的指令,请参考nginx的官方文档

4.2 nginx的工作原理

nginx的模块直接被编译进nginx,因此属于静态编译方式。
启动nginx后,nginx的模块被自动加载,与Apache不一样,首先将模块编译为一个so文件,然后在配置文件中指定是否进行加载。
在解析配置文件时,nginx的每个模块都有可能去处理某个请求,但是同一个处理请求只能由一个模块来完成。

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

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

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

5. nginx的安装与配置

5.1 nginx的安装

[root@localhost ~]# wget https://nginx.org/download/nginx-1.20.2.tar.gz
--2022-10-10 20:08:17--  https://nginx.org/download/nginx-1.20.2.tar.gz
Resolving nginx.org (nginx.org)... 52.58.199.22, 3.125.197.172, 2a05:d014:edb:5702::6, ...
Connecting to nginx.org (nginx.org)|52.58.199.22|:443... 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   320KB/s    in 3.2s    

2022-10-10 20:08:22 (320 KB/s) - 'nginx-1.20.2.tar.gz' saved [1062124/1062124]

[root@localhost ~]# wget https://nginx.org/download/nginx-1.22.0.tar.gz
--2022-10-10 20:08:55--  https://nginx.org/download/nginx-1.22.0.tar.gz
Resolving nginx.org (nginx.org)... 52.58.199.22, 3.125.197.172, 2a05:d014:edb:5702::6, ...
Connecting to nginx.org (nginx.org)|52.58.199.22|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1073322 (1.0M) [application/octet-stream]
Saving to: 'nginx-1.22.0.tar.gz'

nginx-1.22.0.tar.gz  100%[====================>]   1.02M   207KB/s    in 5.1s    

2022-10-10 20:09:02 (207 KB/s) - 'nginx-1.22.0.tar.gz' saved [1073322/1073322]

[root@localhost ~]# ls
anaconda-ks.cfg  nginx-1.20.2.tar.gz  nginx-1.22.0.tar.gz
[root@localhost ~]# tar xf nginx-1.20.2.tar.gz 
[root@localhost ~]# ls
anaconda-ks.cfg  nginx-1.20.2  nginx-1.20.2.tar.gz  nginx-1.22.0.tar.gz
[root@localhost ~]# cd nginx-1.20.2
[root@localhost nginx-1.20.2]# ls
CHANGES     LICENSE  auto  configure  html  src
CHANGES.ru  README   conf  contrib    man
[root@localhost nginx-1.20.2]# useradd -r -M -s /sbin/nologin nginx
[root@localhost nginx-1.20.2]# dnf -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
//创建日志存放目录
[root@localhost nginx-1.20.2]# mkdir -p /var/log/nginx
[root@localhost nginx-1.20.2]# chown -R nginx.nginx /var/log/nginx
[root@localhost nginx-1.20.2]# ll /var/log/
drwxr-xr-x. 2 nginx nginx      6 Oct 10 20:36 nginx
[root@localhost nginx-1.20.2]# ls
CHANGES     LICENSE  auto  configure  html  src
CHANGES.ru  README   conf  contrib    man
[root@localhost 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@localhost nginx-1.20.2]# make -j 2
[root@localhost nginx-1.20.2]# ls
CHANGES     LICENSE   README  conf       contrib  man   src
CHANGES.ru  Makefile  auto    configure  html     objs
[root@localhost nginx-1.20.2]# 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.20.2]# 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]=ea956ab9f0148986147d82e82d94c32909bfc659, with debug_info, not stripped
[root@localhost nginx-1.20.2]# du -sh objs/nginx
6.1M    objs/nginx
[root@localhost nginx-1.20.2]# du -sh /bin/ls
144K    /bin/ls
[root@localhost nginx-1.20.2]# ls /usr/local/
bin  etc  games  include  lib  lib64  libexec  sbin  share  src
[root@localhost nginx-1.20.2]# make install
[root@localhost nginx-1.20.2]# cd /usr/local/nginx/
[root@localhost nginx]# ls
conf  html  logs  sbin
[root@localhost nginx]# ls conf/
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
[root@localhost nginx]# ls html/
50x.html  index.html
[root@localhost nginx]# ls logs/
[root@localhost nginx]# ls sbin/
nginx
[root@localhost nginx]# du -sh sbin/nginx 
6.1M    sbin/nginx
[root@localhost nginx]# ls
conf  html  logs  sbin
[root@localhost nginx]# /usr/local/nginx/sbin/nginx 
[root@localhost 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               [::]:*             
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# getenforce 
Disabled

5.2 nginx安装后配置

[root@localhost nginx]# ll
total 8
-rw-r--r--. 1 root root 446 Oct 10 21:20 access.log
-rw-r--r--. 1 root root 260 Oct 10 21:20 error.log
[root@localhost nginx]# tail access.log 
192.168.29.1 - - [10/Oct/2022:21:20:35 +0800] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 Edg/106.0.1370.37"
192.168.29.1 - - [10/Oct/2022:21:20:35 +0800] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.29.138/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 Edg/106.0.1370.37"
[root@localhost nginx]# source /etc/profile.d/nginx.sh 
[root@localhost nginx]# cd
[root@localhost ~]# which 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               [::]:*               

6. nginx的配置文件详解

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

  • 默认启动nginx时,使用的配置文件是:安装路径/conf/nginx.conf文件
  • 可以在启动nginx时通过-c选项来指定要读取的配置文件
[root@localhost ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/nginx.service
[root@localhost ~]# vim /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 ~]# systemctl daemon-reload
[root@localhost ~]# nginx -s stop
[root@localhost ~]# systemctl status nginx
● nginx.service - nginx server daemon
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset>
   Active: inactive (dead)
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# systemctl status nginx
● nginx.service - nginx server daemon
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset>
   Active: active (running) since Tue 2022-10-11 16:14:41 CST; 4s ago
  Process: 288049 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SU>
 Main PID: 288050 (nginx)
    Tasks: 2 (limit: 12221)
   Memory: 2.7M
   CGroup: /system.slice/nginx.service
           ├─288050 nginx: master process /usr/local/nginx/sbin/nginx
           └─288051 nginx: worker process

Oct 11 16:14:41 localhost.localdomain systemd[1]: Starting nginx server daemon...
Oct 11 16:14:41 localhost.localdomain systemd[1]: Started nginx server daemon.
设置开机自启
[root@localhost ~]# systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@localhost ~]# systemctl status nginx
● nginx.service - nginx server daemon
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset:>
   Active: active (running) since Tue 2022-10-11 16:14:41 CST; 36s ago
 Main PID: 288050 (nginx)
    Tasks: 2 (limit: 12221)
   Memory: 2.7M
   CGroup: /system.slice/nginx.service
           ├─288050 nginx: master process /usr/local/nginx/sbin/nginx
           └─288051 nginx: worker process

Oct 11 16:14:41 localhost.localdomain systemd[1]: Starting nginx server daemon...
Oct 11 16:14:41 localhost.localdomain systemd[1]: Started nginx server daemon.
lines 1-12/12 (END)
[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               [::]:*        [root@localhost ~]# cp /usr/local/nginx/conf/nginx.conf /opt/
[root@localhost ~]# ls /usr/local/nginx/conf/
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
[root@localhost ~]# cp /usr/local/nginx/conf/mime.types /opt/
[root@localhost ~]# ls /opt/
mime.types  nginx.conf
[root@localhost ~]# vim /opt/nginx.conf 
 listen       8080;
[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               [::]:*                
[root@localhost ~]# nginx -c /opt/nginx.conf 
[root@localhost ~]# ss -antl
State    Recv-Q   Send-Q     Local Address:Port     Peer Address:Port   Process   
LISTEN   0        128              0.0.0.0:8080          0.0.0.0:*                
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常见的配置文件及其作用

配置文件 作用
nginx.conf nginx的基本配置文件
mime.types MIME类型关联的扩展文件
fastcgi.conf 与fastcgi相关的配置
proxy.conf 与proxy相关的配置
sites.conf 配置nginx提供的网站,包括虚拟主机
[root@localhost opt]# cd /usr/local/nginx/conf/
[root@localhost conf]# grep -v '#' nginx.conf

worker_processes  1;




events {
    worker_connections  1024;
}


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



    sendfile        on;

    keepalive_timeout  65;


    server {
        listen       80;
        server_name  localhost;



        location / {
            root   html;
            index  index.html index.htm;
        }


        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }



    }










}

6.1 nginx.conf配置详解

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

  • main配置段:全局配置段。其中main配置段中可能包含event配置段
  • event {}:定义event模型工作特性
  • http {}:定义http协议相关的配置

支持使用变量:

  • 内置变量:模块会提供内建变量定义
  • 自定义变量:set var_name value

6.2 用于调试,定位问题的配置参数

daemon {on|off}; //是否以守护进程方式运行nginx,调试时应设置为off
master_process {on|off}; //是否以master/worker模型来运行nginx,调试时可以设置为off
error_log 位置 级别; //配置错误日志

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

位置 级别
file stderr syslog:server=address[,parameter=value] memory:size debug:若要使用debug级别,需要在编译nginx时使用--with-debug选项 info notice warn error crit alert emerg
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
daemon off;
[root@localhost ~]# ps -ef | grep nginx
root         892       1  0 17:26 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx        894     892  0 17:26 ?        00:00:00 nginx: worker process
root       25949       1  0 17:42 ?        00:00:00 nginx: master process nginx -c /opt/nginx.conf
nginx      25950   25949  0 17:42 ?        00:00:00 nginx: worker process
root      115045   85587  0 18:40 pts/2    00:00:00 grep --color=auto nginx
[root@localhost ~]# systemctl restart 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               [::]:*                
[root@localhost ~]# ps -ef | grep nginx
root      116309   85587  0 18:41 pts/2    00:00:00 systemctl restart nginx
root      116314       1  0 18:41 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     116316  116314  0 18:41 ?        00:00:00 nginx: worker process
root      119438  116577  0 18:42 pts/0    00:00:00 grep --color=auto nginx

[root@localhost ~]# nginx
^C[root@localhost ~]# 
[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 ~]# vim /usr/local/nginx/conf/nginx.conf
master_process off;
[root@localhost ~]# nginx

[root@localhost ~]# ps -ef | grep nginx
root      303415   85587  0 19:42 pts/2    00:00:00 nginx
root      304369  116577  0 19:42 pts/0    00:00:00 grep --color=auto nginx

[root@localhost ~]# ls /var/log/nginx/
access.log  error.log
[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

^C[root@localhost ~]# tail /var/log/nginx/error.log 
2022/10/10 21:20:35 [error] 31770#0: *1 open() "/usr/local/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.29.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.29.138", referrer: "http://192.168.29.138/"
2022/10/10 21:47:08 [notice] 120515#0: signal process started
2022/10/11 16:07:56 [notice] 265983#0: signal process started
2022/10/11 16:15:50 [notice] 291800#0: signal process started
2022/10/11 16:35:13 [error] 894#0: *1 open() "/usr/local/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.29.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.29.138", referrer: "http://192.168.29.138/"
2022/10/11 16:35:21 [error] 25950#0: *1 "/usr/local/nginx/html/jenkins/index.html" is not found (2: No such file or directory), client: 192.168.29.1, server: localhost, request: "GET /jenkins/ HTTP/1.1", host: "192.168.29.138:8080"
2022/10/11 16:35:21 [error] 25950#0: *1 open() "/usr/local/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.29.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.29.138:8080", referrer: "http://192.168.29.138:8080/jenkins/"
2022/10/11 18:41:35 [notice] 116311#0: signal process started
2022/10/11 18:41:35 [alert] 892#0: unlink() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
2022/10/11 19:47:58 [error] 316365#0: *1 open() "/usr/local/nginx/html/jjyy" failed (2: No such file or directory), client: 192.168.29.1, server: localhost, request: "GET /jjyy HTTP/1.1", host: "192.168.29.138"
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
error_log  /var/log/nginx/error.log;
[root@localhost ~]# nginx

[root@localhost ~]# curl 127.0.0.1/fjdafjld
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.20.2</center>
</body>
</html>
[root@localhost ~]# tail /var/log/nginx/error.log 
2022/10/10 21:47:08 [notice] 120515#0: signal process started
2022/10/11 16:07:56 [notice] 265983#0: signal process started
2022/10/11 16:15:50 [notice] 291800#0: signal process started
2022/10/11 16:35:13 [error] 894#0: *1 open() "/usr/local/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.29.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.29.138", referrer: "http://192.168.29.138/"
2022/10/11 16:35:21 [error] 25950#0: *1 "/usr/local/nginx/html/jenkins/index.html" is not found (2: No such file or directory), client: 192.168.29.1, server: localhost, request: "GET /jenkins/ HTTP/1.1", host: "192.168.29.138:8080"
2022/10/11 16:35:21 [error] 25950#0: *1 open() "/usr/local/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.29.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.29.138:8080", referrer: "http://192.168.29.138:8080/jenkins/"
2022/10/11 18:41:35 [notice] 116311#0: signal process started
2022/10/11 18:41:35 [alert] 892#0: unlink() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
2022/10/11 19:47:58 [error] 316365#0: *1 open() "/usr/local/nginx/html/jjyy" failed (2: No such file or directory), client: 192.168.29.1, server: localhost, request: "GET /jjyy HTTP/1.1", host: "192.168.29.138"
2022/10/11 19:51:55 [error] 329706#0: *1 open() "/usr/local/nginx/html/fjdafjld" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET /fjdafjld HTTP/1.1", host: "127.0.0.1"
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

#user  nobody;
worker_processes  1;

error_log  /var/log/nginx/error.log;
[root@localhost ~]# systemctl start 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               [::]:*     
[root@localhost ~]# ps -ef | grep nginx
root      341657       1  0 19:54 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     341658  341657  0 19:54 ?        00:00:00 nginx: worker process
root      343154   85587  0 19:54 pts/2    00:00:00 grep --color=auto nginx

6.3 正常运行必备的配置参数

user USERNAME [GROUPNAME]; //指定运行worker进程的用户和组
pid /path/to/pid_file; //指定nginx守护进程的pid文件
worker_rlimit_nofile number; //设置所有worker进程最大可以打开的文件数,默认为1024
worker_rlimit_core size; //指明所有worker进程所能够使用的总体的最大核心文件大小,保持默认即可

[root@localhost logs]# ulimit
unlimited
[root@localhost logs]# ulimit -n
1024

6.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值

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
worker_rlimit_nofile 65535;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
[root@localhost ~]# ps aux | head
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  0.6 244688 13276 ?        Ss   17:26   0:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 18
root           2  0.0  0.0      0     0 ?        S    17:26   0:00 [kthreadd]
root           3  0.0  0.0      0     0 ?        I<   17:26   0:00 [rcu_gp]
root           4  0.0  0.0      0     0 ?        I<   17:26   0:00 [rcu_par_gp]
root           6  0.0  0.0      0     0 ?        I<   17:26   0:00 [kworker/0:0H-kblockd]
root           9  0.0  0.0      0     0 ?        I<   17:26   0:00 [mm_percpu_wq]
root          10  0.0  0.0      0     0 ?        S    17:26   0:00 [ksoftirqd/0]
root          11  0.1  0.0      0     0 ?        I    17:26   0:19 [rcu_sched]
root          12  0.0  0.0      0     0 ?        S    17:26   0:00 [migration/0]
[root@localhost ~]# ps -elf | head -2
F S UID          PID    PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
4 S root           1       0  0  80   0 - 61172 do_epo 17:26 ?        00:00:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 18

[root@localhost ~]# ps -elf | grep nginx
1 S root      405370       1  0  80   0 - 20419 -      20:13 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
5 S nginx     409568  405370  0  80   0 - 28580 do_epo 20:15 ?        00:00:00 nginx: worker process
0 S root      422697  116577  0  80   0 -  2304 -      20:19 pts/0    00:00:00 grep --color=auto nginx

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

worker_cpu_affinity auto;
[root@localhost ~]# ps -elf | grep nginx
1 S root      431376       1  0  80   0 - 20409 -      20:21 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
5 S nginx     431377  431376  0  80   0 - 28557 do_epo 20:21 ?        00:00:00 nginx: worker process
0 S root      432157  116577  0  80   0 -  2304 -      20:21 pts/0    00:00:00 grep --color=auto nginx

6.5 事件相关的配置:event{}段中的配置参数

accept_mutex {off|on}; //master调度用户请求至各worker进程时使用的负载均衡锁;on表示能让多个worker轮流地、序列化地去响应新请求
lock_file file; //accept_mutex用到的互斥锁锁文件路径use [epoll | rtsig | select | poll]; //指明使用的事件模型,建议让nginx自行选择
worker_connections #; //每个进程能够接受的最大连接数

[root@localhost ~]# cd /usr/local/nginx/logs/
[root@localhost logs]# ls
nginx.pid

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
worker_priority -10;
lock_file logs/nginx.lock;
[root@localhost ~]# systemctl restart nginx

[root@localhost logs]# systemctl stop nginx
[root@localhost logs]# nginx
[root@localhost logs]# 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               [::]:*     

6.6 网络连接相关的配置参数

keepalive_timeout number; //长连接的超时时长,默认为65s
keepalive_requests number; //在一个长连接上所能够允许请求的最大资源数
keepalive_disable [msie6|safari|none]; //为指定类型的UserAgent禁用长连接
tcp_nodelay on|off; //是否对长连接使用TCP_NODELAY选项,为了提升用户体验,通常设为on
client_header_timeout number; //读取http请求报文首部的超时时长
client_body_timeout number; //读取http请求报文body部分的超时时长
send_timeout number; //发送响应报文的超时时长

[root@localhost logs]# vim /usr/local/nginx/conf/nginx.conf

log_format  mary  '$remote_addr -  [$time_local] "$request" $status '
                      '"$http_user_agent" ';
    access_log  /var/log/nginx/access.log  mary;
[root@localhost ~]# systemctl reload nginx
[root@localhost logs]# tail -f /var/log/nginx/access.log 
192.168.29.1 - - [10/Oct/2022:21:20:35 +0800] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 Edg/106.0.1370.37"
192.168.29.1 - - [10/Oct/2022:21:20:35 +0800] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.29.138/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 Edg/106.0.1370.37"
192.168.29.1 - - [11/Oct/2022:16:35:12 +0800] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 Edg/106.0.1370.37"
192.168.29.1 - - [11/Oct/2022:16:35:13 +0800] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.29.138/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 Edg/106.0.1370.37"
192.168.29.1 - - [11/Oct/2022:16:35:21 +0800] "GET /jenkins/ HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 Edg/106.0.1370.37"
192.168.29.1 - - [11/Oct/2022:16:35:21 +0800] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.29.138:8080/jenkins/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 Edg/106.0.1370.37"
192.168.29.1 - - [11/Oct/2022:16:37:10 +0800] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 Edg/106.0.1370.37"
192.168.29.1 - - [11/Oct/2022:19:47:58 +0800] "GET /jjyy HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 Edg/106.0.1370.37"
127.0.0.1 - - [11/Oct/2022:19:51:55 +0800] "GET /fjdafjld HTTP/1.1" 404 153 "-" "curl/7.61.1"

(再访问一次浏览器)


192.168.29.1 - - [11/Oct/2022:21:12:34 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 Edg/106.0.1370.37"

6.7 fastcgi的相关配置参数

LNMP:php要启用fpm模型
配置示例如下:


location ~ \.php$ {
  root html;
  fastcgi_pass 127.0.0.1:9000;      //定义反向代理
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  include fastcgi_params;
}
posted @ 2022-10-10 21:50  溜溜威  阅读(260)  评论(0编辑  收藏  举报