浅谈Nginx模块ngx_http_core_module(一)
1️⃣ Http配置结构
- http配置,可以写在
/etc/nginx/nginx.conf
的http配置字段里。也可以单独来写, - 一般是单独写在
/etc/nginx/conf.d/*.conf
文件里,不同的虚拟主机写在不同的配置文件里,清晰明了。 - 在http的配置文件里,和主配置一样,分为{}括起来的,我们称之为段,比如
http{}
、server{}
、location {}
、if CONDITION {}
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; #在开启了sendfile的情况下,合并请求后统⼀发送给客⼾端。
#tcp_nodelay off; #在开启了keepalived模式下的连接是否启⽤TCP_NODELAY选项,当为off时,延
迟0.2s发送,默认On时,不延迟发送,⽴即发送⽤⼾相应报⽂。
#keepalive_timeout 0;
keepalive_timeout 65 65; #设置会话保持时间
#gzip on; #开启⽂件压缩
server {
listen 80; #设置监听地址和端⼝
server_name localhost; #设置server name,可以以空格隔开写多个并⽀持正则表达式,如
*.magedu.com www.magedu.* www.(site\d+)\.magedu\.com$ default_server
#charset koi8-r; #设置编码格式,默认是俄语格式,可以改为utf-8
#access_log logs/host.access.log main;
location / {
root html; # http服务器的根目录相对于系统的路径
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 { # 这里又缩进了一层location,表示50x的error_page实际指向是http root目录的50x.html,根据上文root路径,可得出是系统下/usr/share/nginx/html/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会话缓存模式为共享,时间为1分钟
# ssl_session_timeout 5m; # SSL会话超时时间为5分钟
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
location /linux38/passwd.ht {
deny all;
}
-
http各种配置段的的详细配置选项信息见:
http://nginx.org/en/docs/http/ngx_http_core_module.html -
与http相关的配置指令仅能够放置于
http,server,location,upstream,if CONDITION
段里。而且每个指令都有对应的可以放的段,并不是所有段都可以放。
2️⃣ ngx_http_core_module详解
🅿 1. server 设置虚拟机服务器全局配置
- 配置一个虚拟主机的全局配置
server { listen address[:PORT]|PORT; server_name SERVER_NAME; root /PATH/TO/DOCUMENT_ROOT;
🅿 2.listen设置监听端口和IP
default_server
:设定为默认虚拟主机ssl
:限制仅能够通过ssl连接提供服务backlog=NUMBER
设置调用中限制挂起连接队列的最大长度的backlog参数 listen()。默认情况下, backlog在FreeBSD,DragonFly BSD和macOS上设置为-1,在其他平台上设置为511。rcvbuf=SIZE
设置所监听端口的接收缓存大小(SO_RCVBUF选项)sndbuf=SIZE
设置所监听端口的发送缓存大小(SO_SNDBUF选项)- 注意:
(1) 基于port:listen PORT
指令监听在不同的端口
(2) 基于hostname:server_name
指令指向不同的主机名
(3) 基于ip的虚拟主机
listen IP:PORT
> IP 地址不同 - 示例:
listen 192.168.0.100:8000; # 监听192.168.0.100的8000端口
listen 192.168.0.100; # 监听192.168.0.100,不写端口则默认为80端口
listen 8000; # 监听本地8000端口
listen *:8000; # 同上
listen localhost:8000; # 同上
listen [::]:8000; # 监听本地的ipv6的8000端口
listen [::1]; # 监听本地的ipv6地址,不写端口则默认为80
🅿 3.server_name设置虚拟服务器名字
- 虚拟主机的主机名称后可跟多个由空白字符分隔的字符串。
- 支持
*
通配任意长度的任意字符
server_name *.studylinux.xyz www.studylinux.*
- 支持
~
起始的字符做正则表达式模式匹配
server_name ~^wwww\d+\.studylinux\.xyz$
# \d 表示[0-9] - 匹配机制:
- 首先是字符串精确匹配 如:
www.studylinux.xyz
- 左侧*通配符 如:
*.studylinux.xyz
- 右侧*通配符 如:
www.studylinux.*
- 正则表达式 如:
~^.*\.studylinux\.xyz$
- 都没写,则匹配default_server
- 首先是字符串精确匹配 如:
🅿 4.tcp_nodelay on | off;
- 在keepalived模式下的连接是否启用
TCP_NODELAY
选项。 - off时,延迟发送,合并多个请求后再发送
- 默认On时,不延迟发送。
🅿 5.sendfile 零拷贝是否开启
- 是否启用sendfile零拷贝功能,即在内核中封装报文直接发送。
- 默认Off。推荐启用on
🅿 6.server_tokens 响应报文的server头部是否显示nginx版本
- 是否在响应报文的Server首部显示nginx版本
🅿 7.root
- 设置web资源的路径映射;用于指明请求的URL所对应的文档 的目录路径,可用于http, server, location, if in location
- 示例
server
{
listen 80 default_server reuseport;
#listen [::]:80 default_server ipv6only=on;
server_name studylinux.xyz;
index index.html index.htm index.php;
root /data/wordpress;
}
/ 网络上访问http://www.studylinux.xyz/images/logo.jpg
/ 实际上访问的是服务器上/data/woropress/images/logo.jpg
实验:配置两个虚拟机主机,一个PC web站点,一个Mobile web主机
- 配置www.studylinux.con PC WEB站点
vim /apps/nginx/conf.d/pc.conf
server{
listen 80;
server_name www.studulinux.com;
location / {
root /data/nginx/pc;
}
}
mkdir -p /data/nginx/pc
echo "www.studylinux.com PC WEB" > /data/nginx/PC/index.html
nginx -t
nginx -s reload
/ 修改win10主机hosts文件
172.20.54.37 www.studylinux.com
- 配置mobile.studyliux.com 手机站点
vim /etc/nginx/conf.d/mobile.conf
server {
listen 80
server_name mobile.studylinux.com
location / {
root /data/nginx/mobile;
}
}
mkdir -p /data/nginx/mobile
echo "www.studylinux.com MOBILE WEB" > /data/nginx/mobile/index.html
nginx -t
nginx -s reload
/ 修改win10主机hosts文件
172.20.54.37 mobile.studylinux.com
🅿 8.alias PATH
- 路径别名,文档映射的另一种机制;仅能用于location上下文
- 示例
location /bbs/ {
alias /web/forum/;
}
/ 访问过程www.studylinux.xyz/bbs/index.html > www.studyliunx.xyz/web/forum/index.html
location /bbs/ {
root /web/forum/;
}
/ 访问过程www.studylinux.xyz/bbs/index.html > www.studyliunx.xyz/web/forum/bbs/index.html
-
注意:location中使用root指令和alias指令的意义不同
(a) root,给定的路径对应于location中的/uri/
左侧的/
(b) alias,给定的路径对应于location中的
/uri/
右侧的/
🅿 9.location的详细使⽤
在没有使⽤正则表达式的时候,nginx会先在server中的多个location选取匹配度最⾼的⼀个uri
,uri是⽤⼾请求的
字符串,即域名后⾯的web⽂件路径,然后使⽤该location模块中的正则url和字符串,如果匹配成功就结束搜索,
并使⽤此location处理此请求。
- 语法规则:
location [=|~|~*|^~] /uri/ { … }
= #⽤于标准uri前,需要请求字串与uri精确匹配,如果匹配成功就停⽌向下匹配并⽴即处理请求。
~ #⽤于标准uri前,表⽰包含正则表达式并且区分⼤⼩写,并且匹配
!~ #⽤于标准uri前,表⽰包含正则表达式并且区分⼤⼩写,并且不匹配
~* #⽤于标准uri前,表⽰包含正则表达式并且不区分⼤写,并且匹配
!~* #⽤于标准uri前,表⽰包含正则表达式并且不区分⼤⼩写,并且不匹配
^~ #⽤于标准uri前,表⽰包含正则表达式并且匹配以什么开头
$ #⽤于标准uri前,表⽰包含正则表达式并且匹配以什么结尾
\ #⽤于标准uri前,表⽰包含正则表达式并且转义字符。可以转. * ?等
* #⽤于标准uri前,表⽰包含正则表达式并且代表任意⻓度的任意字符
- 匹配优先级从高到低:
=, ^~, ~/~*, /
- 生产案例
location = / {
[ configuration A ] # “ /” 请求将与配置A匹配
}
location / { # /index.html请求将与配置B匹配
[ configuration B ]
}
location /documents/ { # /documents/document.html请求将与配置C匹配
[ configuration C ]
}
location ^~ /images/ { # /images/1.gif 请求将与配置D匹配
[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ { # /documents/1.jpg 请求将与配置E匹配
[ configuration E ]
}
- 示例
vim /apps/nginx/conf.d/pc.conf
server {
listen 80;
server_name www.studylinux.com;
location / {
root /data/nginx/pc;
location /admin/{
root /data/nginx/;
}
}
}
nginx -t
nginx -s reload
mkdir -p /data/nginx/admin
echo "/data/nginx/admin" > /data/nginx/admin/index.html
🅿 10. index file
- 指定默认网页资源,注意,这个是在
ngx_http_index_module
模块。
未完待续