The installation of the nginx

Download: nginx.org/download/ng…

Setup preparation: Nginx relies on the PCRE library, so install pcRE first

yum install pcre pcre-devel

cd /usr/local/src/

Wget HTTP: / / http://nginx.org/download/nginx-1.4.2.tar.gz

The tar ZXVF nginx – 1.4.2. Tar. Gz

CD nginx – 1.4.2

./configure –prefix=/usr/local/nginx

make && make install

Activation:

CD /ulsr/local/nginx. The following four directories are displayed

. /

. Conf Configuration file

. HTML page file

. Logs log file

. Sbin Main binary program

[root@localhost nginx]# ./sbin/nginx

Nginx: [emerg] Bind () to 0.0.0.0:80 failed (98: Address already in use)

.

Nginx: [emerg] Bind () to 0.0.0.0:80 failed (98: Address already in use)

nginx: [emerg] still could not bind()

Port 80 cannot be bound because it is in use

(Sometimes apache, Nginx, etc., or more often apache shipped with the operating system and started as a service)

Solution: Stop the software or services that occupy port 80.


Nginx

Signal control of

TERM, INT

Quick shutdown

QUIT

Graceful Shutdown Graceful shutdown process

.
That is, wait for the request to end before closing

HUP

Configuration reload ,Start the new worker processes with

a new configuration Gracefully shutdown the old worker processes

Change the configuration file,

Smooth reread of configuration files

USR1

Reopen the log files Reopen the log

.
Log in monthly
/
Useful for daily segmentation

USR2

The Upgrade Executable on the fly

WINCH

Gracefully, the worker processes are disabled

(
Cooperate with
USR2
To upgrade
)

Specific syntax:

Kill – Signal option nginx main process number

Kill -HUP 4873

Kill – Signal control ‘cat/XXX /path/log/nginx.pid’

Kil; -USR1 `cat /xxx/path/log/nginx.pid`

Nginx configuration section

/ / the global area

Event {

// Nginx connection configuration

// How many connections can a word allow at one time

worker_connections 1024; // This means that a child process can connect up to 1024 connections.

HTTP {// This is the main segment for configuring the HTTP server

Server1 {// This is the virtual host segment

Location {// locate the Location of a particular path or file, such as the image directory, separately

} /// handle it separately, as in.php

}

Server2 {

}

}

example
Domain-based virtual hosting

server {

listen 80; # monitor port

server_name a.com; # monitor domain name

location / {

root /var/www/a.com; # Root location

index index.html;

}

}

Example 2: Port-based virtual host configuration

server {

listen 8080;

Server_name 192.168.1.204;

location / {

root /var/www/html8080;

index index.html;

}

}

Log management

If we look at the server segment of nginx, we can see information similar to the following

#access_log logs/host.access.log main;

This indicates that the server, whose access log file is logs/host.access.log, uses the “main” format.

In addition to the main format, you can customize other formats.

What is the main format?

log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘

#                       ‘$status $body_bytes_sent “$http_referer” ‘

#                       ‘”$http_user_agent” “$http_x_forwarded_for”‘;

The main format is that we define a log format and give it a name for easy reference.

In the example above, a log of type main, logging remote_addr…. Http_x_forwarded_for and other options.

1: Log format refers to which options are recorded

The default log format is main

log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘

                                                 ‘$status $body_bytes_sent “$http_referer” ‘

                                                 ‘”$http_user_agent” “$http_x_forwarded_for”‘;

As the default main log format, log several remote entries

Http-user-agent User agent/spider, the original IP address of the forwarded request

Http_x_forwarded_for: When passing through an agent, the agent adds your original IP address to this header and transfers your original IP address

2: Declare a unique log_format and name it

log_format mylog ‘$remote_addr- “$request” ‘

                                 ‘$status $body_bytes_sent “$http_referer” ‘

                                 ‘”$http_user_agent” “$http_x_forwarded_for”‘;

Below server/location, we can refer to mylog

In the server section, declare that Nginx allows different logs for different servers (some Web servers do not support this, such as Lighttp).

access_log logs/access_8080.log mylog;

Log format;

Practical application: Shell + scheduled task + Nginx signal management, complete log storage by date

Nginx generates a new log file under the control of the USR1 information number at 00:00:01 in the morning


Specific script:

#! /bin/bash

base_path=’/usr/local/nginx/logs’

log_path=$(date -d yesterday +”%Y%m”)

day=$(date -d yesterday +”%d”)

mkdir -p $base_path/$log_path

mv $base_path/access.log $base_path/$log_path/access_$day.log

#echo $base_path/$log_path/access_$day.log

kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`

Timing task

Crontab Edits scheduled tasks

01 00 * * * / XXX /path/ B.Sh 00:00 every day (recommended between 02 and 04, the system load is low)

The location of grammar

Location is a location based on the Uri.

In the configuration of the virtual host, is essential,location can put different parts of the site, positioning to different processing methods.

For example, how do I call the PHP interpreter when I encounter.php? Location is needed

The location of the grammar

location [=|~|~*|^~] patt {

}

You can have brackets without any arguments, which is called general matching or you can have arguments so big types can be divided into
Three kinds of

Location = patt {}

Location patt{} [generic matching]

Location ~ patt{} [regular match]


How does it work? :

First, see if there is an exact match, and if there is, stop the matching process.

location = patt {

config A

}

If $uri == patt, configA is used

location = / {

root /var/www/html/;

index index.htm index.html;

}

location / {

root /usr/local/nginx/html;

index index.html index.htm;

}

If you visit xxx.com/

The positioning process is

1: accurate match “/”, the index page is index.htm

HTM. The internal hop URI is /index.htm and the root directory is /usr/local/nginx/html

3: the final result, access to the/usr/local/nginx/HTML/index. The HTM

Re is also involved.

location / {

root /usr/local/nginx/html;

index index.html index.htm;

}

location ~ image {

root /var/www/image;

index index.html;

}

If we visit

Xx.com/image/logo….

In this case, / matches /image/logo.png

PNG matches “image/logo.png”. Who plays a role?

The result of the regular expression will be used.

The image will actually visit /var/www/image/logo.png

location / {

root /usr/local/nginx/html;

index index.html index.htm;

}

location /foo {

root /var/www/html;

index index.html;

}

We go to http://xxx.com/foo and for the uri “/foo”, the patt of both locations, matches them that is, “/” matches “/foo” from the left prefix, ‘/foo’ can also match the left prefix of ‘/foo’.

Rewrite, rewrite

The instruction used in rewriting

If (condition) {} sets the condition and overwrites it

Set # Set variable

Return # Returns the status code

Break out of rewrite

Rewrite # rewrite

If syntax format

If space (condition) {override mode}

What about the conditions?

Answer :3 ways to write

1: “=” to determine equality, used for string comparison

2: “~” matches (case sensitive re)~* case insensitive re

3: -f -d -e To check whether it is a file, directory, and exists.

Example:

$remote_addr = 192.168.1.100) {

return 403;

}

if ($http_user_agent ~ MSIE) {

rewrite ^.*$ /ie.htm;

break; #(do not break loop redirection)

}

if (! -e $document_root$fastcgi_script_name) {

rewrite ^.*$ /404.html break;

}

Note, here also add break,

In the HTTP/1.1 log, GET /dsafsd.html is the same as the 302 redirect.

So if you jump, the URL will change and you’ll rewrite the HTTP request 404.html again, but the internal rewrite, the context will still be the same, so fastcgi_script_name will still be dsafsd.html, so you’ll loop the redirect.

Set is used to set variables and can be used as a marker for reaching multiple criteria.

Achieve the effect of rewrite_condition under Apache

As follows:

Judge IE rewrite, and do not break; We use the set variable to do this

if ($http_user_agent ~* msie) {

set $isie 1;

}

if ($fastcgi_script_name = ie.html) {

set $isie 0;

}

if ($isie 1) {

rewrite ^.*$ ie.html;

}

Rewrite the grammar

Rewrite regular expressions redirect the position pattern

Goods-3.html —->Goods.php? goods_id=3

goods-([\d]+)\.html —> goods.php? goods_id =$1

location /ecshop {

index index.php;

rewrite goods-([\d]+)\.html$ /ecshop/goods.php? id=$1;

rewrite article-([\d]+)\.html$ /ecshop/article.php? id=$1;

rewrite category-(\d+)-b(\d+)\.html /ecshop/category.php? id=$1&brand=$2;

Rewrite the category – (\ d +) – b (\ d +) – min (\ d +) – Max (\ d +) – attr (/ \ \ d +) \. HTML/ecshop/category. PHP? id=$1&brand=$2&price_min=$3&price_max=$4&filter_attr=$5;

Rewrite the category – (\ d +) – b (\ d +) – min (\ d +) – Max (\ d +) – attr ([\ d + \]) – (\ d +) – (^ -) (+) – (+) [^ -] \. HTML/ecshop/category. PHP? id=$1&brand=$2&price_min=$3&price_max=$4&filter_attr=$5&page=$6&sort=$7&order=$8;

}

Note: When rewriting a URL, if the re contains “{}”, the re must be enclosed in double quotes

Nginx + PHP compilation

Apache typically starts PHP as a module of its own.

Nginx forwards HTTP request variables (such as GET, user_Agent, etc.) to PHP processes that communicate with Nginx. It’s called the FastCGI runtime.

Therefore, PHP compiled for Apache cannot be used with Nginx.

Note:
We compile PHP with the following functionality:

Connect to mysql, GD, TTF, and run as FPM (Fascgi)

./configure –prefix=/usr/local/fastphp \

–with-mysql=mysqlnd \

–enable-mysqlnd \

–with-gd \

–enable-gd-native-ttf \

–enable-gd-jis-conv

–enable-fpm

After compiling:

1:nginx+ PHP configuration is relatively simple, the core of the sentence —- forward the request information to the 9000 port PHP process, let PHP process specified directory PHP files.

Here’s an example:

location ~ \.php$ {

                               root html;

Fastcgi_pass 127.0.0.1:9000;

                               fastcgi_index index.php;

                               fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

                               include                 fastcgi_params;  

}

1: Encounters a PHP file,

2: Locate the root directory to HTML,

3: transfer the request context to the 9000 port PHP process,

$document_root$fastcgi_scriptName $document_root$fastcgi_scriptName


Web content compression coding and transmission speed optimization

Let’s look at the header for news.163.com

Request:

Accept-Encoding:gzip,deflate,sdch

Response:

Content-Encoding:gzip

Content-Length:36093

Save the page, observe, about 10W bytes, the actual transmission of 36093 bytes ——- is due to gzip compression.

Principle:

Browser — request —-> declare that gzip or deflate compression or compress or SDCH compression is acceptable from the point of view of HTTP protocol gzip deflate sdch Server –> response — Compress content in gzip mode —-> send to browser for browsing <—– decode gzip—– Receive gzip compressed content —- Calculate the bandwidth saved:

Let’s say news.163.com PV is 200 million

2*10^8 * 9*10^4 bytes

2*10^8 * 9 * 10^4 * 10^-9 = 12*K*G = 18T

The bandwidth savings are staggering

Common parameters for gzip configuration

gzip on|off; Whether to enable gzip

Gzip_buffers 32 4 k | 16 # 8 k buffer (compression in memory buffer pieces? How big is each block?

Gzip_comp_level [1-9] # recommend 6 compression levels (the higher the level, the less pressure, the more wasted CPU computing resources)

Gzip_disable # Re matching UA What Uri is not gzip

Gzip_min_length 200 # The minimum length of compression to start.

Gzip_http_version | 1.1 # to compress the HTTP protocol version 1.0 (can not set up, the present is almost all 1.1 protocol)

Gzip_proxied # Set up the requester proxy server, how to cache the content

Gzip_types text/plain Application/XML

Whether gzip_vary on | off # transmission gzip compression

Note:

Binary files such as pictures/MP3s do not need to be compressed

Because the compression rate is relatively small, such as 100->80 bytes, and compression is CPU intensive.

Smaller files do not need to be compressed, and Nginx’s cache Settings improve site performance. For images on websites, especially news sites, once published, changes are likely to be minimal. We hope that the image can be cached in the user’s browser after the user visits it once, and the cache time is relatively long. You can use the Expires setting in nginx. Nginx sets an expiration time, which is very simple, in the location or if section.

format

expires 30m;

expires 2h;

expires 30d;

(Note: The server date must be accurate. If the server date is behind the actual date, the cache may be invalid.)

In addition: 304 is also a good means of caching

The server responds to the file content with the etag tag and last_modified_since Etag,last_modified_since if the file has not changed, the server returns etag,last_modified_since

The browser knows nothing has changed and calls the local cache directly.

This process also calls the server, but very little is passed.

For short change cycle, such as static HTML, JS, CSS, more suitable to use this way nginx reverse proxy server + load balancing using Nginx reverse proxy and load balancing is very simple, support two uses 1 proxy, Upstream: In the reverse proxy case, nginx does not process PHP requests and forwards PHP requests to Apache.



This is not the legend of “static separation”, static separation is not a rigorous statement, called reverse proxy more standard.

If there are multiple servers on the reverse proxy backend, load balancing can naturally occur. However, how can proxy_pass point to multiple servers?

The algorithm to bind multiple servers together using upstream to specify a group name and proxy_pass to point to the default balancing for that group is simple: request one server at a time, in order of back-end servers.

There are also other load balancing algorithms, such as consistent hashing, that require the installation of a third party module.

Install ngx_HTTP_upstream_consistent_hash as an example.

The reverse proxy causes the IP address of the back-end server to be the IP address of the front-end server, not the real IP address of the client. How to do?

Assignments and lab sessions for next week

1: install a three-port Nginx server on the VM

2: install ecshop/discuz and do URL rewrite

3: use a server with three ports to achieve simple load balancing.

Next week’s experiment:

1: about 25-30 million pieces of enterprise name, phone number, and brief introduction

2: 4 servers, 2G/4 cores (1) 8G/ dual-core (3)

3: the target 3000 pv

Design: the server architecture diagram, including nginx/mysql/PHP/memcached distribution.

Each was asked to come up with a design

Nginx specific compression configuration

The following configurations are commonly used

gzip on|off

Gzip_buffers 4 k | 8 k buffer (and hard disk block)

Gzip_comp_level [1-9] recommends 6

Gzip_disable regular matches such as user-agent, which are not compressed for older browsers

gzip_min_length 200

Gzip_http_version | 1.1 1.0

Gzip_types text/plain, application/ XML (each MIME must be spaced, not comma)

gzip_vary on|off

Vary:


Vary is the basis used to flag the cache.

As seen above: this news page is made up of

Think about:

1: If 2 people, one browser supports gzip, the other browser does not support gzip2 simultaneously request the same page, chinaCache cache compressed, or uncompressed?

2: If 1 person requests the page again, does chinaCache return the cached content after compression, or the cached content before compression?

This is where Vary comes into play.

That is, the contents of the cache are affected by the Accept-encoding header.

So if the request does not support GZIP, the cache server will generate an ungzip copy of the content.

When requested, gZIP is supported and the cache server will generate a copy of the contents of GZIP.

The next time a request is made, the cache server will consider the accept-encoding factor of the client and return appropriate information about Nginx’s cache Settings for static files such as images and JS

Note: This cache refers to the cache of Pointers to the browser, not the data cache on the server side.

Location Expires directive

location ~ \.(jpg|jpeg|png|gif)$ {

expires 1d;

}

location ~ \.js$ {

expires 1h;

}

Set up and load the new configuration file, and using Firebug, you can see that the image content does not generate new requests because it takes advantage of the local cache.

Note: in large news stations, or article stations, the possibility of image changes is very small, it is recommended to do about 1 week of caching Js, CSS and other hour level cache.

You can also use last_Modified or etag instead of expires if the message is moving quickly. (Mainstream Web servers support both headers.)

The principle is:

Response: Calculates the signature, Etag, and last modification time of the response content

Request: Send the ETATg, if-Modified-since header.

After receiving the eTag, the server checks whether the etag is consistent and whether the last modification time is longer than if-modified-since. If the contents of the server are changed, 304 is returned, and the browser knows that the contents have not changed and directly uses the cache.

304 has one more request than the above expires directive, but fewer transfers than the 200 state.

Nginx reverse proxy and load balancing forward proxy



The reverse proxy



Specific load balancing mode

Note: Load balancing is A solution that can be implemented by DNS polling, as shown in the following figure. The DNS server allows multiple A records for A domain name, so when users access the DNS server, they generally return A recent resolution record by region. In this way, users in different parts of the country see 163’s home page from different servers.


Step 2: When parsing out the result, such as browser connection 60.217, there are N hosts behind this host, also want to do load balancing.

1: Load balancing on hardware, F5 big-IP, hardware load balancing (very expensive). Directly from the TCP/IP protocol, directly do packet transfer.

2: Software load balancing (LVS)

3: reverse proxy + load balancer

The reverse proxy connects to keep-alive


Nginx reverse proxy Settings

Example: Rewrite images to port 8080 (if you can write to port 8080, that means you can write to other independent servers)

location ~ \.(jpg|jpeg|png|gif)$ {

Proxy_pass http://192.168.1.204:8080;

expires 1d;

}

Clustering and balancing, how to write if there are many servers at the back end? And how to distribute tasks evenly

Combination of nginx and memcached

Usage: When nginx responds to a request, it requests memcached directly. If nothing is available, it calls back to the PHP page, queries the database, and writes memcached.

Cached (k/ V); cached (key >value); cached (key >value);

/abc.php? id=3

Ngx_http_php_memcache_standard_balancer-master is used as an example to install Nginx third-party modules

1: Decompress to path/ ngx_Module

Configuration:

./configure –prefix=/xxx/xxx –add_module=/path/ngx_module

Compile the installation

Make && make instal

Configure the memcache cluster

Upstream memserver {declare the memcached nodes to a group

hash_key $request_uri; // The basis for the hash calculation, which is based on the URI

server localhost:11211;

server localhost:11212;

}

In the Location

location / {

# root html;

set $memcached_key $uri;

memcached_pass memserver; // memserver is the name of the memcache node above error_page 404 / writem.php;

index index.php index.html index.htm;

}

Upstream {} while doing clustering and load balancing in Nginx: Upstream {}
The module adds multiple servers to a group and memcacheD_pass, fastcgi_pass, proxy_pass ==> upstream group

Default load balancing algorithm:

Set the counter and request N servers in turn.

You can install a third party pattern to hash urIs and so on.

Such as http://wiki.nginx.org/NginxHttpUpstreamConsistentHash

This module uses a consistent hash to request back-end nodes, and its algorithm is compatible with the consistent hash algorithm of the MEMcache module in PHP.

After installing the module:

Nginx. Conf

upstream memserver {

consistent_hash $request_uri;

server localhost:11211;

server localhost:11212;

}

In php.ini, do the following

memcache.hash_strategy = consistent

This way: Nginx and PHP can complete the clustering and load balancing algorithm for memcached.