nginx学习

129 阅读6分钟

nginx

简介

nginx 是一个 HTTP,反向代理服务器,邮件代理服务器,通用的 TCP/UDP 服务器。

安装

源码安装

比较推荐,但是更专业。自定义功能灵活性大

  1. 官网下载\

nginx_download.png

  1. 解压
    tar -zxvf nginx-1.24.0.tar.gz

  2. 通过使用 configure 命令。使用后会生成 Makefile 文件
    用法如下:

    ./configure
    --sbin-path=/usr/local/nginx/nginx
    --conf-path=/usr/local/nginx/nginx.conf
    --pid-path=/usr/local/nginx/nginx.pid
    --with-http_ssl_module
    --with-pcre=../pcre2-10.39
    --with-zlib=../zlib-1.3
    
    ./configure --with-http_ssl_module --with-stream --prefix=/home/hlw/nginx --with-http_stub_status_module
    

    --with-http_ssl_module 添加https协议支持到http server

    更多参数详见 nginx.org/en/docs/con…

    如果出现/configure: error: SSL modules require the OpenSSL library.
    使用如下指令: yum install openssl openssl-devel -y

  3. 配置完之后,nginx编译完,使用 make 命令。 make && make install

基本使用

nginx 有一个主进程和几个工作进程。主进程主要工作是读取和评估配置,维护工作进程。工作进程做实际的请求处理。
nginx 工作完全取决于配置文件的配置。配置文件一般位于 /usr/local/nginx/conf, /etc/nginx, or /usr/local/etc/nginx.

启动、停止、重新加载配置

启动:直接运行
停止:nginx -s stop
重新加载配置:nginx -s reload
重新打开日志文件:nginx -s reopen
测试配置文件是否正确:nginx -t

-s 参数只能在启动后使用。

nginx 重新加载配置文件的过程

  1. 当接收到命令时,首先检查配置是否正确,正确的话,主进程会开启新的工作进程,并发送信息给旧的工作进程让他们关闭。 如若不正确,主进程会回滚配置文件,依旧使用旧工作进程。
  2. 旧的工作进程在收到信息后,会停止接收请求,并等待正在进行的请求结束,然后退出。

配置文件的结构

nginx 是由多个模块组成的,这些模块通过配置文件中的指令控制的。
指令有2种,包括简单指令和块指令。
简单指令:指令 参数; 指令和参数中间空格隔开,以分号结尾。
块指令:与简单类似,但是将分号替换成一组指令,由{}包裹。

典型的块指令有:http stream server location events

使用 # 作为注释

作为静态资源服务器

打开配置文件,在server块中添加 location块

http {
    server {

        location / {    # 匹配/前缀的uri
            root /data/www; # 一旦匹配到,这个uri会被添加到 root 目录,即 /data/www/
        }

        location /images/ {   # /images/  匹配的url路径 
            root   /home/ftpuser/jiajiabao;  # 匹配后映射到服务器的哪个目录里  注意这个目录需要755权限,否则会提示 403 /home/ftpuser/jiajiabao/images/
            index  index.html index.htm;
        }
    }
}
  1. 如果有多个 location 块,nginx会选择最长的前缀的那个; 上面的位置块提供了长度为1的最短前缀,因此只有当所有其他位置块都无法提供匹配时,才会使用该块。
  2. 如果没生效,不妨试着找出原因在 access.log 和 error.log 文件中。

作为代理服务器

最常用的功能之一。接收客户端请求,并转发到代理服务器上,获取响应,并发送到客户端上。

打开配置文件,在server块中添加 location块

server {
    location / { # 未匹配到的,走这
        proxy_pass http://localhost:8080; # 代理到 http://localhost:8080 地址
    }

    location /images/ { # 匹配前缀/images,
        root /data; # 映射到 /data/images
    }
}
  1. 该配置会匹配/images 的请求映射到 /data/images 目录,转发所有其他的请求到代理服务器上

代理http和https

代理http
# 负载均衡
upstream myapp1 {
        server srv1.example.com;
        server srv2.example.com;
        server srv3.example.com;
}

server {
    listen       80;
    server_name  localhost;

    # 转发http请求
    location /http/ { # 未匹配到的,走这
        proxy_pass http://localhost:8080/; # 代理到 http://localhost:8080 地址
    }

    # 转发ocr请求
    location /ocr/ {
        # 负载均衡
        # 转发到https 需要在安装nginx时添加配置:--with-http_ssl_module --with-stream  --with-http_stub_status_module
        proxy_pass https://myapp1;
    }

}
  1. 客户端发送 http://localhost:80/http/ nginx 会转发到 http://localhost:8080/ 客户端发送 http://localhost:80/http/api/ocr nginx 会转发到 http://localhost:8080/api/ocr
  2. 转发请求,原请求的参数,header都会一起转发,但是host不会,如果要转发原请求的host,如下:
    1. 在 location 块里添加 proxy_set_header $host
    2. 设置负载均衡的id就是host名,
  3. 也可以转发到https服务

proxy_set_header 可以设置的值有:

$proxy_host: 代理的主机,也就是proxy_pass中设置的主机名,上个例子里就是 myapp1
$http_host: 请求头里的host属性
$host: 当请求头不存在host或者为空,就等于 server_name;如果存在,就等于host属性除了端口号的部分,如果host是 www.baidu.com:443, 那么host就是www.baidu.com

代理https

和http差不多,唯一区别是需要ssl证书,nginx需要安装插件。

  1. 安装nginx是添加参数:

    ./configure --prefix=/home/hlw/nginx --with-http_ssl_module --with-stream  --with-http_stub_status_module
    
    make && make install
    
  2. 配置如下:

    
    server {
        listen       443 ssl;
        server_name  localhost;
    
        # 指定证书
        ssl_certificate      cert.pem;
        # 指定证书key
        ssl_certificate_key  cert.key;
    
        # 转发https请求
        location /http/ { # 未匹配到的,走这
            proxy_pass http://localhost:8080/; # 代理到 http://localhost:8080 地址
        }
    
        # 转发ocr请求
        location /ocr/ {
            # 负载均衡
            # 
            proxy_pass https://myapp1;
        }
    
    }
    
    1. listen 443 ssl; 添加ssl
    2. 配置ssl证书和key

代理TCP和UDP

日志管理

nginx日志文件有2个,访问日志和错误日志(access.log 和 error.log)。
nginx本身没有日志归档的功能,所以需要借助外部工具,通常使用 logrotate

logrotate 是linux默认自带的日志归档工具。可以切割,转储日志

详细用法不介绍,这里配合nginx使用如下:

  1. 切换到 logrotate 的配置目录

    cd /etc/logrotate.d/
    
  2. 创建 nginx 文件,并编写如下内容:

    vi nginx
    
    /usr/local/nginx/logs/*.log {
        daily
        size 1M
        maxsize 10M
        missingok
        notifempty
        olddir /usr/local/nginx/logs/old
        dateext
        dateformat -%Y%m%d%H
        extension .log
        compress
        create
        postrotate
            /usr/local/nginx/sbin/nginx -s reopen
        endscript
        sharedscripts
        maxage 7
        rotate 7
    }
    

    注意:该文件不能有注释(#开头),否则报错
    解释:

    • daily 归档周期1天
    • size 1M 归档文件大于1M时才会归档
    • maxsize 10M 归档文件大于10M时,提前触发归档
    • missingok 归档文件缺失,不报错
    • notifempty 归档文件为空时不归档
    • olddir /usr/local/nginx/logs/old 归档文件存放目录,依个人更改
    • dateext 启用日期扩展
    • dateformat -%Y%m%d%H 日期格式
    • extension .log 归档文件添加后缀
    • compress 归档后启用压缩
    • create 创建模式,会将原文件重命名,归档后重新创建新的相同名字的文件
    • postrotate /usr/local/nginx/sbin/nginx -s reopen 依个人更改 endscript postrotate ... endscript 是一对使用,在归档后执行的指令
    • sharedscripts 共享脚本,在一次归档中出现多个日志文件,脚本文件只执行一次。
    • maxage 7 最大保存时间7天
    • rotate 7 归档份数 7份

    通常写完脚本后,可以执行测试:

    logrotate -d /etc/logrotate.d/nginx
    
  3. 在日志目录创建文件夹old

    cd /usr/local/nginx/logs/
    mkdir old
    

    因为前面配置了归档目录,所以手动创建下。

    该步骤也可省略,需配置 createolddir,当目录不存在是自动创建目录

  4. 其他 如果想强制执行一次归档,可执行如下指令:

    logrotate -f /etc/logrotate.d/nginx
    

参考文献

  1. nginx.org/en/docs/