nginx实用指南二: 快速配置运行

一 前言

本篇介绍一些nginx的基础用法,以及使用nginx来完成的一些简单任务。
如果您还没有安装nginx,请查看本系列第一篇安装nginx。本文将介绍如何开启、停止nginx、以及重载nginx配置。
解析了配置文件的结构,并描述了如何在静态环境下安装nginx、如何将nginx配置为一个代理服务器、如何将它和一个FastCGI应用程序联系起来。
nginx由一个master进程和几个worker进程构成,主进程的主要目的是读取并评估配置,维护工作进程。由工作进程处理实际的请求。nginx使用事件驱动模型和OS-dependent机理来有效地在workder进程之间分发请求。工作进程的数量由配置所定义,如果不配置的话,默认被适配为CPU的核心数。
nginx已经各模块的工作由配置文件所指定, 其默认配置文件在/usr/local/nginx/conf, /etc/nginx, or /usr/local/etc/nginx。

二 开启 停止 重载配置

运行nginx可执行文件,即可开启nginx,nginx运行起来后,可以通过nginx -s 选项再次触发。
语法如下:
nginx -s signal
Where signal may be one of the following:

signal含义
stopfast shutdown
quitgraceful shutdown
reloadreloading the configuration file
reopenreopening the log files

例如,为了阻止nginx进程继续等待工作进程完成当前的请求,可使用如下命令:
nginx -s quit
这条命令应该在启动nginx的用户下运行。
在修改完nginx.conf配置文件后,需要执行一条命令来让配置生效:
nginx -s reload
一旦主进程收到重新加载进程配置的消息,它会先检测配置的合法性,成功即可应用所提供的配置,接着主进程开启新的worker进程,并发送消息给旧的worker进程,以使旧的workder进程关闭。如果配置失败的话,主进程将回滚到之前的状态并继续和旧的worker进程一起协作。
旧的工作进程在收到shut down命令时,停止接收所有新的连接,并继续处理当前的请求,直到处理完之后才会shut down退出。
Unix工具,例如kill也可以发送signal给nginx进程,在这种情况下,signal被直接发送到指定进程(通过执行nginx主进程ID),缺省情况下,这个pid保存在nginx.pid文件下, 例如,如果主进程pid=1628, 为了让nginx安全关闭,可以使用QUIT signal:
kill -s QUIT 1628
可以通过以下命令来查看nginx是否仍在运行:
ps -ax | grep nginx

三 Configuration File’s Structure

组成nginx的各个子模块,可通过配置文件中的指令进行控制,指令会划分为简单指令和块指令。
一个简单指令由name和paramaters组成,以空格分开,以分号结尾。
块指令的结构和简单指令结构大体相同,不同之处在于,块指令结构并不以分号结尾,而是以一串用[{}]括号包含起来的附加指令结尾。如果在一个块指令内部包含有其他指令,那么这些内部指令被称作context(例如events,http,server,location等指令)。
在任何context之外的指令,被称作main context:
Directives placed in the configuration file outside of any contexts are considered to be in the main context. The events and http directives reside in the main context, server in http, and location in server.

另外,注释行以’#’开头

3.1 Serving Static Content

web server的一个很重要的功能就是提供文件给http协议,例如图片,html页面。你可以实现一个示例,它取决于http请求,文件可以存放于各个不同的本地目录中:/data/www(可以包括html页面文件),或者/data/images(存放图片)。这些都需要修改配置文件,需要定义一个在http block里面的server block,包括两个location blocks.

首先,你需要创建一个/data/www目录,在该目录下编辑一个index.html文件;
再创建一个/data/images目录,然后随便放一些图片在该目录下。
接下来,打开配置文件,缺省的配置文件已经包含了几个server block的例子,大部分都注释掉了,现在请输入新的server block:

http {
        server {
    }
}

一般来说,配置文件可以包含多个server blocks,它们之间以监听的不同端口号、不同的server名来qufen区分。当nginx决定选择哪个server block来处理请求时,它会根据server block里的location指令所携带的参数来test请求头所指定的URI。
我们先添加一个location block 到 the server block中:

location / {
    root /data/www;
}

这个location block指定”/”前缀同请求者的URI进行比较。为了匹配请求,URI将会被添加到被root指令所指示的路径中(/data/www)。如果有好几个匹配项,nginx将选择那个前缀最长的进行处理。
在上面的例子中,location block提供了最短的前缀(长度为1),所以只有其它所有的location都不匹配时,这个location block才会使用到。

接着,添加第二个location block:

location /images/ {
    root /data;
}

它将匹配以/images/开始的请求(当然,/images/… URI也会匹配到 第一个location block,不过如前面所说,它会选择匹配前缀最长的那个location)
最后的配置文件看起来是这样子的:

server {
    location / {
        root /data/www;
    }

    location /images/ {
        root /data;
    }
}

上述配置已经是一个可以正常工作的nginx配置了,它监听标准的80端口,可以直接使用http://localhost进行访问。作为对 以”/images/” 开头的URI 的响应,server将直接在/data/images下面找到对应的文件发送回去,如果文件不存在,nginx将返回404错误。
URI请求若没有以/images开始,将会map到”/”,也就是map到/data/www目录,例如请求URI为:http://localhost/some/example.html,则nginx将发送的文件为:/data/www/some/example.html

如果nginx正在运行的话,修改nginx.conf后,为了让配置生效,需要执行:
nginx -s reload
万一在实际运行和预想中的不一样,可以尝试查看access.log和error.log查找原因(/usr/local/nginx/logs or /var/log/nginx.)

3.2 Setting Up a Simple Proxy Server

有关nginx的一个最常见的用法是,将其设定为一个代理服务器,让它接收http消息,将其发送给真实的服务器,然后获取真实服务器的应答消息,最后将应答消息回传给客户端。

下面的例子,将配置一个基础的代理服务器。用来响应“获取本地目录图片”的请求,并将所有的请求发送给被代理的server。在这个例子中,代理server和被代理server都被定义为nginx的简单实例。

首先,为了定义代理服务器,需要在nginx.conf中再添加一个server block,包括这样的contents:

server {
    listen 8080;
    root /data/up1;

    location / {
    }
}

This will be a simple server that listens on the port 8080 (previously, the listen directive has not been specified since the standard port 80 was used) and maps all requests to the /data/up1 directory on the local file system. Create this directory and put the index.html file into it. Note that the root directive is placed in the server context. Such root directive is used when the location block selected for serving a request does not include own root directive.
这里表示的含义是,监听的是8080端口(如果不定义的话,将默认监听80端口)。将所有的请求映射到本地目录/data/up1下。创建这个目录,并将index.html文件发在该目录下。注意,root指令被放置于server context内。当被选中的location block不包括自己的root指令时,在server context内的指令将会被用到。

Next, use the server configuration from the previous section and modify it to make it a proxy server configuration. In the first location block, put the proxy_pass directive with the protocol, name and port of the proxied server specified in the parameter (in our case, it is http://localhost:8080):
接下来,进一步修改配置文件,以使得它成为一个代理服务器,在第一个location block中,使用带协议的proxy_pass指令:

server {
    location / {
        proxy_pass http://localhost:8080;
    }

    location /images/ {
        root /data;
    }
}

We will modify the second location block, which currently maps requests with the /images/ prefix to the files under the /data/images directory, to make it match the requests of images with typical file extensions. The modified location block looks like this:
我们将修改第二个location block,当前它映射/image/前缀到 /data/images目录下,为了匹配特定的图片,修改后的block看起来是这样的:

location ~ \.(gif|jpg|png)$ {
    root /data/images;
}

The parameter is a regular expression matching all URIs ending with .gif, .jpg, or .png. A regular expression should be preceded with ~. The corresponding requests will be mapped to the /data/images directory.
location后面的参数是一个正则表达式,它匹配所有以.gif, jpg, png为后缀名的图片,一个正则表达式应该是”~”打头,那么相应的请求将被映射到/data/images目录下。

When nginx selects a location block to serve a request it first checks location directives that specify prefixes, remembering location with the longest prefix, and then checks regular expressions. If there is a match with a regular expression, nginx picks this location or, otherwise, it picks the one remembered earlier.
nginx在选择一个location block的时候,先检查location指令所指定的前缀,记住我们在前面讲过的最长匹配原则,然后再检查正则表达式,如果正则表达式匹配上了,那么就使用它,否则就使用之前匹配到的最长的那个prefix。

The resulting configuration of a proxy server will look like this:

server {
    location / {
        proxy_pass http://localhost:8080/;
    }

    location ~ \.(gif|jpg|png)$ {
        root /data/images;
    }
}

This server will filter requests ending with .gif, .jpg, or .png and map them to the /data/images directory (by adding URI to the root directive’s parameter) and pass all other requests to the proxied server configured above.

To apply new configuration, send the reload signal to nginx as described in the previous sections.
There are many more directives that may be used to further configure a proxy connection.

3.3 Setting Up FastCGI Proxying

nginx can be used to route requests to FastCGI servers which run applications built with various frameworks and programming languages such as PHP.
FastCGI服务器应该用来构建服务端应用程序,nginx可被用来route请求到FastCGI服务器,

The most basic nginx configuration to work with a FastCGI server includes using the fastcgi_pass directive instead of the proxy_pass directive, and fastcgi_param directives to set parameters passed to a FastCGI server. Suppose the FastCGI server is accessible on localhost:9000. Taking the proxy configuration from the previous section as a basis, replace the proxy_pass directive with the fastcgi_pass directive and change the parameter to localhost:9000. In PHP, the SCRIPT_FILENAME parameter is used for determining the script name, and the QUERY_STRING parameter is used to pass request parameters. The resulting configuration would be:
使用nginx配置FastCGI服务器的话,最基础的配置是使用fastcgi_pass指令,而不是proxy_pass指令,fastcgi_param指令用于设定传递给FastCGI服务器的参数,假定FastCGI server在localhost:9000。在3.2的配置文件的基础上,将proxy_pass指令替换为fastcgi_pass指令,并将参数修改为localhost:9000。在php中,参数SCRIPT_FILENAME被用来决定脚本名,参数QUERY_STRING被用来传递请求参数,最后的配置文件如下:

server {
    location / {
        fastcgi_pass  localhost:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param QUERY_STRING    $query_string;
    }

    location ~ \.(gif|jpg|png)$ {
        root /data/images;
    }
}

This will set up a server that will route all requests except for requests for static images to the proxied server operating on localhost:9000 through the FastCGI protocol.
如上配置,它将设定一个服务器,该服务器会通过FastCGI协议,route所有除了图片之外的请求到被代理的服务器localhost:9000。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值