Open dushaoshuai opened 1 year ago
event-based model
configuration file: nginx.conf
在 nginx -h
的输出中可以看到配置文件的路径
nginx -s signal
: send signal to a master process: stop, quit, reopen, reload
kill -s QUIT 1628
is the same as nginx -s quit
https://nginx.org/en/docs/beginners_guide.html#conf_structure
configuration file's structure:
name value;
name value { ... more directives ...}
context: events http server location main context
The events and http directives reside in the main context, server in http, and location in server.
comment: #
Permission denied Error on Manjaro:
$http_x_x_x
应该是紧挨着的前一个客户端传来的 header 值。
nginx -V
查看各种文件的位置
在 Nginx 的配置文件 nginx.conf
中,通常只能定义一个 http
块(context)。这个 http
块用于封装全局的 HTTP 设置,它包含了一系列的全局指令,以及定义了如何处理 HTTP 请求的多个 server
块。
虽然您不能有多个 http
块,但您可以在单个 http
块内定义多个 server
块,每个 server
块代表一个虚拟主机,可以有自己的配置和规则。这种方式使得您可以在同一个 Nginx 实例中托管多个网站或应用。
例如,一个基本的 nginx.conf
文件可能看起来像这样:
http {
# 全局 HTTP 设置
# 第一个虚拟主机
server {
listen 80;
server_name www.example.com;
# ... 其他配置 ...
}
# 第二个虚拟主机
server {
listen 80;
server_name www.anotherexample.com;
# ... 其他配置 ...
}
# 更多 server 块...
}
在这个例子中,所有全局 HTTP 相关的配置都放在一个 http
块中,而不同的网站或服务通过不同的 server
块来配置。这种结构既保持了配置的清晰和组织性,又允许了高度的灵活性和可扩展性。
是的,在 Nginx 中,多个 server
块可以监听相同的端口。这是非常常见的做法,尤其是在托管多个网站或域名(虚拟主机)在同一物理服务器上的情况下。Nginx 通过检查请求的主机头部(Host
header)来确定应该将请求路由到哪个 server
块。\n\n以下是一个基本示例,展示了如何在 Nginx 中配置两个虚拟主机,它们都监听同一个端口(假设为 80)但服务于不同的域名:
http {
server {
listen 80;
server_name www.example.com;
# 针对 www.example.com 的配置...
}
server {
listen 80;
server_name www.anotherexample.com;
# 针对 www.anotherexample.com 的配置...
}
}
在这个配置中,两个 server
块都监听端口 80,但根据 server_name
指令处理不同的域名。当一个请求到达 Nginx 服务器时,Nginx 会检查请求的 Host
头部,并将请求转发到匹配该域名的 server
块。
server_name
在每个 server
块中正确设置,以便 Nginx 可以正确地将请求路由到适当的虚拟主机。server
块没有指定 server_name
,或者没有任何 server
块匹配请求的 Host
头部,Nginx 会将请求发送到第一个监听该端口的 server
块。Host
头部进行初始连接的路由。在这种情况下,通常使用 SNI(服务器名称指示)来处理多个域名。server {
listen 443 ssl;
server_name www.example.com;
ssl_certificate /path/to/your/certificate.pem; # SSL 证书文件的路径
ssl_certificate_key /path/to/your/private.key; # SSL 私钥文件的路径
ssl_protocols TLSv1.2 TLSv1.3; # 推荐的 TLS 版本
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384'; # 推荐的加密套件
# 其他配置...
}
one master process and several worker processes; number of worker processes is configurable;