Nginx学习

216 阅读4分钟

一. 课程大纲

1. Nginx 概述

历史背景
应用场景
主要特点
部署使用

2. 基础使用

进程结构
热部署
模块分类
基础语法

3. 进阶使用

core模块
log模块
rewrite模块
ssl模块

4. 场景实践

反向代理
负载均衡
https
限速
动静分离

5. 软件架构

同步异步
阻塞非阻塞
多路IO复用
连接池

6. 性能优化

TCP协议栈优化
磁盘IO优化
模块优化
内存分配优化
安装第一个rpm包Nginx (CentOS 虚拟机)
yum install epel-release -y
yum install nginx -y
rpm -ql nginx
rpm -ql nginx | grep bin
/usr/sbin/nginx -h

二.

1. Nginx 编译安装的配置参数

    **常用配置参数**
参数含义
--prefix指定安装的目录
--user运行nginx的worker子进程的属主
--group运行nginx的worker子进程的属组
--pid-path存放进程运行pid文件的路径
--conf-path配置文件nginx.conf的存放路径
--error-log-path错误日志error.log的存放路径
--http-log-path访问日志access.log的存放路径
--with-pcrepcre库的存放路径,正则表达式会用到
--with-zlibzlib库的存放路径,gzip模块会用到

2. 手动定制编译Nginx

  • 下载Nginx安装包、pcre包、zlib库

    Nginx
    pcre
    zlib

  • 解压包 到/opt/source 目录下

tar xf [包名]
eg: tar xf nginx.tar.gz
  • 安装Nginx
./configure --prefix=/opt/nginx --conf-path=/opt/nginx/conf/nginx.conf --user=nginx --group=nginx --pid-path=/opt/nginx/pid/nginx.pid --error-log-path=/opt/nginx/logs/error.log --with-pcre=/opt/source/pcre-8.43 --with-zlib=/opt/source/zlib-1.2.12 --with-http_ssl_module --with-http_image_filter_module --with-http_stub_status_module --http-log-path=/opt/nginx/logs/access/log

--提示缺少GD library
yum install gd gd-devel
--编译源码
make
--安装Nginx
make install
--修改nginx.conf 文件
vim /opt/nginx/conf/nginx.conf
#user nobyd; ===> user nginx;
worker_processes 1; ===> worker_processes auto;

--添加nginx用户
useradd nginx
-- 启动nginx
/opt/nginx/sbin/nginx

3. 配置文件结构

erDiagram
Main ||--o{ events : l
Main ||..|{ http : l
http ||--|{ server : contains
server ||--|{ location : l

4. root和alias用法区别

            语法: root path;
root ======> 
            context: http server location if
 
             语法: alias path;
alias ======> 
            context: location
            
 相同点: URI到磁盘文件的映射
 区别:   root会将定义路径与URI叠加,alias则只取定义路径
  
  location /picture {
      root /opt/nginx/html/picture;
  }
  客户端请求www.test.com/picture/1.jpg 则对应磁盘映射路径为 
  /opt/nginx/html/picture/picture/1.jpg
  
   location /picture {
      alias /opt/nginx/html/picture/;
  }
  客户端请求www.test.com/picture/1.jpg 则对应磁盘映射路径为 
  /opt/nginx/html/picture/1.jpg
  
-   使用alias时,末尾一定加/
-   alias只能位于location块中

5. location 语法结构

语法: location [ = | ~ | ~* | ^~ ] uri {...}
Context: server location
匹配规则含义示例
=精确匹配location = /images/ {...}
~正则匹配,区分大小写location ~ .(jpg|gif)$ {...}
~*正则匹配,不区分大小写location ~* .(jpg|gif)$ {...}
^~匹配到即停止搜索location ^~ /images/ {...}
不带任何符号location / {...}
location = /match_all/ {
    root html;
    index index.html;
}
location ~ \.(jpeg|jpg)$ {
    root html/images;
}
location ^~ /bbs/ {
    root html;
    index index.html index.html;
}

参考链接

location中URL结尾的反斜线 /
不带/   location /test {...}
带/     location /test/ {...}

不带/ /test 会先去寻找/test 目录下的index.html, 如果没有则寻找/test目录下文件名为test的文件
带/ /test/ 会先去寻找/test 目录下的index.html,如果没有则返回404 Not found

6. return 功能

介绍: 停止处理请求,直接返回响应码或重定向到其他URL、执行return指令后,location中后续指令将不会被执行

location / {
    ....
    return 404;
    ....
}

语法: return code [text];
       return code URL;
       return URL;
默认值: -
Context: server、location、if

eg: 
    location / {
        #return 200 "return 200 HTTP Status";
        #return 302 /bbs;
        return  https://google.com
    }

7. rewrite 语法

语法: rewrite regex replacement [flag];
默认值: -
Context: server、location、if
eg: rewrite /images/(.*\.jpg)$ /pic/$1;
flag可选值含义
last重写后的URL发起新请求,再此进入server段,重试location中的匹配
break直接使用重写后的URL,不在匹配其他location中语句
redirect返回302临时重定向
permanent返回301永久重定向

8. if 语法

语法: if (condition) {...};
默认值: -
Context: server、location
eg: if ($http_user_agent ~ Chrome) {
        rewrite /(.*) /browser/$1 break;
    }
    
    location /search/ {
        if ( $remote_addr = "192.168.0.1" ) {
            return 200 "test if OK in URL /search/";
        }
    }
    
    location / {
        if ( $uri = "/images/" ) {
            rewrite (.*) /pics/ break;
        }
    }
$variable仅为变量时,值为空或以0开头字符串都会被当作false处理
= 或 !=相等或不等比较
~ 或 !~正则匹配或非正则匹配
~*正则匹配,不区分大小写
-f 或 !-f检查文件存在或不存在
-d 或 !-d检查目录存在或不存在
-e 或 !-e检查文件、目录、符号链接等存在或存在
-x 或 !-x检查文件可执行或不可执行

9. Nginx常用变量

变量名含义
remote_addr客户端IP地址
remote_port客户端端口
server_addr服务端IP地址
server_port服务端端口
server_protocol服务端协议
binary_remote_addr二进制格式的客户端IP地址
connectionTCP连接的序号,递增
connection_requestTCP连接当前的请求数量
proxy_protocol_addr若使用了proxy_protocol协议,则返回协议中地址 否则返回空
proxy_protocol_port若使用了proxy_protocol协议,则返回协议中端口 否则返回空
uri请求的URL,不包含参数
request_uri请求的URL,包含参数
scheme协议名,http或https
request_method请求方法
request_length全部请求的长度,包括请求行、请求头、请求体
args全部参数字符串
arg_参数名特定参数值
is_argsURL中有参数,则返回? 否则返回空
query_string与args相同
remote_user由HTTP Basic Authentication协议传入的用户名
host先看请求行,再看请求头,最后找server_name
http_user_agent用户浏览器
http_referer从哪些链接过来的请求
http_via经过一层代理服务器,添加对应代理服务器的信息
http_x_forwarded_for获取用户真实IP
http_cookie用户cookie
request_time处理请求已耗费的时间
request_completion请求处理完成返回OK,否则返回空
server_name匹配上请求的server_name的值
https若开启https,则返回on,否则返回空
request_filename磁盘文件系统待访问文件的完整路径
document_root由URI和root/alias规则生成的文件夹路径
realpath_root将document_root中的软链接换成真实路径
limit_rate返回响应时的速度上限值
http_[header]
  1. proxy_pass 语法
语法: proxy_pass URL;
默认值: 无
Context: location、if、limit_except
eg: proxy_pass http://127.0.0.1:8080
eg: proxy_pass http://127.0.0.1:8090/proxy

URL参数原则
- URL必须以http或https开头
- URL中可以携带变量
- URL中是否带URI,会直接影响发往上游请求的URL

location /proxy {
    proxy_pass http://back_end/proxy;
    
}

proxy_pass用法常见误区 两种常见用法

带/与不带/用法区别

  • 不带/意味着Nginx不会修改用户URL,而是直接传给上游的应用服务器
  • 带/意味着Nginx会修改用户URL,修改方法: 将location后的URL从用户URL中删除
不带/
location /bbs/ {
    proxy_pass http://127.0.0.1:8080;
}
用户请求URL:  /bbs/abc/test.html
请求到达Nginx的URL: /bbs/abc/test.html
请求到达上游应用服务器的URL: /bbs/abc/test.html

带/
location /bbs/ {
    proxy_pass http://127.0.0.1:8080/;
}
用户请求URL:  /bbs/abc/test.html
请求到达Nginx的URL: /bbs/abc/test.html
请求到达上游应用服务器的URL: /abc/test.html