Nginx核心模块

一、配置文件说明

1.1官方说明

nginx官方帮助文档:http://nginx.org/en/docs/
tengine 帮助文档:http://tengine.taobao.org/nginx_docs/cn/docs/

1.2nginx的配置文件的组成部分

  • 主配置文件:nginx.conf
  • 子配置文件:include conf.d/*.conf
  • fastcgi、uwsgi、scgi等相关的配置文件
  • mime.types:支持的mime类型,MIME(Multipurpose Internet Mail Extensions)多用途互联网邮 件扩展类型,MIME消息能包含文本、图像、音频、视频以及其他应用程序专用的数据,是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器会自动 使用指定应用程序来打开。多用于指定一些客户端自定义的文件名,以及一些媒体文件打开方式。
  • MIME参考文档:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_HTTP/MIME_ Types

nginx配置文件格式说明

配置文件由指令与指令块构成
每条指令以;分号结尾,指令与值之间以空格符号分隔
可以将多条指令放在同一行,用分号分隔即可,但可读性差,不推荐
指令块以{ }大括号将多条指令组织在一起,且可以嵌套指令块
include语句允许组合多个配置文件以提升可维护性
使用#符号添加注释,提高可读性
使用$符号使用变量
部分指令的参数支持正则表达式

注意
(1) 指令必须以分号结尾
(2) 支持使用配置变量
 内建变量:由Nginx模块引入,可直接引用
 自定义变量:由用户使用set命令定义,格式: set variable_name value;
 引用变量:$variable_name

1.3主配置文件结构

main block:主配置段,即全局配置段,对http,mail都有效
#事件驱动相关的配置
event {
  ...
}   
#http/https 协议相关配置段
http {
  ...
}          
#默认配置文件不包括下面两个块
#mail 协议相关配置段
mail {
  ...
}    
#stream 服务器相关配置段
stream {
  ...
}

默认的nginx.conf篇日志文件说明

#全局配置端,对全局生效,主要设置nginx的启动用户/组,启动的工作进程数量,工作模式,Nginx的PID路径,日志路径等。
user nginx nginx;
worker_processes  auto;   #启动工作进程数数量
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events { #events设置快,主要影响nginx服务器与用户的网络连接,比如是否允许同时接受多个网络连接,使用哪种事件驱动模型处理请求,每个工作进程可以同时支持的最大连接数,是否开启对多工作进程下的网络连接进行序列化等。
     worker_connections  1024;   #设置单个nginx工作进程可以接受的最大并发,作为web服务器的时候最大并发数为worker_connections * worker_processes,作为反向代理的时候为
(worker_connections * worker_processes)/2
}
http { #http块是Nginx服务器配置中的重要部分,缓存、代理和日志格式定义等绝大多数功能和第三方模块都可以在这设置,http块可以包含多个server块,而一个server块中又可以包含多个location块,server块可以配置文件引入、MIME-Type定义、日志自定义、是否启用sendfile、连接超时时间和单个链接的请求上限等。
   include       mime.types;
   default_type application/octet-stream;
   sendfile       on; #作为web服务器的时候打开sendfile加快静态文件传输,指定是否使用sendfile系统调用来传输文件,sendfile系统调用在两个文件描述符之间直接传递数据(完全在内核中操作),从而避免了数据在内核缓冲区和用户缓冲区之间的拷贝,操作效率很高,被称之为零拷贝,硬盘 >> kernel buffer (快速拷贝到kernelsocket buffer) >>协议栈。
   keepalive_timeout  65;  #长连接超时时间,单位是秒
   server { #设置一个虚拟机主机,可以包含自己的全局快,同时也可以包含多个location模块。比如本虚拟机监听的端口、本虚拟机的名称和IP配置,多个server 可以使用一个端口,比如都使用80端口提供web服务、
       listen       80;  #配置server监听的端口
       server_name localhost; #本server的名称,当访问此名称的时候nginx会调用当前serevr内部的配置进程匹配。
       location / { #location其实是server的一个指令,为nginx服务器提供比较多而且灵活的指令,都是在location中体现的,主要是基于nginx接受到的请求字符串,对用户请求的UIL进行匹配,并对特定的指令进行处理,包括地址重定向、数据缓存和应答控制等功能都是在这部分实现,另外很多第三方模块的配置也是在location模块中配置。
           root   html; #相当于默认页面的目录名称,默认是安装目录的相对路径,可以使用绝对路径配置。
           index index.html index.htm; #默认的页面文件名称
       }
       error_page   500 502 503 504 /50x.html; #错误页面的文件名称
       location = /50x.html { #location处理对应的不同错误码的页面定义到/50x.html,这个跟对应其server中定义的目录下。
           root   html;  #定义默认页面所在的目录
       }
   }
    
#和邮件相关的配置
#mail {
#               ...
#       }         mail 协议相关配置段
#tcp代理配置,1.9版本以上支持
#stream {
#               ...
#       }       stream 服务器相关配置段
#导入其他路径的配置文件
#include /apps/nginx/conf.d/*.conf
}

二、全局配置

Main 全局配置段常见的配置指令分类

  • 正常运行必备的配置
  • 优化性能相关配置
  • 用于调试及定位问题相关的配置
  • 事件驱动相关配置

全局配置说明

user nginx nginx;#启动Nginx工作进程的用户和组
worker_processes [number |auto];#启动Nginx工作进程的数量,一般设为和CPU核心数相同
worker_cpu_affinity  00000001 00000010 00000100 00001000 | auto ;#将Nginx工作进程绑定到指定的CPU核心,默认Nginx是不进行进程绑定的,绑定并不是意味着当前nginx进程独占以一核心CPU,但是可以保证此进程不会运行在其他核心上,这就极大减少了nginx的工作进程在不同的cpu核心上的来回跳转,减少了CPU对进程的资源分配与回收以及内存管理等,因此可以有效的提升nginx服务器的性能。


# 示例,我有两核cpu,使用01,四核使用0001
worker_processes  auto;
worker_cpu_affinity 01 10;

[root@centos71 conf]# ps axo pid,cmd,psr |grep nginx
 41168 nginx: master process /app/   0
 48623 nginx: worker process         0
 48624 nginx: worker process         1
 48626 grep --color=auto nginx       0

# auto自动绑定cpu

worker_processes auto; 
worker_cpu_affinity auto;

#错误日志记录配置,语法:error_log file [debug | info | notice | warn | error | crit | alert | emerg]
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log /apps/nginx/logs/error.log error;

#pid文件保存路径
pid       /apps/nginx/logs/nginx.pid;

worker_priority 0; #工作进程优先级,-20~20(19)
worker_rlimit_nofile 1024; #所有worker进程能打开的文件数量上限,包括:Nginx的所有连接(例如与代理服务器的连接等),而不仅仅是与客户端的连接,另一个考虑因素是实际的并发连接数不能超过系统级别的最大打开文件数的限制.最好与ulimit -n 或者limits.conf的值保持一致,默认不限制

daemon off;  #前台运行Nginx服务用于测试、或者以容器运行时,需要设为off
master_process off|on; #是否开启Nginx的master-worker工作模式,仅用于开发调试场景,默认为on


events {
   worker_connections  1024;  #设置单个工作进程的最大并发连接数,默认512,生产建议根据性能修改更大的值
   use epoll; #使用epoll事件驱动,Nginx支持众多的事件驱动,比如:select、poll、epoll,只能设置在events模块中设置。
   accept_mutex on; #mutex互斥为on表示同一时刻一个请求轮流由worker进程处理,而防止被同时唤醒所有worker,避免多个睡眠进程被唤醒的设置,可以避免多个 worker 进程竞争同一连接而导致性能下降,也可以提高系统的稳定性,默认为off,新请求会唤醒所有worker进程,此过程也称为"惊群",在高并发的场景下多个worker进程可以各自同时接受多个新的连接请求,如果是多CPU和worker进程绑定,就可以提高吞吐量
   multi_accept on; #on时Nginx服务器的每个工作进程可以同时接受多个新的网络连接,此指令默认为off,即默认为一个工作进程只能一次接受一个新的网络连接,打开后几个同时接受多个。建议设置为on
  }

示例:实现nginx高并发

ulimit::显示(或设置)用户可以使用的资源的限制(limit),这限制分为软限制(当前限制)和硬限制(上限),其中硬限制是软限制的上限值,应用程序在运行过程中使用的系统资源不超过相应的软限制,任何的超越都导致进程的终止。

# 零时修改当前shell的
最好通过使用 ulimit -n xx 修改每个进程可打开的文件数,缺省值是 1024。
ulimit -n 4096 将每个进程可以打开的文件数目加大到4096,缺省为1024
其他建议设置成无限制(unlimited)的一些重要设置是:
数据段长度:ulimit -d unlimited
最大内存大小:ulimit -m unlimited
堆栈大小:ulimit -s unlimited
CPU 时间:ulimit -t unlimited
虚拟内存:ulimit -v unlimited
 暂时地,适用于通过 ulimit 命令登录 shell 会话期间。
[root@centos71 conf]# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 7193
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 102400
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 7193
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

输出的每一行由资源名字、(单位,ulimit命令的参数)、软限制组成。详细解释:
参数 描述
core file size core文件的最大值为100 blocks,
data seg size 进程的数据段可以任意大
file size 文件可以任意大
pending signals 最多有2047个待处理的信号
max locked memory 一个任务锁住的物理内存的最大值为32kB
max memory size 一个任务的常驻物理内存的最大值
open files 一个任务最多可以同时打开1024的文件
pipe size 管道的最大空间为4096字节
POSIX message queues POSIX的消息队列的最大值为819200字节
stack size 进程的栈的最大值为8192字节
cpu time 进程使用的CPU时间
max user processes 当前用户同时打开的进程(包括线程)的最大个数为2047
virtual memory 没有限制进程的最大地址空间
file locks 所能锁住的文件的最大个数没有限制
# 在配置文件中修改
1)、解除 Linux 系统的最大进程数和最大文件打开数限制:
        vi /etc/security/limits.conf
        # 添加如下的行
        * soft noproc 11000
        * hard noproc 11000
        * soft nofile 4100
        * hard nofile 4100
       说明:* 代表针对所有用户,noproc 是代表最大进程数,nofile 是代表最大文件打开数

[root@centos71 conf]# ulimit -n
1024
[root@centos71 conf]# ulimit -n 102400
[root@centos71 conf]# ulimit -n
102400

#默认nginx配置不支持高并发,会出现以下错误日志
#如果systemd启动,则需要修改nginx.service文件中加LimitNOFILE=100000,才能有效

[Service]
Type=forking
PIDFile=/app/nginx/logs/nginx.pid
ExecStartPre=/bin/rm -f /app/nginx/logs/nginx.pid
ExecStartPre=/app/nginx/sbin/nginx -t
ExecStart=/app/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
#
LimitNOFILE=100000

[Install]
WantedBy=multi-user.target

#如果非systemd启动,可以修改下面pam限制
[root@centos71 conf]# vim /etc/security/limits.conf
*               soft   nofile          1000000
*               hard   nofile          1000000

三、Http配置快

3.1Http协议相关的配置结构

http {
   ...
   ...  #各server的公共配置
   server {    #每个server用于定义一个虚拟主机,第一个server为默认虚拟服务器
     ...
   }
   server {     
     ...
     server_name   #虚拟主机名
     root     #主目录
     alias     #路径别名
     location [OPERATOR] URL {     #指定URL的特性
         ...
         if CONDITION {
         ...
         }
       }
   }
}

http协议相关说明

http {
   include       mime.types; #导入支持的文件类型,是相对于/apps/nginx/conf的目录
   default_type application/octet-stream; #除mime.types中文件类型外,设置其它文件默认类型,访问其它类型时会提示下载不匹配的类型文件
#日志配置部分
    #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    #                 '$status $body_bytes_sent "$http_referer" '
    #                 '"$http_user_agent" "$http_x_forwarded_for"';
    #access_log logs/access.log main;
    #自定义优化参数
   sendfile       on; 
    #tcp_nopush     on; 
    #开启sendfile的情况下,合并请求后统一发送给客户端,必须开启sendfile
    #tcp_nodelay   off; 
    #开启keepalived模式下的连接是否启用TCP_NODELAY选项,为off时,延迟0.2s发送,默认On时,不延迟发送,立即发送用户响应报文。
    #keepalive_timeout 0;
   keepalive_timeout  65 65; 
    #设置会话保持时间,第二个值为响应首部:keep——Alived:timeout=65,可以和第一个值不同
    #gzip on; #开启文件压缩
   server {
       listen       80 default_server; #设置监听地址和端口,多个虚拟机时当前是否是默认的虚拟主机,default_server表示是默认主机,否则排在前面server为默认主机
        
       server_name localhost; #设置server name,可以以空格隔开写多个并支持正则表达式,如:*.wang.com www.wang.* ~^www\d+\.wang\.com$ 示例: .wang.org 相当于*.wang.org和wang.org
        #charset koi8-r; #设置编码格式,默认是俄语格式,建议改为utf-8
        #access_log logs/host.access.log main;
       location / {
           root   html;
           index index.html index.htm;
       }
        #error_page 404             /404.html;
        # redirect server error pages to the static page /50x.html
        #
       error_page   500 502 503 504 /50x.html; #定义错误页面
       location = /50x.html {
           root   html;
       }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ { #以http的方式转发php请求到指定web服务器
        #   proxy_pass   http://127.0.0.1;
        #}
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ { #以fastcgi的方式转发php请求到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;
        #}
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht { #拒绝web形式访问指定文件,如很多的网站都是通过.htaccess文件来改变自己的重定向等功能。
        #   deny all;
        #}
       location ~ /passwd.html {
           deny all;
             }
   }
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server { #自定义虚拟server
    #   listen       8000;
    #   listen       somename:8080;
    #   server_name somename alias another.alias;
    #   location / { 
    #       root   html;
    #       index index.html index.htm; #指定默认网页文件,此指令由ngx_http_index_module模块提供
    #   }
    #}
    # HTTPS server
    #
    #server { #https服务器配置
    #   listen       443 ssl;
    #   server_name localhost;
    #   ssl_certificate     cert.pem;
    #   ssl_certificate_key cert.key;
    #   ssl_session_cache   shared:SSL:1m;
    #   ssl_session_timeout 5m;
    #   ssl_ciphers HIGH:!aNULL:!MD5;
    #   ssl_prefer_server_ciphers on;
    #   location / {
    #       root   html;
    #       index index.html index.htm;
    #   }
    #}

3.2MIME

#在响应报文中将指定的文件扩展名映射至MIME对应的类型
include           /etc/nginx/mime.types;
default_type     application/octet-stream;#除mime.types中的类型外,指定其它文件的默认
MIME类型,浏览器一般会提示下载
types {
   text/html html;
   image/gif gif;
   image/jpeg jpg;
}
#MIME参考文档:
https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_HTTP/MIME_Types

3.3指定字符集

指定utf-8,防止网页显示乱码

# 是否在响应报文中的Content-Type显示指定的字符集,默认off显示
charset charset |off;

# 示例
charset utf-8;

四、常见核心模块配置

基于不同的IP、不同的端口以及不用得域名实现不同的虚拟主机,依赖于核心模块ngx_http_core_module实现。
https://nginx.org/en/docs/http/ngx_http_core_module.html

4.1新建PC web站点


[root@centos71 ~]# mkdir /app/nginx/conf/conf.d

[root@centos71 ~]# cat /app/nginx/conf/nginx.conf |grep -Ev '#|^$'
worker_processes  auto;
worker_cpu_affinity 01 10;
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;
        }
    }
  # 增加这一行
   # include conf.d/*.conf;
}

[root@centos71 ~]# vim /app/nginx/conf/pc.conf
[root@centos71 ~]# cat /app/nginx/conf/pc.conf
server {
    listen 80;
    server_name www.gw.com;
    root /data/nginx/html/pc;
    index index.html;
}

[root@centos71 ~]# echo "this is pc" > /data/nginx/html/pc/index.html
[root@centos71 ~]# cat /data/nginx/html/pc/index.html
this is pc
    # 重新加载文件
[root@centos71 ~]# nginx -s reload
# 另一台主机配置域名解析
[root@centos72 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.146  www.gw.com

# 访问
[root@centos72 ~]# curl www.gw.com
this is pc

4.2 建立mobile.conf

[root@centos71 ~]# vim /app/nginx/conf/conf.d/mobile.conf
[root@centos71 ~]# cat /app/nginx/conf/conf.d/mobile.conf
server {
    listen 80;
    server_name m.gw.com;
    root /data/nginx/html/mobile;
}

[root@centos71 ~]#  mkdir -p /data/nginx/html/mobile
[root@centos71 ~]# echo "this is mobile" > /data/nginx/html/mobile/index.html

[root@centos71 ~]# nginx -t
nginx: the configuration file /app/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /app/nginx/conf/nginx.conf test is successful
[root@centos71 ~]# nginx -s reload

# 配置另外一台机器的域名解析
[root@centos72 ~]# vim /etc/hosts
[root@centos72 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.146  www.gw.com m.gw.com

# 访问
[root@centos72 ~]# curl m.gw.com
this is mobile

4.3root与alias

root:指定web的家目录,在定义location的时候,文件的绝对路径等于 root+location

# root 在location外
[root@centos71 ~]# cat  /app/nginx/conf/conf.d/pc.conf 
server {
 listen 80 default_server;
 server_name www.wang.org;
 root /data/nginx/html/pc;
 location /about {
    index index.html;
    }
 }
[root@centos71 ~]# mkdir  /data/nginx/html/pc/about
[root@centos71 ~]# echo 'about' >  /data/nginx/html/pc/about/index.html

# 访问
[root@centos72 ~]# curl   www.gw.com/about/
about
# root 在location内
[root@centos71 ~]# cat /app/nginx/conf/conf.d/pc.conf 
server {
 listen 80 default_server;
 server_name www.wang.org;
 location /about {
    root /opt/html;
    index index.html;
    }
 }

[root@centos71 ~]# mkdir /opt/html
[root@centos71 ~]# mkdir /opt/html/about
[root@centos71 ~]# vim /opt/html/about/index.html
[root@centos71 ~]# nginx -s reload

#访问
[root@centos72 ~]# curl   www.gw.com/about/
about

alias:定义路径别名,会把访问的路径重新定义到其指定的路径,文档映射的另一种机制;仅能用于 location上下文,此指令使用较少

[root@centos71 ~]# mkdir /opt/html/test
[root@centos71 ~]# vim /opt/html/test/index.html
[root@centos71 ~]# cat /app/nginx/conf/conf.d/pc.conf 
server {
 listen 80 default_server;
 server_name www.wang.org;
 location /about {
    root /opt/html;
    index index.html;
    }
 location /test {
    alias /opt/html/test;
    index index.html;
    }
}
[root@centos71 ~]# nginx -s reload

# 访问
[root@centos72 ~]# curl   www.gw.com/test/
test

注意:location中使用root指令和alias指令的意义不同

root 给定的路径对应于location中的/uri 左侧的/
alias 给定的路径对应于location中的/uri 的完整路径

4.4 location指令

在一个server中location配置段可存在多个,用于实现从uri到文件系统的路径映射;ngnix会根据用户请求的URI来检查定义的所有location,按一定的优先级找出一个最佳匹配,而后应用其配置
在没有使用正则表达式的时候,nginx会先在server中的多个location选取匹配度最高的一个uri,uri是 用户请求的字符串,即域名后面的web文件路径,然后使用该location模块中的正则url和字符串,如果 匹配成功就结束搜索,并使用此location处理此请求。
http://nginx.org/en/docs/http/ngx_http_core_module.html#location

Syntax:	location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default:	—
Context:	server, location


location = / {
    [ configuration A ]
}

location / {
    [ configuration B ]
}

location /documents/ {
    [ configuration C ]
}

location ^~ /images/ {
    [ configuration D ]
}

location ~* \.(gif|jpg|jpeg)$ {
    [ configuration E ]
}

location 语法规则

#语法规则:
location [ = | ~ | ~* | ^~ ] uri { ... }
=   #用于标准uri前,需要请求字串与uri精确匹配,大小敏感,如果匹配成功就停止向下匹配并立即处理请求

^~  #用于标准uri前,表示包含正则表达式,并且匹配以指定的正则表达式开头,对uri的最左边部分做匹配检查,不区分字符大小写

~   #用于标准uri前,表示包含正则表达式,并且区分大小写

~*  #用于标准uri前,表示包含正则表达式,并且不区分大写

不带符号 #匹配起始于此uri的所有的 uri

\   #用于标准uri前,表示包含正则表达式并且转义字符。可以将 . * ?等转义为普通符号
#匹配优先级从高到低:
=, ^~, ~/~*, 不带符号
server {
 listen 80;
 server_name location.gw.org;
 location = / {
 default_type text/html;
 return 200 'location = /';
 }
 location / {
 default_type text/html;
 return 200 'location /';
 }
 location /documents/ {
 default_type text/html;
 return 200 'location /documents/';
 }
 location ^~ /images/ {
 default_type text/html;
 return 200 'location ^~ /images/';
 }
 location ~* \.(gif|jpg|jpeg)$ {
 default_type text/html;
 return 200 'location ~* \.(gif|jpg|jpeg)';
 }
}
#测试结果如下,建议是curl测试
#1.请求 http://location.gw.org/ 会被 location =/ 匹配
#2.请求 http://location.gw.org/index.html 会被 location / 匹配
#3.请求 http://location.gw.org/documents/1.html 会被 location /documents/ 匹配
#4.请求 http://location.gw.org/images/1.gif 会被 location ^~ /images/ 匹配
#5.请求 http://location.gw.org/documents/1.jpg 会被 location ~* \.(gif|jpg|jpeg)$匹配
4.4.1匹配案例精确匹配

在server部分使用location配置一个web界面,例如:当访问nginx 服务器的/logo.jpg的时候要显示指定html文件的内容
精确匹配一般用于匹配组织的logo等相对固定的URL,匹配优先级最高

[root@centos71 conf.d]# cat test1.conf
server {
    listen 80;
    server_name www.gw.com;
    location = /logo.jpg {
        root /data/nginx/images;
        index index.html;
    }
}
[root@centos71 conf.d]# nginx -s reload
#上传logo.jpg图片到/data/nginx/images,重启Nginx并访问测试
#访问测试:http://www.wang.org/logo.jpg
4.4.2区分大小写

**~ **实现区分大小写的模糊匹配. 以下范例中, 如果访问uri中包含大写字母的JPG,则以下location匹Ax.jpg条件不成功,因为 ~ 区分大小写,当用户的请求被执行匹配时发现location中定义的是小写的jpg,本次访问的uri匹配失败,后续要么继续往下匹配其他的location(如果有),要么报错给客户端

[root@centos71 conf.d]# cat test2.conf 
server {
    listen 80;
    server_name www.gw.com;
    location ~ /A.?\.jpg {
        root /data/nginx/images;
        index index.html;
    }
}
#重启Nginx并访问测试
#将只能访问以小写字符的jpg图片,不能识别大写的JPG结尾的图片
4.4.3不区分大小写

*~ **用来对用户请求的uri做模糊匹配,uri中无论都是大写、都是小写或者大小写混合,此模式也都会匹
配,通常使用此模式匹配用户request中的静态资源并继续做下一步操作,此方式使用较多
注意: 此方式中,对于Linux文件系统上的文件仍然是区分大小写的,如果磁盘文件不存在,仍会提示404


[root@centos71 conf.d]# vim test3.conf 
[root@centos71 conf.d]# cat test3.conf 
server {
    listen 80;
    server_name www.gw.com;
    location ~* /A.?\.jpg {
        root /data/nginx/images;
        index index.html;
    }
}
#重启Nginx并访问测试对于不区分大小写的location,则可以访问任意大小写结尾的图片文件,如区分大小写则只能访问Aa.jpg此类文件,不区分大小写则可以访问除了aa.jpg以外,还有其它的资源比如Aa.JPG、aA.jPG这样的混合名称文件,但是还同时也要求nginx服务器的资源目录有相应的文件,比如:必须有Aa.JPG,aA.jPG这样文件存在。
4.4.4url开始

[root@centos71 conf.d]# cat test4.conf 
server {
    listen 80;
    server_name www.gw.com;
    location ^~ /images {
        root /data/nginx;
        index index.html;
    }
    location /api {
        alias /data/nginx/api;
        index index.html;
    }
}

4.4.5文件名后缀
[root@centos71 conf.d]# cat test5.conf 
server {
    listen 80;
    server_name www.gw.com;
    location ~* \.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js|css)$ {
       root /data/nginx/static;
       index index.html;
 }
}
4.4.6优先级
[root@centos71 conf.d]# cat test6.conf 
server {
    listen 80;
    server_name www.gw.com;
    location = /1.jpg {
       root /data/nginx/static1;
       index index.html;
    }
    location /1.jpg {
        root /data/nginx/static2;
        index index.html;
    }
    location ~* \.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js)$ {
        root /data/nginx/static3;
        index index.html;
    }
}
#匹配优先级:=, ^~, ~/~*,/
location优先级:(location =) > (location ^~ 路径) > (location ~,~* 正则顺序) > 
(location 完整路径) > (location 部分起始路径) > (/)
4.4.7生产使用案例
#直接匹配网站根会加速Nginx访问处理
location = /index.html {
   ......;
}
location / {
   ......;
}
#静态资源配置方法1
location ^~ /static/ {
   ......;
}
#静态资源配置方法2,应用较多
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
   ......;
}
#多应用配置
location ~* /app1 {
     ......;
}
location ~* /app2 {
     ......;
}
4.4.8location@重定向

location @name 这样的location不用于常规请求处理,而是用于请求重定向

server {
   listen 80;
   server_name www.wang.org;
   root /data/www;
   location / {
     index index.html;
   }
   #如果出现异常,则重新定向到@error_404这个location上
   error_page 404 @error_404;
   location @error_404 {
     default_type text/html;
     charset utf8;
     return 200 '你访问的页面可能走丢了!';
   }
}
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值