NGINX 配置文件入门

内容纲要

查看「NGINX 专题」获取更多相关内容


NGINX 及其模块的工作方式由配置文件决定

配置文件路径

nginx.conf 文件是 NGINX 服务使用的默认配置入口。这个配置文件为诸如 worker 进程、调优、日志、加载动态模块以及对其他 NGINX 配置文件的引用等设置了全局设置。

该文件可能位于 /usr/local/nginx/conf/etc/nginx/usr/local/etc/nginx 目录下,这个目录是 NGINX 服务器的默认配置根目录。

一般来说通过预构建包安装的和从源代码编译安装的 NGINX 配置路径有所不同,你可以通过测试配置文件的命令来查看当前系统 NGINX 的配置文件路径:

sudo nginx -t
# 通过预构建包安装的返回结果:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

# 通过源码构建的返回结果:
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

然后可以使用命令查看 nginx.conf 的内容:

less /usr/local/nginx/conf/nginx.conf

语法规则

  1. 配置文件由指令与指令块构成;
  2. 每条指令以分号(;)结尾,指令与参数间以空格符号分割;
  3. 指令块以大括号({ })将多条指令组织在一起;
  4. include 语句允许组合多个配置文件以提升可维护性;
  5. 使用 # 符号添加注释;
  6. 使用 $ 符号使用变量;
  7. 部分指令参数支持正则表达式;

配置内容结构

正如「语法规则」中提到的,配置文件由指令与指令块构成,而指令块以大括号将多条指令组织在一起

main
├── events
├── http
│   └── server
│       └── location
├── mail
└── stream
# 运行 NGINX woker 进程的用户及用户组(用户组可不指定)
# user user [group]
user www;

# worker 进程进程数
worker_processes auto;

# 错误日志存放路径
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
error_log  logs/error.log;

# NGINX 进程的 pid 存放路径
pid logs/nginx.pid;

events {
    # 每个 worker 进程的最大连接数
    worker_connections  1024;
}

http {
    server {
        location / {
        }
    }
}

基本上每个刚安装好的 NGINX 的默认配置都像上面这样,由一些全局配置指令、一个 events {} 指令块和一个 http {} 指令块组成。

不同的安装方式,甚至是不同发行版的预构建包的默认配置都会有所不同,例如在 Debian 谱系中的 userwww-data,而 Red Hat 谱系的为 nginx,不过不需要太过纠结于这些。

组合多个配置文件

为了有助于维护以及保持配置文件的简洁,不应该直接将站点的配置直接写入在主配置文件 nginx.conf 中,安装预构建包版 NGINX 的默认配置中,在 http {} 指令块下有着 include 语句,在 Debian 谱系的 NGINX 预构建包默认配置文件中是这样的:

...
http {
    ...
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
    ...
}

而在 Red Hat 谱系的 NGINX 预构建包默认配置文件中是这样的:

...
http {
    ...
    include /etc/nginx/conf.d/*.conf;
    ...
}

先说说常见于 Debian 及其谱系如 Ubuntu 的 include /etc/nginx/sites-enabled/*;,在 /etc/nginx/ 目录下除了 sites-enabled 目录还有个 sites-available 目录,而真正的配置文件放置于 sites-available 目录内

当需要启用虚拟主机(如编写好了一份配置如 example.com.conf)时创建链接到 sites-enabled

sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/

反之,当删除 sites-enabled 目录内当某个虚拟主机链接文件,就是将该虚拟主机禁用。这样的好处是可以在禁用某个虚拟主机的同时又将其配置文件进行保留,在下次使用时不用再写一次配置文件。

💡 你可能已经在很多使用 Ubuntu 的教程中看到过这样的用法,不过在《NGINX Cookbook》一书中表示这样的做法惯例已经废弃了

现在更推荐的是使用前者 /etc/nginx/conf.d/*.conf,可以看到它匹配的是以 .conf 结尾的文件,所以只要将独立的虚拟主机配置文件以 .conf 文件名后缀写入到 /etc/nginx/conf.d/ 目录中,如果想禁用某个虚拟主机的配置,那么将其后缀名进行修改,如 example.com.conf.bak 就不会启用了。

❓ 以上都是在讲当遇到预构建包版本的 NGINX 的配置时的处理方式,那么如果是编译安装的 NGINX 呢?

编译安装的 NGINX 默认配置中 http {} 默认没有 include 语句,所以需要手动修改 nginx.conf 配置文件:

...
http {
    ...
    include /usr/local/etc/nginx/*.conf;
    ...
}

此处将包含其他配置的路径设为 /usr/local/etc/nginx/*.conf,然后使用命令在 /usr/local/etc/ 下新建名为 nginx 的目录,以及重载 NGINX:

sudo mkdir /usr/local/etc/nginx/
sudo nginx -s reload

实践

设置一个用于存放 Web 项目文件的目录:

sudo mkdir /var/www

如果是用包管理器安装的 NGINX,有一个默认站点的配置文件是安置在 /etc/nginx/sites-available 下的,但如果是编译安装的,这个默认站点配置就放在主配置文件 nginx.conf 中,而为了管理和维护不建议这么操作,所以做个示例将这部分内容单独剥离出来

# 新建一个专门放置 Web 项目的目录(和包管理器安装的 NGINX 路径一致)
sudo mkdir /var/www/html

# 然后将 NGINX 默认示例 HTML 文件移动过去
sudo cp /usr/local/nginx/html/index.html /var/www/html
sudo cp /usr/local/nginx/html/50x.html /var/www/html

修改 nginx.conf 配置文件

sudo vim /usr/local/nginx/conf/nginx.conf

http {} 下部分剪切出来或删除或完全注释掉:

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

并且同样在 http {} 下添加一条:

include /usr/local/etc/nginx/*.conf;

如果你想设置成与包管理器安装的 NGINX 配置路径(include /etc/nginx/conf.d/*.conf;)一致也可以

然后新建一个专门放置 http {} 配置文件的目录,以及默认配置站点的配置文件:

sudo mkdir /usr/local/etc/nginx/
sudo vim /usr/local/etc/nginx/default.conf

配置文件内容如下:

server {
    listen       80;
    server_name  localhost;
    root /var/www/html

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

注意第四行内容,设置了 HTML 文件路径,然后重载 NGINX:

sudo nginx -s reload
# 或者
sudo systemctl reload nginx

如果配置文件没有修改正确可以使用命令 sudo nginx -t 查看是配置的第几行出现了问题。


小结

  1. 通过预先构建和源码构建安装的 NGINX,一般来说配置文件的默认路径有所不同;
  2. 不要直接将新建站点的配置信息直接写入到 NGINX 的主配置文件 nginx.conf 中,这样并不利于管理和维护;
  3. 接上一条,参照前文所述将不同站点配置写成各自独立的 .conf 文件存入如 /etc/nginx/conf.d/ 这样的目录中,在主配置文件 nginx.conf,利用 include 语句组织调用它们;
  4. Debian 系预先构建的 NGINX 默认是将站点独立配置文件写入到 sites-enabled 目录下然后链接到 sites-available,这个方法已被废弃,能用但不建议这么用;