nginx配置

nginx配置

参考自,B站技术蛋老师

环境:win10+nginx1.15.11,nginx1.24.0(后面主动换了高版本,不然正则有问题)

nginx的配置每次改动,需要重复加载

1
nginx -t && nginx -s reload

基础格式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
events{} #events模块必须有


http{ #http块允许多个server块

#全局设置包含文件
#避免css引用时为text/plain形式
include D:\phpstudy_pro\Extensions\Nginx1.15.11\conf\mime.types;

#服务文件夹块
server{
listen 82;
server_name localhost;

# return 200 "sysyly.\n";
#指定根目录路径
root D:\phpstudy_pro\Extensions\Nginx1.15.11\html;#html是自建的,1.24.0会默认有,可以新建就是了,不管它
index yly.html;#指定文件(当默认名不为index时)
}
}

html内容

1
2
3
4
5
6
<head>
<link rel="stylesheet" href="style.css"/>
</head>

<pre> asdsadsadsadsadasdylysadsadsadsadqweqwe </pre>

events事件必须要有,这个为什么先不纠结

之后在http模块后写入Server块,指定监听端口服务名,以及根路径名

若未写入Server块:(会出现无法访问情况)

若仅写入端口和服务名:(由于无响应数据,故404页面,后来又测试一遍成了403页面)

默认会去访问根路径下的index.html,除非用index指定别名文件,否则会是403页面

而include允许我们去包含不同文件,可以将nginx的配置看做多个文件

新增一个文件夹如conf.d文件夹,且默认文件default.conf

那么此时,nginx.conf的配置为:

1
2
3
4
5
6
7
8
9
10
11
events{}

http{

# 全局设置包含文件
# 避免css引用时为text/plain形式
include D:/phpstudy_pro/Extensions/Nginx1.15.11/conf/mime.types;

include D:/phpstudy_pro/Extensions/Nginx1.15.11/conf/conf.d/*.conf;

}

default.conf的配置为:

1
2
3
4
5
6
7
8
9
#服务文件夹块
server{
listen 82;
server_name localhost;

#return 200 "sysyly.\n";
root D:/phpstudy_pro/Extensions/Nginx1.15.11/html;
index yly.html;
}

那么以后只需要改动default.conf的配置即可

location

location的目的在于可以自定义路径;

而根路径root是要包含location指定目录的一个上级目录

下面实现的效果是只访问localhost,就相当于访问根目录的index.html文件(注意要有index.html文件)

1
2
3
4
5
...  

location / {
root D:/phpstudy_pro/Extensions/Nginx1.15.11/html;
}
1
2
3
4
5
6
7
html> mkdir /yly #新建文件夹
> move index.html yly/ #移动文件
...

location /yly {
root D:/phpstudy_pro/Extensions/Nginx1.15.11/html;
}

去浏览器访问倒是可以 localhost:82/yly即可

但是curl时

1
2
curl localhost:82/yly #301
curl localhost:82/yly/ #200

之所以会这样,是因为它将 yly视为了一个文件,导致出错

而第二条语句添加了一个斜杠,意味着将 yly视为了一个文件夹

这样有一个好处是nginx帮你自动访问其中的文件,只要文件名匹配了即可,你比如 yly/index.html,只要知道文件夹的名字为 yly即可

这样的话,存在暴露的风险,那么还有一条完全匹配的格式

1
2
3
location = /yly/index.html {
root D:/phpstudy_pro/Extensions/Nginx1.15.11/html;
}

也就是要求必须访问 localhost:82/yly/index.html才可以访问到页面

(但是我同样的操作,为什么仅仅 /yly/就可以访问成功了?)

这里将index的名字改了改,比如 yly.html,就可以了,这个默认index.html,它自动帮你找,很烦

这里先不管,往下走

为了灵活地进行匹配,引入了正则

1
2
3
location ~ /movies/m[2-3].txt {
root D:/phpstudy_pro/Extensions/Nginx1.15.11/html;
}

之后根据 curl -i localhost/movies/m1.txt、m3.txt的结果来看,我十分怀疑,正则没有起到任何效果

我干脆换了nginx版本(1.24.0),正则有效

对于movies来说,由于只匹配m2,m3,m1.txt即使存在,也会显示 404

重定向

临时重定向,会在响应包给一个Loation

1
2
3
location /movies {
return 307 /yly/index.html;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
D:\phpstudy_pro\Extensions\nginx-1.24.0\nginx-1.24.0>curl -i localhost:82/movies
HTTP/1.1 307 Temporary Redirect
Server: nginx/1.24.0
Date: Mon, 22 Apr 2024 09:13:13 GMT
Content-Type: text/html
Content-Length: 171
Location: http://localhost:82/yly/index.html
Connection: keep-alive

<html>
<head><title>307 Temporary Redirect</title></head>
<body>
<center><h1>307 Temporary Redirect</h1></center>
<hr><center>nginx/1.24.0</center>
</body>
</html>

重写

为了更丝滑,更隐蔽,使用rewrite

1
rewrite /movies /yly/index.html;

这样直接就是200状态码了

多文件

上面的重定向/重写是针对单路径的,对于多路径

1
2
3
4
5
6
7
8
root D:/phpstudy_pro/Extensions/nginx-1.24.0/nginx-1.24.0/local;
index index.html;

rewrite /movies /yly/index.html;
location / {
add_header X-debug-uri "$uri";
try_files $uri $uri/ =404;
}
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
#访问存在的指定文件时
curl -i localhost:82/yly/yly.html

HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Mon, 22 Apr 2024 09:24:08 GMT
Content-Type: text/html
Content-Length: 28
Last-Modified: Mon, 22 Apr 2024 09:20:07 GMT
Connection: keep-alive
ETag: "66262bc7-1c"
X-debug-uri: /yly/yly.html
Accept-Ranges: bytes

<pre>this is ylyl !!!!</pre>

#访问文件夹,这个和设定的index有关
curl -i localhost:82/yly/

HTTP/1.1 200 OK
Server: nginx/1.24.0
Date: Mon, 22 Apr 2024 09:24:27 GMT
Content-Type: text/html
Content-Length: 114
Last-Modified: Mon, 22 Apr 2024 03:38:35 GMT
Connection: keep-alive
ETag: "6625dbbb-72"
X-debug-uri: /yly/index.html
Accept-Ranges: bytes

<head>
<link rel="stylesheet" href="style.css"/>
</head>

<pre> asdsadsadsadsadasdsadsadsadsadqweqwe </pre>

#访问不存在的文件时
curl -i localhost:82/342

HTTP/1.1 404 Not Found
Server: nginx/1.24.0
Date: Mon, 22 Apr 2024 09:24:50 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive

<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.24.0</center>
</body>
</html>

nginx 配置 https://zhuanlan.zhihu.com/p/130819099

自定义配置页面

对404,500等默认配置页面自定义

在root的路径下,写一个即可

1
error_page 404 /404.html;

反向代理

proxy_pass

相当于是后端地址吧,比如两个后端项目

1
2
3
4
5
6
7
8
...
location /p1 {
proxy_pass http://localhost:1234;
}

location /p2 {
proxy_pass http://localhost:4321;
}

负载均衡

分流作用

上游服务器 upstream

让nginx把流量导到指定的服务器集群,再负载均衡分配到集群里面的服务器。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
http{

upstream backend-servers{
server localhost:1234;
server localhost:4321;
}
server{
...
location / {
proxy_pass http://backend-servers;
}
}
}

#看起来很像随机代理池的用法

这个时候反复刷新的话,可以看到有概率会被分配到不同的服务器(设置不同的网页内容)

最后高配置服务器可以分配多一点流量,低的呢就低一点,这个时候需要用到weight,进行一个权重的分配

1
2
3
4
upstream backend-servers{
server localhost:1234 weight=2;
server localhost:4321 weight=6;
}

安全漏洞

https://book.hacktricks.xyz/v/cn/network-services-pentesting/pentesting-web/nginx

踩坑

nginx启动成功,浏览器无法访问?

A:我那个确实是include,还有root的路径配置错误导致的,细心点

  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2023-2025 是羽泪云诶
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信