Nginx location相关配置说明


  基于不同的IP、不同的端口以及不用得域名实现不同的虚拟主机,依赖于核心模块ngx_http_core_module实现。

新建PC web站点

[root@CentOS7 ~]#mkdir /apps/nginx/conf.d
[root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf
server {
  listen 80;
  server_name www.darius.com;
  location / {
    root /data/nginx/html/pc;
  }
}
[root@CentOS7 ~]#mkdir /data/nginx/html/pc -pv
mkdir: 已创建目录 "/data/nginx"
mkdir: 已创建目录 "/data/nginx/html"
mkdir: 已创建目录 "/data/nginx/html/pc"
[root@CentOS7 ~]#echo "pc web" >>/data/nginx/html/pc/index.html
[root@CentOS7 ~]#tail -2 /apps/nginx/conf/nginx.conf
include /apps/nginx/conf.d/*.conf;
}

加载配置文件

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

访问测试

测试机添加一条主机解析
[root@CentOS-Test ~]#cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.36.104 www.darius.com

访问测试
[root@CentOS-Test ~]#curl www.darius.com
pc web

root与alias

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

[root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf
server {
  listen 80;
  server_name www.darius.com;
  location / {
    root /data/nginx/html/pc;
  }
  location /about {
    root /data/nginx/html/pc;  # #必须要在pc目录中创建一个about目录才可以访问,否则报错。
    index index.html;
  }
}

[root@CentOS7 ~]#mkdir /data/nginx/html/pc/about
[root@CentOS7 ~]#echo "/data/nginx/html/pc/about" >>/data/nginx/html/pc/about/index.html

重启测试

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

访问

[root@CentOS-Test ~]#curl -L www.darius.com/about
/data/nginx/html/pc/about

alias:定义路径别名,会把访问的路径重新定义到其指定的路径

[root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf
server {
  listen 80;
  server_name www.darius.com;
  location / {
    root /data/nginx/html/pc;
  }

  location /about {  # 使用alias的时候uri后面如果加了斜杠则下面的路径配置必须加斜杠,否则403
    #root /data/nginx/html/pc;
    alias /data/nginx/html/pc;  # 当访问about的时候,会显示alias定义的/data/nginx/html/pc里面的内容。
    index index.html;
  }
}

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

访问测试

[root@CentOS-Test ~]#curl -L www.darius.com/about
pc web

location 的使用方法

  在没有使用正则表达式的时候,nginx会先在server中的多个location选取匹配度最高的一个uri,uri是用户请求的字符串,即域名后面的web文件路径,然后使用该location模块中的正则url和字符串,如果匹配成功就结束搜索,并使用此location处理此请求。

语法规则: location [=|~|~*|^~] /uri/ { … }

= #用于标准uri前,需要请求字串与uri精确匹配,如果匹配成功就停止向下匹配并立即处理请求。
~ #用于标准uri前,表示包含正则表达式并且区分大小写
~* #用于标准uri前,表示包含正则表达式并且不区分大写
!~ #用于标准uri前,表示包含正则表达式并且区分大小写不匹配
!~* #用于标准uri前,表示包含正则表达式并且不区分大小写不匹配
^~ #用于标准uri前,表示包含正则表达式并且匹配以什么开头
$ #用于标准uri前,表示包含正则表达式并且匹配以什么结尾
\ #用于标准uri前,表示包含正则表达式并且转义字符。可以转. * ?等
* #用于标准uri前,表示包含正则表达式并且代表任意长度的任意字符

location 精确匹配

[root@CentOS7 ~]#vim /apps/nginx/conf.d/pc.conf
[root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf
server {
  listen 80;
  server_name www.darius.com;
  location / {
    root /data/nginx/html/pc;
  }

  location = /1.jpg {
    root /data/nginx/images;
    index index.html;
  }
}
[root@CentOS7 ~]#mkdir /data/nginx/images
[root@CentOS7 ~]#rz -E
rz waiting to receive.
[root@CentOS7 ~]#mv 1.jpg /data/nginx/images/  # 上传1.jpg到/data/nginx/images
[root@CentOS7 ~]#/apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload  # 重启nginx

访问测试
Nginx location相关配置说明

location 区分大小写

[root@CentOS7 ~]#mv /data/nginx/images/1.jpg /data/nginx/images/Darius.jpg
[root@CentOS7 ~]#vim /apps/nginx/conf.d/pc.conf
[root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf
server {
  listen 80;
  server_name www.darius.com;
  location / {
    root /data/nginx/html/pc;
  }

  location ~ /D.*\.jpg {
    root /data/nginx/images;
    index index.html;
  }
}

[root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload

测试页
Nginx location相关配置说明

Nginx location相关配置说明

location 不区分大小写

  对用户请求的uri做模糊匹配,也就是uri中无论都是大写、都是小写或者大小写混合,此模式也都会匹配,通常使用此模式匹配用户request中的静态资源并继续做下一步操作。

[root@CentOS7 ~]#vim /apps/nginx/conf.d/pc.conf
[root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf
server {
  listen 80;
  server_name www.darius.com;
  location / {
    root /data/nginx/html/pc;
  }

  location ~* /D.*\.jpg {
    root /data/nginx/images;
    index index.html;
  }
}
[root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload
注:不区分大小写是对该目录文件中的规则不区分大小写,不是对此文件名称不区分大小写,在Linux中是严格区分文件大小写的。

location 文件名后缀

#上传一个和images目录不一样内容的的图片1.j到/data/nginx/images1
    location ~* \.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js)$ {
        root /data/nginx/images1;
        index index.html;
    }
重启Nginx并访问测试

匹配案例的优先级

匹配优先级:=, ^~, ~/~*,/
location优先级:(location =) > (location 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (/)

注:
    完整路径就是 /static  这样的完整的URL 。
    部分就是 使用location  ^~  /static 定义了开始,但是后面还有可能是 /statici-mage

生产中使用的案例

直接匹配网站根会加速Nginx访问处理:
location = / {
......;
} l
ocation / {
......;
} 静
态资源配置:
location ^~ /static/ {
......;
} #
或者
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
......;
} 多
应用配置
location ~* /app1 {
......;
} l
ocation ~* /app2 {
......;
}

Nginx 四层访问控制

  访问控制基于模块ngx_http_access_module实现,可以通过匹配客户端源IP地址进行限制。

server {
  listen 80;
  server_name www.darius.com;
  error_log logs/www_darius_com.log;
  location / {
    alias /data/nginx/html/pc;
    index index.html;
    deny 192.168.36.110;
    allow 192.168.36.0/24;
    deny all; #先允许小部分,再拒绝大部分
    }
}

[root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload

测试

[root@CentOS-Test ~]#curl www.darius.com   # 拒绝192.168.36.110主机
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.14.2</center>
</body>
</html>

[root@CentOS7 ~]#curl www.darius.com  # 允许192.168.36.0/24网段主机
pc web

Nginx账户认证功能

[root@CentOS7 ~]#yum install -y httpd-tools -y  
[root@CentOS7 ~]#htpasswd -cbm /apps/nginx/conf.d/.htpasswd user1 123456   # 生成认证用户
Adding password for user user1
[root@CentOS7 ~]#htpasswd -bm /apps/nginx/conf.d/.htpasswd user2 123456
Adding password for user user2
[root@CentOS7 ~]#tail /apps/nginx/conf.d/.htpasswd
user1:$apr1$CsaWdjbv$EwlG9UR6hEg/RYKhP0HS/1
user2:$apr1$5/nkjgHY$Sj.vqTB6M4wqapmm.jTu3.

修改配置文件

[root@CentOS7 ~]#vim /apps/nginx/conf.d/pc.conf
[root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf
server {
  listen 80;
  server_name www.darius.com;
  error_log logs/www_darius_com.log;
  location / {
    root /data/nginx/html/pc;
    index index.html;
    auth_basic  "login password";
    auth_basic_user_file /apps/nginx/conf.d/.htpasswd;
  }
}
[root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload  # 重启nginx服务

测试
Nginx location相关配置说明

自定义错误页面

[root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf
server {
  listen 80;
  server_name www.darius.com;
  error_page 500 502 503 504 404 /error.html;   # 默认目录下创建error.html页面
  error_log logs/www_darius_com_error.log;   # 错误日志
  access_log logs/www_darius_com_access.log;   # 访问日志
  location = /error.html {
    root html;
  }
  location / {
    root /data/nginx/html/pc;
    index index.html;
  }
}

[root@CentOS7 ~]#echo "ERROR" >/apps/nginx/html/error.html  # 创建error页面

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

测试

[root@CentOS-Test ~]#curl www.darius.com
pc web
[root@CentOS-Test ~]#curl www.darius.com/aa  # 访问不存在的页面,显示定义的错误页面
ERROR

检测文件是否存在

  try_files会按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件夹),如果所有文件或文件夹都找不到,会进行一个内部重定向到最后一个参数。只有最后一个参数可以引起一个内部重定向,之前的参数只设置内部URI的指向。最后一个参数是回退URI且必须存在,否则会出现内部500错误。

[root@CentOS7 ~]#vim /apps/nginx/conf.d/pc.conf
[root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf
server {
  listen 80;
  server_name www.darius.com;
  error_log logs/www_darius_com_error.log;
  access_log logs/www_darius_com_access.log;
  location / {
    root /data/nginx/html/pc;
    index index.html;
    try_files $uri $uri/index.html $uri.html =489;   # 重启nginx,当访问http://www.darius.com/about/xx.html等不存在的uri显示返回数据的状态码
  }
}
[root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload

访问测试

[root@CentOS-Test ~]#curl --head www.darius.com/about/xx.html
HTTP/1.1 489  # 489就是自定义的状态返回码
Server: nginx/1.14.2
Date: Thu, 30 May 2019 07:56:54 GMT
Content-Length: 0
Connection: keep-alive

当访问的uri不存在时返回default

[root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf
server {
  listen 80;
  server_name www.darius.com;
  error_log logs/www_darius_com_error.log;
  access_log logs/www_darius_com_access.log;
  location / {
    root /data/nginx/html/pc;
    index index.html;
    try_files $uri $uri/index.html $uri.html /about/default.html;   # 重启nginx并测试,当访问到http://www.darius.com/about/xx.html等不存在的uri会显示default
  }
}

[root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload

[root@CentOS7 ~]#echo "default" >> /data/nginx/html/pc/about/default.html

访问测试

[root@CentOS-Test ~]#curl www.darius.com/about/xx.html
default

长连接配置

  keepalive_timeout number; #设定保持连接超时时长,0表示禁止长连接,默认为75s,通常配置在http字段作为站点全局配置 keepalive_requests number; #在一次长连接上所允许请求的资源的最大数量,默认为100次

keepalive_requests 3;
keepalive_timeout 65 60;
开启长连接后,返回客户端的会话保持时间为60s,单次长连接累计请求达到指定次数请求或65秒就会被断开,后面的60为发送给客户端应答报文头部中显示的超时时间设置为60s:如不设置客户端将不显示超时时间。

Keep-Alive:timeout=60 #浏览器收到的服务器返回的报文

如果设置为0表示关闭会话保持功能,将如下显示:
Connection:close #浏览器收到的服务器返回的报文

Nginx作为下载服务器配置

location /download {
    autoindex on;  # 自动索引功能
    autoindex_exact_size on;   # 计算文件确切大小(单位bytes),off只显示大概大小(单位kb、mb、gb)
    autoindex_localtime on;  # 显示本机时间而非GMT(格林威治)时间
    limit_rate 10k;  # 限制响应给客户端的传输速率,单位是bytes/second,默认值0表示无限制限速与不限速的对比
    root /data/nginx/html/pc;
  }

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

创建测试页面进行测试

将光盘镜像挂载到此目录下进行测试
[root@CentOS7 ~]#mount /dev/sr0 /data/nginx/html/pc/download/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@CentOS7 ~]#ll /data/nginx/html/pc/download/
总用量 1656
-rw-rw-r-- 1 root root      14 11月 26 2018 CentOS_BuildTag
drwxr-xr-x 3 root root    2048 11月 26 2018 EFI
-rw-rw-r-- 1 root root     227 8月  30 2017 EULA
-rw-rw-r-- 1 root root   18009 12月 10 2015 GPL
drwxr-xr-x 3 root root    2048 11月 26 2018 images
drwxr-xr-x 2 root root    2048 11月 26 2018 isolinux
drwxr-xr-x 2 root root    2048 11月 26 2018 LiveOS
drwxrwxr-x 2 root root 1656832 11月 25 2018 Packages
drwxrwxr-x 2 root root    4096 11月 26 2018 repodata
-rw-rw-r-- 1 root root    1690 12月 10 2015 RPM-GPG-KEY-CentOS-7
-rw-rw-r-- 1 root root    1690 12月 10 2015 RPM-GPG-KEY-CentOS-Testing-7
-r--r--r-- 1 root root    2883 11月 26 2018 TRANS.TBL

测试页
Nginx location相关配置说明

Nginx作为上传服务器

client_max_body_size 1m; #设置允许客户端上传单个文件的最大值,默认值为1m
client_body_buffer_size size; #用于接收每个客户端请求报文的body部分的缓冲区大小;默认16k;
超出此大小时,其将被暂存到磁盘上的由下面client_body_temp_path指令所定义的位置
client_body_temp_path path [level1 [level2 [level3]]];
#设定存储客户端请求报文的body部分的临时存储路径及子目录结构和数量,目录名为16进制的数字,使用hash
之后的值从后往前截取1位、2位、2位作为文件名:
[root@s3 ~]# md5sum /data/nginx/html/pc/index.html
95f6f65f498c74938064851b1bb 96 3d 4 /data/nginx/html/pc/index.html
1级目录占1位16进制,即2^4=16个目录 0-f
2级目录占2位16进制,即2^8=256个目录 00-ff
3级目录占2位16进制,即2^8=256个目录 00-ff

配置示例:
    client_max_body_size 10m;
    client_body_buffer_size 16k;
    client_body_temp_path /apps/nginx/temp 1 2 2; #reload Nginx会自动创建temp目录

其他配置

keepalive_disable none | browser ...;  # 对哪种浏览器禁用长连接

limit_except method ... { ... }  # 限制客户端使用除了指定的请求方法之外的其它方法,仅用于location
    method:GET, HEAD, POST, PUT, DELETE,MKCOL, COPY, MOVE, OPTIONS, PROPFIND,PROPPATCH, LOCK, UNLOCK, PATCH
    示例:
        location /upload {
            root /data/magedu/pc;
            index index.html;
            limit_except GET {  # 除了GET之外的其他请求方法,仅允许192.168.36.110主机使用
            allow 192.168.36.110;
            deny all;
            }
        }
aio on | off #是否启用asynchronous file I/O(AIO)功能,需要编译开启
linux 2.6以上内核提供以下几个系统调用来支持aio:
1、SYS_io_setup:建立aio 的context
2、SYS_io_submit: 提交I/O操作请求
3、SYS_io_getevents:获取已完成的I/O事件
4、SYS_io_cancel:取消I/O操作请求
5、SYS_io_destroy:毁销aio的context
directio size | off; #操作完全和aio相反,aio是读取文件而directio是写文件到磁盘,启用直接I/O,默认为关闭,当文件大于等于给定大小时,例如directio 4m,同步(直接)写磁盘,而非写缓存。
open_file_cache off; #是否缓存打开过的文件信息
open_file_cache max=N [inactive=time];
    nginx可以缓存以下三种信息:
    (1) 文件元数据:文件的描述符、文件大小和最近一次的修改时间
    (2) 打开的目录结构
    (3) 没有找到的或者没有权限访问的文件的相关信息
    max=N:可缓存的缓存项上限数量;达到上限后会使用LRU(Least recently used,最近最少使用)算法实现管理
    inactive=time:缓存项的非活动时长,在此处指定的时长内未被命中的或命中的次数少于
open_file_cache_min_uses指令所指定的次数的缓存项即为非活动项,将被删除
open_file_cache_errors on | off;
    是否缓存查找时发生错误的文件一类的信息
    默认值为off
open_file_cache_min_uses number;
    open_file_cache指令的inactive参数指定的时长内,至少被命中此处指定的次数方可被归类为活动项
    默认值为1
open_file_cache_valid time;
    缓存项有效性的检查验证频率,默认值为60s

open_file_cache max=10000 inactive=60s;  # 最大缓存10000个文件,非活动数据超时时长60s
open_file_cache_valid 60s;  # 每间隔60s检查一下缓存数据有效性
open_file_cache_min_uses 5;  # 60秒内至少被命中访问5次才被标记为活动数据
open_file_cache_errors on;  # 缓存错误信息
server_tokens off;  # 隐藏Nginx server版本

[root@CentOS7 ~]#grep "server_tokens" /apps/nginx/conf/nginx.conf
    server_tokens off;
[root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload

访问测试
[root@CentOS-Test ~]#curl --head www.darius.com
HTTP/1.1 200 OK
Server: nginx       # 版本号已隐藏
Date: Thu, 30 May 2019 08:27:49 GMT
Content-Type: text/html
Content-Length: 7
Last-Modified: Thu, 30 May 2019 03:06:03 GMT
Connection: keep-alive
ETag: "5cef489b-7"
Accept-Ranges: bytes

猜你喜欢

转载自blog.51cto.com/12980155/2402781
今日推荐