Nginx配置指南

2019/08/02 Nginx

Nginx配置指南

Nginx 的配置文件的格式非常合乎逻辑。学习这种格式以及如何使用每个部分是基础,这将有助于你手工创建一个配置文件。

Nginx配置文件

Nginx的配置文件默认在Nginx程序安装目录的conf二级目录下,主配置文件为nginx.conf,假设您的Nginx安装在/usr/local/webserver/nginx/目录下,那么默认的主配置文件则为/usr/local/webserver/nginx/nginx.conf

基本配置格式

基本的Nginx配置文件由若干个部分组成。每一个部分都是通过下列方法定义的。

<section> {
<directive> <parameters>;
}

需要注意的是每一个指令行都由分号结束(;),这标记着一行的结束。大括号({})实际上表示一个新上下文(context),但是大多数情况下我们将它们作为“节、部分(section)”来读。

Nginx的全局配置参数

全局配置部分被用于配置对整个server都有效的参数和前一个章节中的例外格式。全局部分可能包含配置指令,例如,user和worker_processes,也包括“节、部分(section)”。例如,events,这里没有大括号({})包围全局部分。

下面是一个使用这些指令的简短的例子。

# we want nginx to run as user 'www'
user www;
# the load is CPU-bound and we have 12 cores
worker_processes 12;
# explicitly specifying the path to the mandatory error log
error_log /var/log/nginx/error.log;
# also explicitly specifying the path to the pid file
pid  /var/run/nginx.pid;
# sets up a new configuration context for the 'events' modul eevents {
# we're on a Solaris-based system and have determined that nginx
# will stop responding to new requests over time with the default
# connection-processing mechanism, so we switch to the second-best use /dev/poll;
# the product of this number and the number of worker_processes
# indicate s how many simultaneous connections per IP:port pair are
# accepted
worker_connections 2048;
}

这一部分应该放置在nginx.conf文件的顶部。

使用include文件

在Nginx的配置文件中,include文件可以在任何地方,以便增强配置文件的可读性,并且能够使得部分配置文件重新使用。使用include文件,要确保被包含的文件自身有正确的Nginx语法,即配置指令和块(blocks),然后指定这些文件的路径。

include /opt/local/etc/nginx/mime.types;
在路径中出现通配符,表示可以配置多个文件。
include /opt/local/etc/nginx/vhost/*.conf;
如果没有给定全路径,那么Nginx将会依据它的主配置文件路径进行搜索。

Nginx测试配置文件很容易,通过下面的命令来完成。

nginx -t -c <path-to-nginx.conf>

该命令将测试Nginx的配置文件,包括include文件,但是它只检查语法错误。

Http的server部分

在Http中,server部分或者是Http配置context是可用的,除非在编译安装Nginx时没有包含Http模块(也就是使用了–without-http)。这部分控制了Http模块的方方面面,是使用最多的一个部分。 本部分的指令用于处理Http连接,因此该模块提供了相当数量的指令。为了更容易理解这些指令我们将它们划分为不同的类型来讲述。

客户端指令

文件I/O指令

虚拟server部分

任何由关键字server开始的部分都被称作“虚拟服务器”部分。它描述的是一组根据server_name 指令逻辑分割的资源,这些虚拟服务器响应 Http 请求,因此它们都包含在http部分中。

一个虚拟服务器由listen和server_name指令组合定义,listen指令定义了一个IP地址/端口组合或者是UNIX域套接字路径。

listen address[:port];
listen port;
listen unix:path;

listen指令唯一地标识了在Nginx下的套接字绑定,此外还有一些其他的可选参数。

Locations

location指令可以用在虚拟服务器server部分,并且意味着提供来自客户端的URI或者内部重定向访问。除少数情况外,location也可以被嵌套使用,它们被作为特定的配置尽可能地处理请求。 location定义如下。

location [modifier] uri {...}
或者是命名location。
location @name {…}

命名 location 仅对内部访问重定向,在进入一个 location 之前它会保留被请求的 URI部分。命名location只能够在server级别定义。

完整的样本配置文件

以下示例是一个样本配置文件,它包括了在本章讨论的各个不同方面。

user www;
worker_processes 12;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
    use /dev/poll;
    worker_connections 2048;
}

http {
    include  /opt/local/etc/nginx/mime.types;
    default_type application/octet-stream;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    server_names_hash_max_size 1024;
    server {
        listen 80;
        return 444;
    }
    server {
        listen 80;
        server_name www.example.com;
        location / {
             try_files $uri $uri/ @mongrel;
        }
        location @mongrel {
            proxy_pass http://127.0.0.1:8080;
        }
    }

}

Search

    微信好友

    博士的沙漏

    Table of Contents