Beginner ' s Guide (Start Wizard)

Source: Internet
Author: User
Tags compact

This guide gives a basic introduction to Nginx and describes some simple tasks, the can is done with it.

This wizard gives a basic introduction to Nginx and some simple tasks of how to work with Nginx.

It is supposed this nginx is already installed on the reader's machine.

It is supported when Nginx has been installed on the machine

If It isn't, see the Installing Nginx page.

If not installed, view the installation Nginx page

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

This wizard describes how to start and winter solstice Ngin, as well as reload its configuration file

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

Explains the configuration file structure and describes how to install Nginx to send static content

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

How to configure Nginx as a proxy server and how to connect a fast graphical interface application

Nginx has one master process and several worker processes.

Nginx has a master process and multiple worker processes

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

The main purpose of the main process is to read and evaluate the configuration file, and to keep the worker process

Worker processes do actual processing of requests.

Process that the worker process makes the actual request

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

Nginx employs multiple event-based demonstrations and operational dependency mechanisms in order to efficiently allocate requests between worker processes

The number of the worker processes is defined in the configuration file and could be fixed for a given configuration or Automati Cally adjusted to the number of available CPUs cores (see worker_processes).

The number of worker processes is defined in the configuration file and is set to the given configuration file or automatically adapts to the number of active CPU cores

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

This nginx approach and its model work are determined in the configuration file. By default, the configuration file is named nginx.conf and placed in the/usr/local/nginx/conf,/etc/nginx,/usr/local/etc/nginx directory

Starting, stopping, and reloading Configuration

Start, stop, restart the configuration file

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:

In order to start Nginx, run the executable file. Once Nginx is activated, he can be controlled by a call to execute the-s parameter, using the following variable: Nginx-s signal

Signal

Where Signal May is one of the following: This signal is probably a below.

    • stop-fast shutdown stop-Quick close
    • quit-graceful shutdown exit-Graceful off
    • reload-reloading The configuration file restart--Restart the boot profile
    • reopen-reopening the log files re-opened--Reopen logical log

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

For example, in order to stop a process that is waiting to work to complete the current request, several current commands, such as the following command, are executed.

Nginx-s quit

This
command should is executed under the same user that started Nginx.

This command should be executed under the same user that started the Nginx

Changes made in the config file will not be applied changed profile won't be applied

Until the command to reload configuration was sent to Nginx or it was restarted. To reload configuration, execute:

Until the command restarts the configuration file, in order to restart the configuration file, do the following:

Nginx-s Reload

Once The master process receives the signal to reload configuration

Once the main process takes over to the signal or restarts the configuration file

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

It selects the syntax that is valid in the new configuration file and then applies the content that is mentioned in the configuration file for the application

If this is a success, if it is a successful

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

The main process starts a new process and sends a message to the old worker process

, requesting them to shut.

Ask them to shut down

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

Otherwise, the main thread rolls back the changes and continues to work with the old configuration file. The old work process,

Receiving a command to shut down, stop accepting new connections and continue to service current requests until all such r Equests is serviced.

Take over to the command to close, stop taking over the new connection and continue serving the current request to know that all requests are serviced

After this, the old worker processes exit.

After that, the old work process exits

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

A signal is sent to the Nginx process with Uninx tools such as the process of closing the unit

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 the nginx.pid directory /usr/local/nginx/logs or /var/run . For example, if the master process ID was 1628, to send the QUIT signal resulting in nginx ' s graceful shutdown, execute:

In such a case a signal is sent directly to a process with the process ID number. The process of this master process is written, by default, in the directory/usr/local/nginx/logs or/var/run directory under Nginx.pid. For example, if the main process ID is 1628, in order to send an gracefully closed exit signal result, do the following: Kill-s quit 1628

Kill-s QUIT 1628

For getting the list of all running nginx processes, the ps utility is used, for example, in the following-in-the-loop:

In order to get a list of the nginx processes that are running, PS-Yong may be used, for example, the bottom way Ps-ax |grep nginx

Ps-ax | grep nginx

For more information on sending signals to Nginx, see controlling Nginx.

For more information on sending the signal to Nginx, please see the control nginx

Configuration File ' s Structure

Structure of the configuration file

Nginx consists of modules which is controlled by directives specified in the configuration file.

Nginx module composition is specified in the command of the configuration file

Directives is divided into the simple directives and block directives.

Commands are divided into simple commands and module commands

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

A simple life consists of a space and a well-terminated argument and noun

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

A module command structure with the same simple instructions

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

This module command replaces the semicolon with {}

IF a block directive can has other directives inside braces, it is called a context (Examples:events, HTTP, server, and Location).

If a module directive can have other internal instructions, he is called content such as: Time, HTTP, service, and location

Directives placed in the configuration file outside of any contexts is considered to is in the main context.

A specific instruction outside of any content in the configuration file is considered to be the main content

The events http and directives reside in main the context, server in http , and location server .

Specific events and HTTP directives reside in primary content, servers, and service locations

The rest of a is # considered a comment.

The code behind the # is looked at is a comment

Serving Static Content

Service Static Content

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

An important Web service task is serving files

You'll implement an example where,

You need integration to implement such an example here

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

Depending on the request, the file is serviced by a different directory:/data/www (he may contain HTML files) and /data/images picture files

This would require editing of the configuration file and setting up of a server block inside the HTTP block with the Locati On blocks.

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

Next, open the configuration file. The default configuration file already includes several examples server of the 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 S Erver names. Once Nginx decides which server processes a request, it tests the URI specified in the request ' s header against the Paramet ERs 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 would be added to the path specified in the root directive, that's, to /data/www , to form the P Ath to the requested file on the local file system. If there is several matching location blocks Nginx selects the one with the longest prefix. locationThe block above provides the shortest prefix, of length one, and so if all other location blocks fail to provide a Match, this block would be used.

Next, add the second location block:

location/images/{    root/data;}

It is a match for requests starting /images/ location / with (also matches such requests, but have 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, listens on the standard port, and is accessible on the local MA Chine at http://localhost/ . In response to requests with URIs starting /images/ with, the server would send files from the /data/images directory. For example, in response to the http://localhost/images/example.png request Nginx send the /data/images/example.png file. If Such file does not exist, Nginx would send a response indicating the 404 error. Requests with URIs No starting with'll be /images/ mapped onto the /data/www directory. For example, in response to the http://localhost/some/example.html request Nginx send the /data/www/some/example.html file.

To apply the new configuration, start nginx if it isn't yet started or send the signal to the reload Nginx ' s master proces s, by executing:

Nginx-s Reload

in case something does isn't work as expected, you could try to find out of the reason in and files in the access.log error.log 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'll 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'll be is 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 Conte Nts

server {    listen 8080;    Root/data/up1;    Location/{    }}

This is a simple server that listens on the port 8080 (previously, the directive have not been specified since listen t He standard port is used) and maps all requests to the directory on the /data/up1 local file system. Create This directory and put the index.html file into it. Note that the root directive are 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's configuration from the previous sections 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'll modify the second location block, which currently maps requests with the /images/ prefix to the files under the /data/images di Rectory, to do 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 is preceded with ~ . The corresponding requests is 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 locationwith 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 would look like this:

server {location    /{        proxy_pass http://localhost:8080/;    }    Location ~ \. (gif|jpg|png) $ {        root/data/images;    }}

This server would filter requests ending with .gif , .jpg or and .png maps them to the /data/images directory (by adding URI To the directive's parameter) and pass all of the requests to the root proxied server configured above.

To apply a new configuration, send the signal to Nginx as described in the reload previous sections.

There is many more directives, the May is 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 programmin G languages such as PHP.

The most basic nginx configuration to work with a FastCGI server includes using the FASTCGI_PASS directive instead of T He 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 sections as a basis, replace the proxy_pass directive with th E fastcgi_pass directive and change the parameter to localhost:9000 . In PHP, the script_filename parameter are 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 would set up a server that would route all requests except for requests for static images to the proxied server Operati Ng on localhost:9000 through the FastCGI protocol.

Beginner ' s Guide (Start Wizard)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.