首页 / 数字家庭 / 正文
Beginner’s Guide

Time:2025年10月22日 Read:19 评论:0 作者:mlgyp_com

  Starting, Stopping, and Reloading Configuration

Beginner’s Guide
(图片来源网络,侵删)

  Configuration File’s Structure

  Serving Static Content

  Setting Up a Simple Proxy Server

  Setting Up FastCGI Proxying

  This guide gives a basic introduction to nginx and describes some

  simple tasks that can be done with it.

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

  If it is not, see the Installing nginx page.

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

  configuration, explains the structure

  of the configuration file and describes how to set up nginx

  to serve out static content, how to configure nginx as a proxy

  server, and how to connect it with a FastCGI application.

  nginx has one master process and several worker processes.

  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.

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

  The way nginx and its modules work is determined in the configuration file.

  By default, the configuration file is named

  and placed in the directory

  ,

  , or

  .

  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 parameter.

  Use the following syntax:

  Where signal may be one of the following:

  For example, to stop nginx processes with waiting for the worker processes

  to finish serving current requests, the following command can be executed:

  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:

  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 utility.

  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

  in the directory

  or

  .

  For example, if the master process ID is 1628, to send the QUIT signal

  resulting in nginx’s graceful shutdown, execute:

  For getting the list of all running nginx processes, the

  utility may be used, for example, in the following way:

  For more information on sending signals to nginx, see

  Controlling nginx.

  Configuration File’s Structure

  nginx consists of modules which are controlled by directives specified

  in the configuration file.

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

  Directives placed in the configuration file outside

  of any contexts are considered to be in the

  main context.

  The and directives

  reside in the context,

  in , and in

  .

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

  You will implement an example where, depending on the request,

  files will be served from different local directories:

  (which may contain HTML files) and

  (containing 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 directory and put an

  file with any text content into it and

  create the directory and place some

  images in it.

  Next, open the configuration file.

  The default configuration file already includes several examples of

  the block, mostly commented out.

  For now comment out all such blocks and start a new

  block:

  Generally, the configuration file may include several

  blocks

  distinguished by ports on which

  they listen to

  and by

  server names.

  Once nginx decides which processes a request,

  it tests the URI specified in the request’s header against the parameters of the

  directives defined inside the

  block.

  Add the following block to the

  block:

  This 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 ,

  to form the path to the requested file on the local file system.

  If there are several matching blocks nginx

  selects the one with the longest prefix.

  The block above provides the shortest

  prefix, of length one,

  and so only if all other

  blocks fail to provide a match, this block will be used.

  Next, add the second block:

  It will be a match for requests starting with

  ( also matches such requests,

  but has shorter prefix).

  The resulting configuration of the block should

  look like this:

  This is already a working configuration of a server that listens

  on the standard port 80 and is accessible on the local machine at

  .

  In response to requests with URIs starting with ,

  the server will send files from the directory.

  For example, in response to the

  request nginx will

  send the file.

  If such file does not exist, nginx will send a response

  indicating the 404 error.

  Requests with URIs not starting with will be

  mapped onto the directory.

  For example, in response to the

  request nginx will

  send the file.

  To apply the new configuration, start nginx if it is not yet started or

  send the signal to the nginx’s master process,

  by executing:

  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

  block to the nginx’s configuration file with the following contents:

  This will be a simple server that listens on the port 8080

  (previously, the directive has not been specified

  since the standard port 80 was used) and maps

  all requests to the directory on the local

  file system.

  Create this directory and put the file into it.

  Note that the directive is placed in the

  context.

  Such directive is used when the

  block selected for serving a request does not

  include its own directive.

  Next, use the server configuration from the previous section

  and modify it to make it a proxy server configuration.

  In the first 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 ):

  We will modify the second

  block, which currently maps requests with the

  prefix to the files under the directory,

  to make it match the requests of images with typical file extensions.

  The modified block looks like this:

  The parameter is a regular expression matching all URIs ending

  with , , or .

  A regular expression should be preceded with .

  The corresponding requests will be mapped to the

  directory.

  When nginx selects a block to serve a request

  it first checks location

  directives that specify prefixes, remembering

  with the longest prefix, and then checks regular expressions.

  If there is a match with a regular expression, nginx picks this

  or, otherwise, it picks the one remembered earlier.

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

  This server will filter requests ending with ,

  , or

  and map them to the directory (by adding URI to the

  directive’s parameter) and pass all other requests

  to the proxied server configured above.

  To apply new configuration, send the 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 directive,

  and fastcgi_param

  directives to set parameters passed to a FastCGI server.

  Suppose the FastCGI server is accessible on .

  Taking the proxy configuration from the previous section as a basis,

  replace the directive with the

  directive and change the parameter to

  .

  In PHP, the parameter is used for

  determining the script name, and the

  parameter is used to pass request parameters.

  The resulting configuration would be:

  This will set up a server that will route all requests except for

  requests for static images to the proxied server operating on

  through the FastCGI protocol.

标签:
关于我们
民贸生活服务网为您整理的分类信息及实用建议,涵盖生活各方面需求:【房产资讯】二手房市场:近期政策利好,首付比例下调至15%,建议关注地铁沿线学区房租房指南:9月开学季租金普遍上涨5-10%,建议提前1个月锁定房源新房动态:智能家居成标配,精装房交付标准升级【美食探店】• 必吃榜单:秋季限定蟹黄汤包上市(推荐老字号王记)• 网红打卡:新开业的星空主题餐厅,人均150元需提前3天预约• 省钱攻略:每周三半价日活动覆盖30+连锁餐厅【社区活动】✓ 9月8日社区文化节(免费参与包粽子比赛)✓ 每周六上午跳蚤市场(二手家具5折起)✓ 中秋晚会节目招募(报名截止9月15日)【婚恋服务】❤ 实名认证相亲会:9月14日七夕专场(需提供学历/房产证明)❤ 线上匹配:推荐知心红娘小程序,AI算法匹配度达85%【智能生活】• 家电换新:以旧换新补贴最高2000元(限9月30日前)• 黑科技:可语音控制的智能窗帘套装(安装仅需2小时)【金融理财】💰 大额存单利率:3年期2.85%(较上月上涨0.1%)💰 黄金投资:金价回调至480元/克,适合分批建仓【旅游推荐】✈ 错峰游:9月三亚机票价格回落40%🚗 周边游:新开通的环湖绿道适合骑行(租车点:社区服务中心)【生活服务】• 家政保洁:开学季大扫除预约已排至9月20日• 维修服务:空调清洗特惠99元(需提前3天预约)【二手市场】📌 急转:9成新婴儿床(原价1800元,现价600元)📌 求购:求购二手钢琴(预算3000元内)实用建议:房产交易前务必查验五证原件相亲时建议选择公共场所首次见面旅游旺季建议购买取消险(可退80%费用)需要更详细的某类信息,欢迎随时告知!
扫码关注
Copyright ©2004-2025 Comsenz Inc.Powered
蜀ICP备2025157516号-4