nginx
值得注意的是 Nginx 默认上传最大大小是 1MB。
client_max_body_size 50M;
文件目录
CGI(Common Gateway Interface)通用网关【接口】,主要解决的问题是从客户端发送一个请求和数据,服务端获取到请求和数据后可以调用调用CGI【程序】处理及相应结果给客户端的一种标准规范。
fastcgi.conf:fastcgi相关配置文件
fastcgi.conf.default:fastcgi.conf的备份文件
fastcgi_params:fastcgi的参数文件
fastcgi_params.default:fastcgi的参数备份文件
scgi_params:scgi的参数文件
scgi_params.default:scgi的参数备份文件
uwsgi_params:uwsgi的参数文件
uwsgi_params.default:uwsgi的参数备份文件
mime.types:记录的是HTTP协议中的Content-Type的值和文件后缀名的对应关系
mime.types.default:mime.types的备份文件
nginx.conf:这个是Nginx的核心配置文件,这个文件非常重要,也是我们即将要学习的重点*
nginx.conf.default:nginx.conf的备份文件*
koi-utf、koi-win、win-utf这三个文件都是与编码转换映射相关的配置文件,用来将一种编码转换成另一种编码
html:存放nginx自带的两个静态的html页面*
50x.html:访问失败后的失败页面
index.html:成功访问的默认首页
logs:记录入门的文件,当nginx服务器启动后,这里面会有 access.log error.log 和nginx.pid三个文件出现。*
sbin:是存放执行程序文件nginx*
nginx是用来控制Nginx的启动和停止等相关的命令。*
nginx.conf
总览
main:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。
event:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
http:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
server:配置虚拟主机的相关参数,一个http中可以有多个server。
location:配置请求的路由,以及各种页面的处理情况。
main
- user 指定 worker 运行的用户,涉及用户权限等
- worker_processess x; 指定worker 进程数
- pid logs/nginx.pid; 将进程的 id 存储在该文件中
event
- worker_connections 1024; 一个worker 最大连接数
- use epoll; 使用epoll
epoll 高并发处理,不会同步阻塞而创建很多 worker,默认 on (异步非阻塞通信模式)
http
inclue etc/nginx/mime.types;
include 即是导入文件,观察文件内容,发现文件即是 http 结构中的内容(就相当于把文件中的内容插进 http 结构中)
而这个 mime.types 文件在同一目录,查看可以发现里面包含许多常见类型,mime.types 即是指定包含类型
default_type
默认 type 类型 ?
log_format main
日志格式,要与下连着看
access_log /var/log/nginx/access.log main;
用户请求日志会存放在 logs/access.log 里
sendfile on
默认打开, 文件高效传输
tcp_onpush on
需要 sendfile 开启,作用是等待数据包一定大小才发送,高效传输
keepalive_timeout x
keepalive 模式:处理完请求后仍然保持连接。在连接期间,如果接受到该客户端其他请求,就可以利用该未关闭的请求
timeout 即是指定保持连接时间,单位s
gzip on
压缩,内容传输会被压缩,降低传输加快传输,但也会增加cpu负担
在 http 中 gzip on 开启 gzip 压缩功能,提高传输效率,节约带宽
限制最小压缩,小于1字节的文件不会被压缩:
gzip_min_length 1;
定义压缩级别(文件越大,压缩越多,cpu使用越多:
gzip_comp_level 3;
定义压缩文件类型
gzip_types text/plain image/jpeg image/gif
server
虚拟服务器,主机
listen
监听端口号
server_name
对应请求的 Host 字段。
location
路由
/(root) 默认访问内容
error_page 发生错误时展示内容
跨域请求
- add_header ‘Access-Control-Allow-Origin’ *;
- add_header ‘Access-Control-Allow-Credentials’ ‘true’;
防盗链
让其他站点不能直接引用图片显示在其他站点上
在 server 全局中
1
2
3
4
5valid_referers *.imooc.com;
if ($invalid_referer) {
return 404;
}curl 模拟其他访问源访问:
curl --referer https://baidu.com -I 192.168.6.128/images
1 |
|
location
if 的优先级比 location 更高
location 路由规则,有多个表达式
location 顺序(与文件中顺序无关)
Nginx Location Match Visible (detailyang.github.io)
- 等号类型(=)的优先级最高。一旦匹配成功,则不再查找其他匹配项。
- ^~类型表达式。一旦匹配成功,则不再查找其他匹配项。
- 正则表达式类型(~ ~*)的优先级次之。如果有多个location的正则能匹配的话,则使用正则表达式最长的那个。
- / 通用匹配,没有其他匹配,就会来这。
具体符号
- ~ 表示执行一个正则匹配,区分大小写。搜寻有没有该字符串
- ~* 表示执行一个正则匹配,不区分大小写。搜寻有没有该字符串
- ^~ 表示普通字符匹配。使用前缀匹配。如果匹配成功,则不再匹配其他location。前缀匹配 (类似(等同)不加符号的匹配方式?)
- = 进行普通字符精确匹配。也就是完全匹配。精确匹配
- @ 它定义一个命名的 location,使用在内部定向时,例如 error_page, try_files
root 的用法:
1
2
3location /request_path/image/ {
root /local_path;
}location 中的 root 指根目录,具体的访问将指向 root/$request_uri (代理目录)
alias 的用法:
1
2
3location /request_path/image/ {
alias /local_path;
}当客户端请求 /request_path/image/cat.png 的时候, nginx把请求映射为/local_path/cat.png。(别名)
upstream
- upstream 集群,内网服务器
杂
docker run -d \ -v ~/nginx/html:/usr/share/nginx/html \ -v ~/nginx/conf:/etc/nginx \ -v ~/nginx/logs:/var/log/nginx \ -v /etc/localtime:/etc/localtime \ --net=host nginx
大概解释: 首先为什么都会重定向到有斜杠? 不知道 重定向到有斜杠的位置,则可知道的是都可以被路由,如果 alias 只是文本串替换,多个连续斜杠不影响(确实),则上面就解释的通了。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
用 host 模式就不用端口映射了,虽然也不能用网了..
2. 命令
- ./nginx -v 版本
- ./nginx -t 检查配置文件
- ./nginx 启动
- ./nginx -s stop 暴力关闭(会取消用户连接)
- ./nginx -s quit 等待用户连接关闭才停止
- ./nginx -s restart 重启
- ./nginx -s reload 重新加载配置文件
- ./nginx -c filename 指定特定的配置文件
3. return 返回
1. 返回状态码
return 404;
2. 返回字符串
return 200 'hello';
3. 地址跳转
return <http://www.baidu.com>
4. 返回自定义变量
set $name 'user123';
return 200;
5. 返回内置变量
return $uri;
6. 返回日志信息实例
return 200 '\$remote_addr - \$remote_user .......'
4. 访问:<http://127.0.0.1/test/>
\$uri:/test/test.html
\$request_uri:/test/
5. **if**后面接的**(括号)** 两者之间要有空格隔开,
6. ~ 用于判断正则表达式相等
7. 实际上,这两者之间并没有太大的区别。如果访问的是一个目录或文件夹,加上“/”后访问结果是一样的。例如,访问 <http://www.example.com/test/> 的效果和访问 <http://www.example.com/test> 的效果是一样的。
不过,如果要访问一个具体的文件,那么在网址末尾加上“/”就会出现错误了。例如,访问 <http://www.example.com/test.html> 可以正常访问到该文件,而访问 <http://www.example.com/test.html/> 就会出现错误了。
8. 访问网页一定注意后面有没有/!!!!
## New
- proxy_pass 需要协议
- proxy_pass
- 一个 server 就代表一个地址和端口,如果多个端口(地址)就要多个 server
- fastcgi_pass
## request
server 级别
[request process](https://nginx.org/en/docs/http/request_processing.html),这个
[server](https://nginx.org/en/docs/http/ngx_http_core_module.html#server)
### server_name
- `server_name` 对应请求中 `Header` 中的 `Host` 字段。
- `server_name ""` 这是默认值,即匹配 `Host` 为空的。
- The request’s header field “Host” to determine which server the request should be routed to. If its value does not match any server name, or the request does not contain this header field at all, then **nginx will route the request to the default server for this port**(万恶之源)。
默认 server 也就是第一个监听这个端口的。你可以在 `listen` 字段最后加一个 `default_server` 代表当前是 default 的。也就是说这是针对端口而言的。进一步来讲,是针对 listen 而言的。
注意这里是比较请求的 "Host" 这个 header 字段,而不是比较链接。
这也说明 nginx 实际上 会监听所有地址的 80 端口?确实是的 `sudo netstat -tunlp | grep 80` 可以发现 nginx 监听 `0.0.0.0`
- nginx first tests the IP address and port of the request against the listen directives of the server blocks. It then tests the “Host” header field of the request against the server_name entries of the server blocks that matched the IP address and port. If the server name is not found, the request will be processed by the default server. For example, a request for `www.example.com` received on the 192.168.1.1:80 port will be handled by the default server of the 192.168.1.1:80 port, i.e., by the first server, since there is no `www.example.com` defined for this port.
- 即 server_name 是针对 host 字段(不一定有),`listen` 是接受的 ip 或端口(一定有)。收到 request 时,我们先匹配一定有的 ip 或端口,在这里面再继续匹配 host 字段,没有匹配到就选择默认的。这也解释了为什么我们只通过 ip 访问一些网站就不行
### listen
[listen](https://nginx.org/en/docs/http/ngx_http_core_module.html#listen)
- If the directive is not present then either *:80 is used if nginx runs with the superuser privileges, or `*:8000` otherwise.
即没有 ip 字段,则会监听 `0.0.0.0`
## location
location 级别
[location](https://nginx.org/en/docs/http/ngx_http_core_module.html#location)
[proxy_pass](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass)
- tutorial: <https://www.digitalocean.com/community/tutorials/understanding-the-nginx-configuration-file-structure-and-configuration-contexts>, <https://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms>, <https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&ved=2ahUKEwjoq_PbhNeBAxU2m1YBHULlCwAQFnoECCkQAQ&url=https%3A%2F%2Fwww.nginx.com%2Fblog%2Favoiding-top-10-nginx-configuration-mistakes%2F&usg=AOvVaw1Qqb1Fve7WyVsO8AQB44NX&opi=89978449>
- location 路径(文件没有)都跟 `/`,root 后不跟 `/`,因为 `request_url` 以 `/` 起始。
- Note that locations of all types test only a URI part of request line without arguments. This is done because arguments in the query string may be given in several ways, for example:
- To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered. Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used. 也就是说有两种模式:一种是前缀匹配模式,一种是正则匹配模式
前缀匹配模式用 空 或 = 或 ~^ 来定义
正则表达式模式用 `~ | ~*` 来定义
首先进行前缀匹配,如果过程有 `=` 完全匹配则直接结束。如果有 空 且**执行完所有前缀匹配后**,记录下最长的那个,然后再找正则表达式匹配,按顺序,如果找到第一个匹配则直接选择正则表达式匹配结束。如果没找到就使用之前的匹配
- Regular expressions can contain captures (0.7.40) that can later be used in other directives.
- The “@” prefix defines a named location. Such a location is not used for a regular request processing, but instead used for request redirection. They cannot be nested, and cannot contain nested locations.
- If a location is defined by a prefix string that ends with the slash character, and requests are processed by one of proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, memcached_pass, or grpc_pass, then the special processing is performed. In response to a request with URI equal to this string, but without the trailing slash, a permanent redirect with the code 301 will be returned to the requested URI with the slash appended. If this is not desired, an exact match of the URI and location could be defined like this:
不理解
- 如果最后路由出来的地址是文件夹,就会看 `index` 设置,默认 `index.html` 等。
- 可以嵌套
- proxy to other servers or locations (using http, fastcgi, scgi, and uwsgi proxying).
- If a request ends with a slash, NGINX treats it as a request for a directory and tries to find an index file in the directory. The index directive defines the index file’s name (the default value is index.html). To continue with the example, if the request URI is /images/some/path/, NGINX delivers the file /www/data/images/some/path/index.html if it exists. If it does not, NGINX returns HTTP code 404 (Not Found) by default. To configure NGINX to return an automatically generated directory listing instead, include the on parameter to the autoindex directive:`autoindex: on`。
即如果链接以斜杠结尾,则代表请求目录,则 `index` 代表实际返回的内容(可以设置为图片)。如果设置了 `autoindex: on`,则是返回目录内容。
### alias and root
- An alias within a regular expression location block requires a value which specifies the full path to the target file. 所以用正则表达式和 alias 时要小心
[alias](http://nginx.org/en/docs/http/ngx_http_core_module.html#alias)
最好还是用 root(请求开头和资源位置结尾重合时用!!)
- 测试
location alias request res 1 代表有斜杠或有结果,括号内容代表重定向
```text
1 1 1 1
1 1 0(1) 1
0 0 0(1) 1
0 0 1 1
0 1 1 1
0 1 0(1) 1
0 1 1 1
1 0 1 0
1 0 0(1) 0
- 这块大概只要明白:root,alias 原理(前者直接衔接,后者文本串替换),然后
location
在不是文件时最后有/
,是文件时不用/
,而alias
做到和location
一样的规则,而root
肯定只会用于路由文件夹,再加上是衔接的原因就不用再/
(虽然也可以有多个/
)
其他
expires 30d;
try_files
proxy_pass 反向代理
代理,即不改变 request_url,由服务器去请求数据然后返回。
如
proxypass https://lllei.top
,这样就是代理add_header
if
The only directives that are considered reliably safe to use inside of these contexts are the return and rewrite directives (the ones this context was created for).
security
static content
web server
https
With this configuration a browser receives the default server’s certificate, i.e. www.example.com
regardless of the requested server name. This is caused by SSL protocol behaviour. The SSL connection is established before the browser sends an HTTP request and nginx does not know the name of the requested server. Therefore, it may only offer the default server’s certificate.
也就是说如果有多个证书时,我们应该按 listen
中的 IP 把证书分开来,否则可能会发生证书错误。
reverse proxy
reverse proxy,讲了一些默认行为和建议。
注意区分 proxy server 和 proxied server,前者指 nginx,后者指被代理的 server。
content cache
rewrite
debug
ddos
- server_name is space-separated, not comma-separated!!!!