【问题标题】:How nginx pick the configuration order?nginx如何挑选配置顺序?
【发布时间】:2016-11-17 04:11:56
【问题描述】:

在我的 /etc/nginx/nginx.conf 文件中我有配置。如:-

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    include /etc/nginx/conf.d/*.conf;
}

现在,我不想污染上面默认的 nginx.conf 文件,所以我在 /etc/nginx/conf.d/default.conf 中保留了配置:-

worker_processes 2;
events {
    worker_connections  2048;
}  

我的问题是,在上述情况下,nginx 会覆盖或从 default.conf 文件或 nginx.conf 文件中选择 worker_processes 和 worker_connections 的配置吗?另外,我想知道简而言之nginx是如何处理配置文件的?

【问题讨论】:

    标签: nginx


    【解决方案1】:

    对于“nginx如何处理配置文件”,一个简单的方法是:

    • 读取配置以/etc/nginx/nginx.conf开始
    • 指令从上到下读取
    • includeing 文件将其插入到include 的位置 类似于 C 预处理器的方式
    • 设置具有范围,例如httpserverlocation,按从宽到窄的顺序排列
    • 某些设置可能出现在多个范围级别
    • 在较窄范围内设置参数会覆盖在较宽范围内设置相同参数
    • 同一范围内的冲突设置被拒绝

    有关详细信息,请参阅有关 Nginx 目录结构的 Debian wiki 文章。

    【讨论】:

      【解决方案2】:

      我确定 default.conf 中的配置将首先由 nginx 选择,这就是为什么“include /etc/nginx/conf.d/*.conf;”行包含以覆盖任何默认值并添加功能。

      【讨论】:

        猜你喜欢
        • 2012-01-16
        • 2014-08-20
        • 2012-11-07
        • 2019-10-20
        • 1970-01-01
        • 2019-01-27
        • 2020-12-21
        • 2019-01-21
        • 2011-04-16
        相关资源
        最近更新 更多