nginx配置文件结构
1、nginx配置文件结构
源码安装Nginx 配置文件:
默认安装目录:/usr/local/nginx
[root@jfedu nginx]# ls
conf html logs sbin
# nginx中默认目录解释
conf: 存放nginx 配置文件
html:存放nginx发布网页
logs:存放nginx 日志文件
sbin:存放nginx 启动文件
nginx 主配置文件:/usr/local/nginx/conf/nginx.conf
nginx 访问日志文件:/usr/local/nginx/logs/access.log
nginx 错误日志文件:/usr/local/nginx/logs/error.log
nginx 启动文件:/usr/local/nginx/sbin/nginx
nginx 主配置文件结构问题:
nginx.conf配置文件有指令控制的模块进行组成,指令分为简单指令和块指令。
一个简单指令有名称和参数组成,空格分隔,分号结尾。
块指令:
- 全局块:配置全局的参数
- events块:配置nginx 引擎,nginx与用户连接的相关参数
- http 块:提供http服务
- server块:配置虚拟主机,一个http 可以有多个server
- location 块:匹配url 之后做什么操作或者响应。
# 块指令:以“{” 开头, “}”结尾。
# nginx 参数进行配置的时候要分清楚块的内容,不能把一些小的参数配置到全局块里面,比如日志。
默认配置分析:
cat /usr/local/nginx/conf/nginx.conf
# user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#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;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
server {
listen 80;
server_name localhost;
charset koi8-r;
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;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ .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;
}
}
}
2、location优先级
= 字面精确匹配; ^~ 最大前缀匹配; 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。 nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。 / 不带任何前缀:最大前缀匹配; 通用匹配,任何请求都会匹配到。 ~ 区分大小写相关的正则匹配; ~* 不区分大小写无关的正则匹配;只会匹配后面的正则表达式大小写,和网页文件无关 !~和!~* 分别为区分大小写不匹配及不区分大小写不匹配 的正则
3、ECHO模块测试 nginx location规则
# 支持在配置文件中使用echo,sleep,time,exec 等命令 # 不加echo 模块,不能再配置文件使用上述命令 #下载 wget https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz #查看原来参数: [root@jfedu sbin]# ./nginx -V nginx version: nginx/1.14.0 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) configure arguments: #增加模块到原来的nginx中 [root@jfedu root]# ls v0.61.tar.gz tar xzf v0.61.tar.gz -C nginx-1.14.0 [root@jfedu nginx-1.14.0]# pwd /root/nginx-1.14.0 #预编译参数 [root@jfedu nginx-1.14.0]# ./configure --add-module=echo-nginx-module-0.61 #编译 [root@jfedu nginx-1.14.0]# make #平滑升级 [root@jfedu nginx-1.14.0]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old [root@jfedu nginx-1.14.0]# cp objs/nginx /usr/local/nginx/sbin/nginx [root@jfedu nginx-1.14.0]# make upgrade /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful kill -USR2cat /usr/local/nginx/logs/nginx.pid#向nginx 的pid 文件,发送一个信号,告诉pid 利用nginx 执行文件,生成一个新的nginx 进程 sleep 1 test -f /usr/local/nginx/logs/nginx.pid.oldbin kill -QUITcat /usr/local/nginx/logs/nginx.pid.oldbin#完美退出老的nginx 进程 #平滑升级就成功了 [root@jfedu nginx-1.14.0]# /usr/local/nginx/sbin/nginx -V nginx version: nginx/1.14.0 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) configure arguments: --add-module=echo-nginx-module-0.61
4、location测试配置
配置location 测试命令
# 配置了一个域名
server {
listen 80;
server_name www.jf1.com; 配置域名
# 本地域名生效
vim /etc/hosts
10.0.0.6 www.jf1.com
修改了配置文件,echo 命令测试 "/" "= /test.png" "/test.png" 两个优先级,谁更优先?
location / {
echo "jfedu0";
}
location /test.png {
echo "jfedu1";
}
location = /test.png {
echo "jfedu2";
}
# 测试:
[root@jfedu conf]# curl www.jf1.com # 匹配 "/"
jfedu0
[root@jfedu conf]# curl www.jf1.com/
jfedu0
[root@jfedu conf]# curl www.jf1.com/test.png # 精确匹配
jfedu2
[root@jfedu conf]# curl www.jf1.com/test # 匹配 "/",匹配字符没有匹配到,默认有/ 托底。如果没有echo 模块,这个访问url 就会报错 404 ,没有找到这个文件。
jfedu0
修改了配置文件,echo 命令测试 "^~ /test/" "^~ /test" 两个优先级,谁更优先?
location ^~ /test/ {
echo "jfedu3";
}
location ^~ /test {
echo "jfedu4";
}
测试
[root@jfedu conf]# curl www.jf1.com/test #最大前缀匹配test
jfedu4
[root@jfedu conf]# curl www.jf1.com/test666 #最大前缀匹配test666
jfedu4
[root@jfedu conf]# curl www.jf1.com/test666777
jfedu4
[root@jfedu conf]# curl www.jf1.com/test/ #匹配目录
jfedu3
[root@jfedu conf]# curl www.jf1.com/test123/ #最大前缀匹配test
jfedu4
[root@jfedu conf]# curl www.jf1.com/test/123 #匹配目录test里面的文件
jfedu3
# 匹配"^~ /test/" "^~ /test" ,"^~ /test/" 匹配目录使用,"^~ /test" 主要匹配文件,和文件名前缀匹配
修改了配置文件,echo 命令测试 "~" "~" 两个优先级,谁更优先?
location ~ /test {
echo "jfedu5";
}
location ~ /Test {
echo "jfedu6";
}
# 测试
[root@jfedu conf]# curl www.jf1.com/test #大小写相关
jfedu5
[root@jfedu conf]# curl www.jf1.com/tesT #大小写无关
jfedu6
# 如果 "~" "~*" 在配置文件中执行功能是相同的,那么匹配顺序会按照配置中配置的先后顺序进行匹配。
#总结优先级
"=" 高于 "^~" 高于 "~*" 高于 "~" 高于 "=/" 高于 "/"
#如果整条location 规则中有 = 号,肯定是精确匹配。
生产环境中常用的几条规则:
location =/ {
#直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理.
}
location ^~ /test/ {
#匹配目录
}
location ~* \.(jpg|jpeg|css|js)$ {
#匹配无关大小写以.jpg .jpeg .css .js 等结尾的文件
#. 转义点为.的原意,$ 代表以什么固定结尾
}
location ~ ".bak|.html" {
return 404;
#匹配相关大小写的文件,url 中出现.bak .html 字眼的URL直接拒绝
}
=/ 和 = /的区别是什么 ?
没有区别,带上等号,就代表是精确匹配了
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。






