Beginner’s Guide(开始者向导)

This guide gives a basic introduction to nginx and describes some simple tasks that can be done with it.

这个向导给了对Nginx的基本介绍和如何用nginx工作的一些简单任务。

It is supposed that nginx is already installed on the reader’s machine.

它被支持当nginx已经安装到机器上

If it is not, see the Installing nginx page.

如果没有安装,查看安装nginx页面

This guide describes how to start and stop nginx, and reload its configuration,

这个向导描述怎么样启动和冬至Ngin,以及重新加载它的配置文件

explains the structure of the configuration file and describes how to set up nginx to serve out static content,

解释配置文件的结构和描述如何安装nginx到发送静态内容

how to configure nginx as a proxy server, and how to connect it with a FastCGI application.

如何配置nginx作为一个代理服务器,以及如何连接一个快速图形化界面应用

nginx has one master process and several worker processes.

nginx有一个主进程和多个工作进程

The main purpose of the master process is to read and evaluate configuration, and maintain worker processes.

主进程的主要目的是读取和评价配置文件,以及保持工作进程

Worker processes do actual processing of requests.

工作进程做实际请求的进程

nginx employs event-based model and OS-dependent mechanisms to efficiently distribute requests among worker processes.

nginx雇佣了多个事件基础示范和操作依赖机制为了有效分配工作进程之间的请求

The number of worker processes is defined in the configuration file and may be fixed for a given configuration or automatically adjusted to the number of available CPU cores (see worker_processes).

工作进程的数被定义在配置文件中以及被制定到给定的配置文件或者自动适应有效CPU核数的数量

The way nginx and its modules work is determined in the configuration file. By default, the configuration file is named nginx.conf and placed in the directory /usr/local/nginx/conf, /etc/nginx, or /usr/local/etc/nginx.

这种nginx的方式和它的模型工作在配置文件中被确定。默认情况下,配置文件被命名为nginx.conf并且放在/usr/local/nginx/conf,/etc/nginx,/usr/local/etc/nginx目录中

Starting, Stopping, and Reloading Configuration

开始、停止、重新启动配置文件

To start nginx, run the executable file. Once nginx is started, it can be controlled by invoking the executable with the -s parameter. Use the following syntax:

为了开始nginx,运行可执行文件。一旦nginx被启动,他可以被一个调用可执行的-s参数控制,使用下边的变量: nginx -s signal

nginx -s signal

Where signal may be one of the following: 这个signal可能是下边中的一个.

  • stop — fast shutdown停止-快速关闭
  • quit — graceful shutdown 退出-优雅的关闭
  • reload — reloading the configuration file重启--重启启动配置文件
  • reopen — reopening the log files 重打开--重新打开逻辑日志

For example, to stop nginx processes with waiting for the worker processes to finish serving current requests, the following command can be executed:

例如,为了停止正在等待工作的进程以完成当前的请求,几个当前命令,如下的命令被执行。

nginx -s quit

This command should be executed under the same user that started nginx.

这个命令应该在开始的nginx的同一个用户下被执行

Changes made in the configuration file will not be applied   改变的配置文件将不会被应用

until the command to reload configuration is sent to nginx or it is restarted. To reload configuration, execute:

直到命令重新启动配置文件,为了重新启动配置文件,执行如下:

nginx -s reload

Once the master process receives the signal to reload configuration

一旦主进程接手到信号或者重新启动配置文件

, it checks the syntax validity of the new configuration file and tries to apply the configuration provided in it.

它选择新的配置文件中有效的语法,然后适找应用在配置文件中提到的内容

If this is a success,如果这是一个成功

the master process starts new worker processes and sends messages to old worker processes

主进程开始一个新进程并且发送消息给老的工作进程

, requesting them to shut down.

请求他们关闭

Otherwise, the master process rolls back the changes and continues to work with the old configuration. Old worker processes,

否则,这个主线程回滚改变并且继续用老的配置文件工作。老的工作进程,

receiving a command to shut down, stop accepting new connections and continue to service current requests until all such requests are serviced.

接手到命令关闭,停止接手新的连接并且继续服务当前请求知道所有请求被服务

After that, the old worker processes exit.

在那以后,老的工作进程退出

A signal may also be sent to nginx processes with the help of Unix tools such as the kill utility.

一个信号被发送到nginx进程带着Uninx工具比如关闭单元的进程

In this case a signal is sent directly to a process with a given process ID. The process ID of the nginx master process is written, by default, to the nginx.pid in the directory /usr/local/nginx/logs or /var/run. For example, if the master process ID is 1628, to send the QUIT signal resulting in nginx’s graceful shutdown, execute:

在这样的情况下一个信号被直接发送一个带有进程ID号的进程。这个主进程的进程被写,默认,在目录/usr/local/nginx/logs或者/var/run目录下的nginx.pid里面。例如,如果主进程ID为1628,为了发送优雅关闭的退出信号结果,执行如下: kill -s quit 1628

kill -s QUIT 1628

For getting the list of all running nginx processes, the ps utility may be used, for example, in the following way:

为了获得正在运行的nginx进程的列表,PS小勇可能被用,例如,下边的方式  ps -ax |grep nginx

ps -ax | grep nginx

For more information on sending signals to nginx, see Controlling nginx.

为了更多发送信号到nginx的信息,请查看控制nginx

Configuration File’s Structure

配置文件的结构

nginx consists of modules which are controlled by directives specified in the configuration file.

nginx的模块构成被在配置文件的命令指定

Directives are divided into simple directives and block directives.

命令被分为简单命令和模块命令

A simple directive consists of the name and parameters separated by spaces and ends with a semicolon (;).

一个简单命由一个空间和一个分好结束的参数和名词的构成

A block directive has the same structure as a simple directive,

一个模块命令有相同简单指令的结构

but instead of the semicolon it ends with a set of additional instructions surrounded by braces ({ and }).

这个模块命令由{}替代分号

If a block directive can have other directives inside braces, it is called a context (examples: events, http, server, and location).

如果一个模块指令能够有其他内部指令,他被叫做内容例如:时间、http、服务和位置

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.

特定事件和http指令居住在主要内容、服务器和服务位置

The rest of a line after the # sign is considered a comment.

在#后边的代码被看着是注释

Serving Static Content

服务静态内容

An important web server task is serving out files (such as images or static HTML pages).

一个重要的web服务任务正在服务文件

You will implement an example where,

你需要集成实施这样的例子在这里

depending on the request, files will be served from different local directories: /data/www (which may contain HTML files) and /data/images (containing images).

依赖请求,文件被不同的目录所服务:/data/www(他可能包含Html 文件) 和/data/images图片文件

This will require editing of the configuration file and setting up of a server block inside the http block with two location blocks.

First, create the /data/www directory and put an index.html file with any text content into it and create the /data/images directory and place some images in it.

Next, open the configuration file. The default configuration file already includes several examples of the server block, mostly commented out. For now comment out all such blocks and start a new server block:

http {
    server {
    }
}

Generally, the configuration file may include several server blocks distinguished by ports on which they listen to and by server names. Once nginx decides which server processes a request, it tests the URI specified in the request’s header against the parameters of the location directives defined inside the server block.

Add the following location block to the server block:

location / {
    root /data/www;
}

This location block specifies the “/” prefix compared with the URI from the request. For matching requests, the URI will be added to the path specified in the root directive, that is, to /data/www, to form the path to the requested file on the local file system. If there are several matching location blocks nginx selects the one with the longest prefix. The location block above provides the shortest prefix, of length one, and so only if all other location blocks fail to provide a match, this block will be used.

Next, add the second location block:

location /images/ {
    root /data;
}

It will be a match for requests starting with /images/ (location / also matches such requests, but has shorter prefix).

The resulting configuration of the server block should look like this:

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

    location /images/ {
        root /data;
    }
}

This is already a working configuration of a server that listens on the standard port 80 and is accessible on the local machine at http://localhost/. In response to requests with URIs starting with /images/, the server will send files from the /data/images directory. For example, in response to the http://localhost/images/example.png request nginx will send the /data/images/example.png file. If such file does not exist, nginx will send a response indicating the 404 error. Requests with URIs not starting with /images/ will be mapped onto the /data/www directory. For example, in response to the http://localhost/some/example.html request nginx will send the /data/www/some/example.html file.

To apply the new configuration, start nginx if it is not yet started or send the reload signal to the nginx’s master process, by executing:

nginx -s reload

In case something does not work as expected, you may try to find out the reason in access.log and error.log files in the directory /usr/local/nginx/logs or /var/log/nginx.

Setting Up a Simple Proxy Server

One of the frequent uses of nginx is setting it up as a proxy server, which means a server that receives requests, passes them to the proxied servers, retrieves responses from them, and sends them to the clients.

We will configure a basic proxy server, which serves requests of images with files from the local directory and sends all other requests to a proxied server. In this example, both servers will be defined on a single nginx instance.

First, define the proxied server by adding one more server block to the nginx’s configuration file with the following 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.

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):

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 ~ \.(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.

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.

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.

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.

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:

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.

时间: 2024-10-18 00:40:47

Beginner’s Guide(开始者向导)的相关文章

(转)A Beginner's Guide To Understanding Convolutional Neural Networks Part 2

Adit Deshpande CS Undergrad at UCLA ('19) Blog About A Beginner's Guide To Understanding Convolutional Neural Networks Part 2 Introduction Link to Part 1 In this post, we’ll go into a lot more of the specifics of ConvNets. Disclaimer: Now, I do reali

(转)A Beginner's Guide To Understanding Convolutional Neural Networks

Adit Deshpande CS Undergrad at UCLA ('19) Blog About A Beginner's Guide To Understanding Convolutional Neural Networks Introduction Convolutional neural networks. Sounds like a weird combination of biology and math with a little CS sprinkled in, but

Beginner's Guide to Python-新手指导

Refer English Version: http://wiki.python.org/moin/BeginnersGuide New to programming? Python is free, and easy to learn if you know where to start! This guide will help you to get started quickly. 没玩过编程? Python语言是免费的,如果你知道从何处开始,它很容易上手! 本指南将帮助你快速入门. N

(Molehill) Game Programming Beginner's Guide 读书笔记 chapter2

-- chapter2. //23.-- Blueprint of a MolehillStage3D objects are not inside the DisplayList in Flash!As they are not DisplayObjects, you cannot apply filters or blendmodes. You cannot even put a Stage3D object over the top of other Flash 2D graphics!

Photography theory: a beginner's guide(telegraph.co.uk)

By Diane Smyth, Tim Clark, Rachel Segal Hamilton and Lewis Bush 11:00AM BST 09 Jun 2014 Have you read the Bible cover to cover? Probably not, but it's also fair to assume you know the basic plot, the central characters and a few choice quotes. This i

A Beginner’s Guide to Recurrent Networks and LSTMs

A Beginner’s Guide to Recurrent Networks and LSTMs Contents Feedforward Networks Recurrent Networks Backpropagation Through Time Vanishing and Exploding Gradients Long Short-Term Memory Units (LSTMs) Capturing Diverse Time Scales Code Sample & Commen

A Beginner's Guide To Understanding Convolutional Neural Networks(转)

A Beginner's Guide To Understanding Convolutional Neural Networks Introduction Convolutional neural networks. Sounds like a weird combination of biology and math with a little CS sprinkled in, but these networks have been some of the most influential

NHibernate 3 Beginner's Guide

前言 这一章是一个完整的NHibernate的Simple,原文中用Fluent NHibernate做映射,但我使用NHibernate3.2版本,所以3.2的Conformist代替Fluent NHibernate. 从这里我们将学习到使用NHibernate的一般步骤: 1.定义Model 2.映射Model 3.定义配置 4.1根据配置创建数据库 4.2根据配置BuildSessionFactory 5.用SessionFactory对象OpenSession 6.用session对象

A Beginner’s Guide to Eigenvectors, PCA, Covariance and Entropy

A Beginner’s Guide to Eigenvectors, PCA, Covariance and Entropy Content: Linear Transformations Principal Component Analysis (PCA) Covariance Matrix Change of Basis Entropy & Information Gain Resources This post introduces eigenvectors and their rela

这书真的不错--Spring MVC Beginner's Guide

五百多页,我干到三百多页了. 每个知识点都有说明,操作,解释. 学SPRING MVC,有它就够了. 遗憾的是,这个PDF的文档格式太稀松啦,且,无中文版~~~ 我都想作汉化翻译工作了...算了,忍住,先往前走走~~~~~~~ 这书真的不错--Spring MVC Beginner's Guide