Nginx

  1. 基本概念:
    • 一个高性能的http和反向代理web服务器,能支持高负载,效率高

    • 正向代理
      通过代理服务器访问服务器,例如搭建梯子才访问www.google.com。(可以隐藏客户端的信息)这个内容就叫正向代理

    • 反向代理
      一般情况下是
      image
      就是客户端对代理无感知,只是通过代理服务器去隐藏目标服务器的具体信息,只暴露代理服务器的地址,隐藏真实服务器的地址。
      反向代理后,客户端无法知道服务器的具体位置。

    • 负载均衡

默认情况下,是一个服务器,前后连锁相关。
image
将原先集中到单个服务器上的情况改为请求分发到多个服务器上。
image
- 动静分离
实际上就是前后端分离,nginx可以直接放前端的静态资源,而后端单独部署。
2. 安装,配置

  • 先安装依赖项(以centos7.8为例)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    yum update
    yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre pcre-devel
    #下载稳定版安装包
    wget https://nginx.org/download/nginx-1.24.0.tar.gz
    tar -zxvf nginx-1.24.0.tar.gz
    cd nginx-1.24.0
    #配置并安装
    ./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/usr/local/nginx/conf/nginx.pid --lock-path=/usr/local/nginx/lock/nginx.lock --with-http_ssl_module
    make && make install

3.

  • /usr/local/nginx/sbin/目录下,可以使用nginx的常用命令。(不方便,可以加入到path环境变量中)
    查看版本号./nginx -v启动nginx./nginx关闭nginx./nginx -s stop重新加载配置./nginx -s reload
  • 设置nginx为系统服务,
    /usr/lib/systemd/system目录下添加nginx.service
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    [Unit]
    Description=nginx - high performance web server
    Documentation=http://nginx.org/en/docs/
    After=network.target remote-fs.target nss-lookup.target

    [Service]
    Type=forking
    PIDFile=/usr/local/nginx/logs/nginx.pid
    ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
    ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    ExecReload=/bin/kill -s HUP $MAINPID
    ExecStop=/bin/kill -s QUIT $MAINPID
    PrivateTmp=true

    [Install]
    WantedBy=multi-user.target
    然后启动nginx并设置开机自启动
    1
    2
    3
    systemctl start nginx
    systemctl enable nginx
    systemctl status nginx #查看状态
  1. 配置文件
  2. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    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;
    }
    }
  3. 全局块
    设置整体运行的配置指令
    worker_processes 1;值越大,处理并发的数量越多
  4. events块
    设置nginx服务器和用户的网络连接
    work process 1024;表示最大支持1024个连接数
  5. http块
    • http全局块
    • server块(和虚拟主机有重要关系)
  • 每个 http 块可以包括多个 server 块,而每个 server 块就相当于一个虚拟主机。而每个 server 块也分为全局 server 块,以及可以同时包含多个 locaton 块。(☆☆☆☆☆)
  1. 反向代理实例
  • 实例1
    /usr/local/nginx/conf/nginx.conf文件中。
    修改其http块中的server块中的location

补充:location 指令说明

该指令用于匹配 URL, 语法如下:

1
2
3
location [ = | ~ | ~* | ^~] uri {

}
  • = :用于不含正则表达式的 uri 前,要求请求字符串与 uri 严格匹配, 如果匹配成功,就停止继续向下搜索并立即处理该请求。
  • ~:用于表示 uri 包含正则表达式,并且区分大小写。
  • ~*:用于表示 uri 包含正则表达式,并且不区分大小写。
  • ^~:用于不含正则表达式的 uri 前,要求 Nginx 服务器找到标识 uri 和请求

字符串匹配度最高的 location 后,立即使用此 location 处理请求,而不再使用 location块中的正则 uri 和请求字符串做匹配。
6. 动静分离
7. nginx配置高可用集群