dushaoshuai / dushaoshuai.github.io

https://www.shuai.host
0 stars 0 forks source link

nginx [engine x] #91

Open dushaoshuai opened 1 year ago

dushaoshuai commented 1 year ago

one master process and several worker processes; number of worker processes is configurable;

dushaoshuai commented 1 year ago

event-based model

dushaoshuai commented 1 year ago

configuration file: nginx.conf 在 nginx -h 的输出中可以看到配置文件的路径

dushaoshuai commented 1 year ago

nginx -s signal : send signal to a master process: stop, quit, reopen, reload

dushaoshuai commented 1 year ago

kill -s QUIT 1628 is the same as nginx -s quit

dushaoshuai commented 1 year ago

https://nginx.org/en/docs/beginners_guide.html#conf_structure

configuration file's structure:

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: #

dushaoshuai commented 1 year ago

Permission denied Error on Manjaro:

Running under different user

dushaoshuai commented 1 year ago

$http_x_x_x 应该是紧挨着的前一个客户端传来的 header 值。

dushaoshuai commented 9 months ago

nginx -V 查看各种文件的位置

dushaoshuai commented 9 months ago

nginx.conf 中可以有多个 http context 吗?

在 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 块来配置。这种结构既保持了配置的清晰和组织性,又允许了高度的灵活性和可扩展性。

dushaoshuai commented 9 months ago

多个 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 块。

注意事项

dushaoshuai commented 9 months ago

使用 SSL/TLS(监听 443 端口)时,如何正确配置 SSL 证书和密钥?

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'; # 推荐的加密套件

    # 其他配置...
}
dushaoshuai commented 9 months ago

nginx Beginner’s Guide

dushaoshuai commented 9 months ago

https://byronhe.com/post/2015/05/08/https-config-optimize-in-nginx/