最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【基本解决】nginx的配置参数含义和各个配置文件路径之间的关系

nginx crifan 3609浏览 0评论

折腾:

【已解决】把本地前端页面部署到在线Flask环境中

期间,还是要对nginx本身搞清楚:

nginx的各个参数的含义

以及各个nginx的配置文件的路径,及其其中配置文件的优先级,生效的顺序等等都搞清楚了,才能继续折腾。

nginx 配置 详解

centos nginx 配置 详解

nginx配置文件详解中文版 – 简书

(总结)Nginx配置文件nginx.conf中文详解

Nginx服务器搭建和基本配置详解-baby神-51CTO博客

nginx服务器安装及配置文件详解 – Sean’s Notes – SegmentFault 思否

“Nginx配置文件主要分成四部分:main(全局设置)、server(主机设置)、upstream(上游服务器设置,主要为反向代理、负载均衡相关配置)和 location(URL匹配特定位置后的设置),每部分包含若干个指令。main部分设置的指令将影响其它所有部分的设置;server部分的指令主要用于指定虚拟主机域名、IP和端口;upstream的指令用于设置一系列的后端服务器,设置反向代理及后端服务器的负载均衡;location部分用于匹配网页位置(比如,根目录“/”,“/images”,等等)。他们之间的关系式:server继承main,location继承server;upstream既不会继承指令也不会被继承。它有自己的特殊指令,不需要在其他地方的应用。”

CentOS 下Nginx 配置详解 – 明月知心

centos nginx安装与配置 – 简书

算了,还是有点点蒙。

还是要先去了解这些配置路径的关系:

nginx /etc/conf conf.d relation

Understanding the Nginx Configuration File Structure and Configuration Contexts | DigitalOcean

NGINX Docs | Creating NGINX Plus and NGINX Configuration Files

Mounting Files vs Mounting Directories | Servers for Hackers

NGINX Docs | Installing NGINX Open Source

还是好好看看官网的介绍吧:

再去看文档:

NGINX Docs | Configuring NGINX Plus as a Web Server

但是写的是:NGINX Plus,所以去看看看:

【已解决】Nginx Plus和Nginx有何区别

所以对于此处只是学习普通的server和location方面的配置的话,

暂时可以认为Nginx Plus的文档,也是适用于Nginx的

其实配置NGINX为Web服务器,本质上来说就是:

配置对应规则,决定了,会去处理哪些URL,以及如何处理对应的URL的资源

而底层规则则是:

定义了一堆的虚拟主机virtual server,用于处理特定的域名或IP的请求。

【总结】

至此,各个配置文件的生效关系,不是很清楚。

对于各种nginx的配置,大概清楚了:

nginx服务器安装及配置文件详解 – Sean’s Notes – SegmentFault 思否

“Nginx配置文件主要分成四部分:main(全局设置)、server(主机设置)、upstream(上游服务器设置,主要为反向代理、负载均衡相关配置)和 location(URL匹配特定位置后的设置),每部分包含若干个指令。main部分设置的指令将影响其它所有部分的设置;server部分的指令主要用于指定虚拟主机域名、IP和端口;upstream的指令用于设置一系列的后端服务器,设置反向代理及后端服务器的负载均衡;location部分用于匹配网页位置(比如,根目录“/”,“/images”,等等)。他们之间的关系式:server继承main,location继承server;upstream既不会继承指令也不会被继承。它有自己的特殊指令,不需要在其他地方的应用。”

默认的nginx的配置文件名是:nginx.conf

常见的nginx,不同系统安装后的位置有:

  • /usr/local/nginx/conf

  • /etc/nginx

    • 此处CentOS 7中yum install后就是这个位置

  • /usr/local/etc/nginx

Directives指令:

参数 和值,最后加个分号

比如:

<code>user             nobody;
error_log        logs/error.log notice;
worker_processes 1;
</code>

成一组的参数则放到大花括号{}中。

为了配置文件便于管理,建议把配置文件,根据功能去划分,放到:

etc/nginx/conf.d

中去,然后在

nginx.conf

用include去包含进来

<code>include conf.d/http;
include conf.d/stream;
include conf.d/exchange-enhanced;
</code>

Contexts上下文:

最顶级的几类指令有:

  • events – 通用的连接处理

  • http – HTTP流量

  • mail – 邮件Mail流量

  • stream – TCP和UDP的流量

其他在这些指令之外的,全局的,都叫做main context 主上下文

Virtual Servers虚拟主机服务器

用server去定义虚拟主机

比如:

  • 对于HTTP的访问流量来说:

    • 用http定义访问哪些IP或域名

    • 用location定义具体如何处理URI

  • 对于邮件(mail)和TCP/UDP(stream)

    • server中定义了访问哪些TCP端口还是UNIX端口

官网的demo配置:

<code>user nobody; # a directive in the 'main' context

events {
    # configuration of connection processing
}

http {
    # Configuration specific to HTTP and affecting all virtual servers  

    server {
        # configuration of HTTP virtual server 1       
        location /one {
            # configuration for processing URIs starting with '/one'
        }
        location /two {
            # configuration for processing URIs starting with '/two'
        }
    } 
    
    server {
        # configuration of HTTP virtual server 2
    }
}

stream {
    # Configuration specific to TCP/UDP and affecting all virtual servers
    server {
        # configuration of TCP virtual server 1 
    }
}
</code>

下一级的配置,默认是集成来自上一级的所有的配置

想要覆盖默认配置,在当前一级中设置对应的参数即可。

比如:

proxy_set_header

Syntax:
proxy_set_header field value;
Default:
proxy_set_header Host $proxy_host;
proxy_set_header Connection close;
Context:
http, server, location

Allows redefining or appending fields to the request header passed to the proxied server. The value can contain text, variables, and their combinations. These directives are inherited from the previous level if and only if there are no proxy_set_header directives defined on the current level. By default, only two fields are redefined:

proxy_set_header Host       $proxy_host;

proxy_set_header Connection close;

If caching is enabled, the header fields “If-Modified-Since”, “If-Unmodified-Since”, “If-None-Match”, “If-Match”, “Range”, and “If-Range” from the original request are not passed to the proxied server.

An unchanged “Host” request header field can be passed like this:

proxy_set_header Host       $http_host;

However, if this field is not present in a client request header then nothing will be passed. In such a case it is better to use the $host variable – its value equals the server name in the “Host” request header field or the primary server name if this field is not present:

proxy_set_header Host       $host;

In addition, the server name can be passed together with the port of the proxied server:

proxy_set_header Host       $host:$proxy_port;

If the value of a header field is an empty string then this field will not be passed to a proxied server:

proxy_set_header Accept-Encoding “”;

注意:

另外,是否传递header参数,取决于:

“Syntax:

proxy_pass_request_headers on | off;

Default:

proxy_pass_request_headers on;

Context:

http, server, location

Indicates whether the header fields of the original request are passed to the proxied server.

location /x-accel-redirect-here/ {

    proxy_method GET;

    proxy_pass_request_headers off;

    proxy_pass_request_body off;

    proxy_pass …

}

See also the proxy_set_header and proxy_pass_request_body directives.”

很明显,默认是on,是传递header参数的。

而对于:

<code>proxy_set_header Host       $http_host;
</code>

还是:

<code>proxy_set_header Host       $host;
</code>

貌似后者更好。

另外,想要传递端口的话,也可以写成:

<code>proxy_set_header Host       $host:$proxy_port;
</code>

当配置文件更改后,需要reload重新加载

比如此处CentOS中可以用:

<code>systemctl reload nginx
</code>

对于关系,继续去看看:

nginx /etc/nginx /etc/nginx/conf.d relation

Understanding the Nginx Configuration File Structure and Configuration Contexts | DigitalOcean

转载请注明:在路上 » 【基本解决】nginx的配置参数含义和各个配置文件路径之间的关系

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.168 seconds, using 22.05MB memory