Nginx (one)

Source: Internet
Author: User
Tags aliases nginx server nginx reverse proxy microsoft iis domian

First, Introduction
Web server:
1.Apache: Still the most used Web server in the world. Advantages: Open source code, an open development team, support for cross-platform (can be run on almost all UNIX, Windows, Linux systems), portability, etc. is a heavyweight product that consumes more memory than other servers.
2.LIGHTTPD: The original source written by the Germans. Advantages: Low memory overhead, low CPU usage, good performance, rich modules and so on. Support FastCGI, CGI, Auth, output compression, url rewriting and alias, and other important functions. Lightweight Web server.
3.Tomcat: Open Source, Java-based Web application container that runs Servlets and JSP Web applications. Implementation of the Apache-jakarta specification, but the static file, high concurrency processing is weak.
4.IBM WebSphere: Java-based application environment to build, deploy, and manage the Internet and Web applications
5.Microsoft IIS: Microsoft, IIS provides a graphical interface for administrative tools that you can use to monitor configuration and control Internet services. You can only run on the Microsoft Windows platform, the Linux/unix platform, and you need to purchase a commercial Windows Server operating system.
6.Nginx: Russian Igor an HTTP and reverse proxy server written by Sysoev. In high concurrency, Nginx is a good alternative to Apache server, can support up to 50,000 concurrent connections, and memory, CPU and other system resource consumption is very low, running very stable.
The advantages of Nginx:
1. Support High concurrent connection: official test 50,000 concurrency, real production of the concurrency. This is due to nginx using the latest epoll (Linux2.6 kernel) and Kqueue (FreeBSD) network I/O models, while Apache uses the traditional select model with a more stable prefork mode of multi-process mode. You need to derive child processes frequently, consuming more server resources than Nginx. The Select Network I/O model used by Apache is very inefficient in handling a large number of connection reads and writes.
2. Low memory consumption: the nginx+php (FastCGI) server consumes less than 2GB of memory under 30,000 concurrent connections. The NGINX+PHP5 (FastCGI) server handles PHP dynamic program capabilities over "700 requests per second", equivalent to 60 million visits per day.
3. Low cost: Buy hardware load balancer switch requires a hundred thousand of or even hundreds of thousands of RMB, and Nginx is open source software, using the 2-clause bsd-like protocol, can be used free of charge, and can be used for commercial purposes.
4. Other Advantages:
A. configuration file is very simple
B. Support for rewrite rewrite rules
C. Built-in Health check function
D. Bandwidth Savings
E. High stability
F. Support for hot deployment
Summary: Nginx in reverse proxy, rewrite rules, stability, static file processing, memory consumption and other aspects have shown a strong advantage.


Second, the installation and configuration of Nginx server
Windows Edition:
After the official website is downloaded, unzip to a path that does not contain spaces, and execute nginx.exe. The Windows version of Nginx performance is much worse than the Linux/unix version of Nginx.
Linux version:
Install an Linux/unix operating system release (CentOS-6.3) before installing the Nginx server
CentOS is completely free and fixes many bugs in Redhat, but CentOS does not provide technical support to users and is not responsible for any business.
(1) Requirements for compiling nginx:
1> disk space: You need to ensure that there is more than 10MB of disk space.
2> gcc compiler and related tools: #yum-y install gcc gcc-c++ autoconf automake
3> module dependencies: Nginx modules require support from other third-party libraries: #yum-y install zlib zlib-devel OpenSSL openssl-devel pcre pcre-devel
(2) Nginx Download:
http://www.nginx.net/website
(3) Nginx Installation:
1> Windows installation: Download the compressed package, unzip it into a path that does not contain spaces, and then execute the DOS command in Start, run, cmd, to start Nginx:
D:
CD D:\nginx
Start Nginx
If you want to control the Nginx process, you can use the DOS command: nginx-s [Stop | quit | reopen | reload]
Installation in 2> Linux environment:
#tar-ZXVF nginx-0.x.xx.tar.gz
#cd nginx-0.x.xx
#./configure
#make
#sudo make Install
The default is installed in the/usr/local/nginx directory

(4) nginx start, stop
1> start
/usr/local/nginx/sbin/nginx-c/usr/local/nginx/conf/nginx.conf
The parameter "-C" specifies the path of the configuration file, if you do not add the "-C" parameter, Nginx will default loader installation directory in the Conf subdirectory of the nginx.conf file
2> stop
The Nginx stop method is many, generally by sending the system model to Nginx Master Process way to stop Nginx
Find Nginx main process number: #ps-ef | grep nginx
An Nginx process notes that "master process" is the primary, and its process number is the primary Procedure number
A. Gracefully stop nginx
#kill-quit nginx main process number
or
#kill-quit '/usr/local/webserve R/nginx/logs/nginx.pid '
B. Quick Stop Nginx
#kill-term nginx main process number
#kill-term '/usr/local/we Bserver/nginx/logs/nginx.pid '
or
#kill-int nginx main process number
#kill-int '/usr/local/webser Ver/nginx/logs/nginx.pid '
C. Force stop all Nginx processes
#pkill-9 Nginx
(5) Nginx Smooth Restart
If you change Nginx's Configuration file (nginx.conf) to restart Nginx can also be done by sending a system signal to the Nginx master process. Before restarting, verify that the Nginx configuration file syntax is correct:

#/usr/local/webserver/nginx/sbin/nginx-t-C
#/usr/local/webserver/nginx/conf/nginx.conf
If the configuration is incorrect, the first few lines of error are displayed, and two lines of prompt information are shown correctly.
Correct can be smooth restart Nginx:
#kill-hup nginx main process number
#kill-hup '/usr/local/webserver/nginx/logs/nginx.        PID '
(6) Nginx signal Control
supports the following signals:
Term,int   Quick close
QUIT    gracefully Close
HUP     Smooth restart
USR1    reopen log file for larger use when cutting logs
USR2     Smooth up Level executable program
WINCH gracefully shuts down worker processes
Iii. basic configuration and optimization of Nginx
1.Nginx Full Configuration example (page45~page47)
Nginx configuration file Default in Nginx program installation directory Conf Level two directory, the master configuration file is nginx.conf

2.Nginx Virtual Host Configuration
In the Nginx configuration file (nginx.conf), one of the most simplified virtual host configurations:
----------------------------------------------------------------------------
http
{
Server
{
Listen80default;
server_name_*;
Access_loglogs/default.access.log combined;
Location/{
Index index.html index.php;
Root/data0/htdocs/htdocs;
}
}
}
---------------------------------------------------------------------------
Like Apache, Nginx can also be configured with multiple types of virtual hosts: One is IP-based virtual host, the second is a domain-based virtual host, and the third is a port-based virtual host

I. Configuring an IP-based virtual host
Both the Linux and FreeBSD operating systems allow the addition of IP aliases. The concept behind IP aliases: You can bind multiple IP addresses on a physical network card. This enables multiple IP-based virtual hosts to be run on the same server that uses a single network card. On Linux, you can add IP aliases using the standard network Configuration tool (Ifconfig and route commands).
The local loopback represents the local virtual interface of the device, so the default is to be considered an interface that will never go down. Main role: ① test the local network configuration, can ping 127.0.0.1 description of the network card and IP protocol installation is not a problem; ② some server/client applications must invoke resources on the server at run time, typically specifying that the server's resources are installed on this machine. The server's IP address is set to 127.0.0.1 and can also be run

Adding two IP aliases to the ETH0 network card device 192.168.149.132 and 192.168.149.232 can be done through the ifconfig and route commands:
#/sbin/ifconfig eth0:1 192.168.149.132 broadcast 192.168.149.255 netmask 255.255.255.0 up
#/sbin/route add-host 192.168.149.132 Dev eth0:1

#/sbin/ifconfig eth0:2 192.168.149.232 broadcast 192.168.149.255 netmask 255.255.255.0 up
#/sbin/route add-host 192.168.149.232 Dev eth0:2
At this time execute the ifconfig command can see the ETH0 network card device bound two IP aliases, but the IP alias will disappear after reboot, you can also set the IP alias through the configuration file, add the above two ifconfig and the route command to the/etc/ rc.local file, let the system run automatically.

Next in the Nginx configuration file (nginx.conf), respectively, the 192.168.149.32, 192.168.149.132, 192.168.149.232 three IP configuration three pure static HTML support virtual host.
--------------------------------------------------------------------------
http
{
#第一个虚拟主机
Server
{
#监听的IP和端口
listen192.168.149.32:80;
#主机名称
server_name192.168.149.32;
#访问日志文件存放路径
Access_loglogs/server1.access.log combined;
Location/
{
#默认首页文件, from left to right
Index index.html index.htm;
#HTML网页存放目录
Root/data0/htdocs/server1;
}

}
}

http
{
#第二个虚拟主机
Server
{
#监听的IP和端口
listen192.168.149.132:80;
#主机名称
server_name192.168.149.132;
#访问日志文件存放路径
Access_loglogs/server2.access.log combined;
Location/
{
#默认首页文件, from left to right
Index index.html index.htm;
#HTML网页存放目录
Root/data0/htdocs/server2;
}

}
}

http
{
#第三个虚拟主机
Server
{
#监听的IP和端口
listen192.168.149.232:80;
#主机名称
server_name192.168.149.232;
#访问日志文件存放路径
Access_loglogs/server3.access.log combined;
Location/
{
#默认首页文件, from left to right
Index index.html index.htm;
#HTML网页存放目录
Root/data0/htdocs/server3;
}

}
}
---------------------------------------------------------------------------
From the above configuration can be seen, a section of server{....} is a virtual host, if you configure multiple virtual hosts to establish multi-segment server{....} Configuration can be very convenient. The listening port can also not write the IP address of the direct write port, configure it as "Listen 80", that is, listen to all the IP on the server 80 ports, you can distinguish between different virtual hosts through server_name.

Ii. Configuring a domain-based virtual host
Domain-based virtual hosts are the most common kind of virtual hosts. Just configure your DNS server, map each host name to the correct IP address, and then configure the Nginx server so that it can recognize different host names. Shared IP address, effectively resolves the problem of insufficient IP address. If no special requirements must be based on an IP virtual host, it is best to use a domain-based virtual host.

Configure three virtual hosts, the first aaa.domain.com: indicates that all access to aaa.domian.com is handled by it, and the second bbb.otherdomain.com: Indicates that all access to bbb.otherdomain.com is handled by it, the third www.domain.com, domain.com, and all except aaa.domian.com *. Access to the domian.com level two domain name is handled by it. Each virtual host's Web page files are stored in separate directories, and each virtual host uses a different log file to log access logs.  
----------------------------------------------------------------------------
http
{
#第一个虚拟主机
    Server
{
#监听端口
Linsten80;
    #主机名称
Server_nameaaa.domain.com;
    #访问日志文件存放路径
Access_loglogs/aaa.domain.com.access.log combined;
Location/
{
#默认首页文件, order left-to-right
index index.html index.htm
#HTML网页文件存放的目录
Root/dat a0/htdocs/aaa.domain.com;
}
}
}

HTTP
{
#第二个虚拟主机
server
{
#监听端口
Linsten80;
    #主机名称
Server_namebbb.otherdomain.com;
    #访问日志文件存放路径
Access_loglogs/bbb.otherdomain.com.access.log combined;
Location/
{
#默认首页文件, order left-to-right
index index.html index.htm
#HTML网页文件存放的目录
Root/dat a0/htdocs/bbb.otherdomain.com;
}
}
}

HTTP
{
#第三个虚拟主机
server
{
#监听端口
Linsten80;
    #主机名称
server_namewww.domain.com domain.com *.domian.com;
    #访问日志文件存放路径
Access_loglogs/www.domain.com.access.log combined;
Location/
{
#默认首页文件, order left-to-right
index index.html index.htm
#HTML网页文件存放的目录
Root/dat a0/htdocs/domain.com;
}
}
}
----------------------------------------------------------------------------------
3. Nginx log file configuration and cutting
Nginx access log file configuration
related to nginx log instructions are mainly two:
Log_format used to set the format of the log
Access_log used to specify the log file storage path, Format and cache size. The location of the
two instruction in the Nginx configuration file can be http{...} Can also be between virtual hosts, that is, server{....} Between the two curly braces.

I.log_format
Syntax format: Log_format name format [format ...]
Where name represents the format name of the definition, format represents the formatting style defined.
Log_format has a default, combined log format setting that is not required, equivalent to the Apache combined log format:
Log_format combined ' $remote _addr-$remote _user [$time _local] '
"$request" $status $body _bytes_sent '
' "$http _referer" "$http _user_agent";
You can also customize the logging format, note that the name names set by the Log_format directive cannot be duplicated in the Nginx configuration file.

Nginx server as a Web server, in the Load Balancer device, SQUID, Nginx reverse proxy, can not get to the client's real IP address. The reason is that after the reverse proxy, due to the addition of the middle tier between the client and the Web server, the Web server can not directly get the client IP, through the $REMOTE_ADDR variable to get the IP address of the reverse proxy server. However, in the HTTP header information of the forwarding request, the reverse proxy server can increase the X-forwarded-for information to record the original client IP address and the server address of the original client request.
Log_format combined ' $http _x_forwarded_for-$remote _user [$time _local] '
"$request" $status $body _bytes_sent '
' "$http _referer" "$http _user_agent";

Ii.access_log
Syntax format: access_log path [format [Buffer=size | off]]
Where path represents the log file's storage path, format means the name of the log format is set using the Log_format instruction, buffer=size indicates the size of the memory buffer set
① do not want to record the day: Access_log off;
② default combined format logging: Access_log/data1/logs/filename.log;
or Access_log/data1/logs/filename.log combined;
③ Custom Log format:
Log_format Mylogformat ' $remote _addr-$remote _user [$time _local] "$request" '
' $status $body _bytes_sent ' $http _referer '
' "$http _user_agent" $http _x_forwarded_for ';
Access_log/data1/logs/access.log Mylogformat buffer=32k;
④nginx 0.7.4 later, the log file path in the Access_log directive can contain variables:
access_log/data1/logs/$server _name.log combined;
Limitations of the existence of variables:
A. Users and groups set by the Nginx process must have permission to create files on that path
B. Buffers will not be used
C. For each log record, the log file opens the file, writes the log record, and then shuts down immediately.
Iii. the cutting of nginx log files
Nginx does not support using Cronolog to rotate logs like Apache, but can be used in the following ways:
#mv/data1/logs/access.log/data1/logs/20160715.log
#kill-USR1 Nginx Master Process number
First through the MV duplicate log file, and then send KILL-USR1 signal to the Nginx process number, let Nginx regenerate a new log file/data1/logs/access.log
If you want to cut logs every day, you also need to use crontab. Write a log of day-cutting logs, a shell script that stores logs by year, month directory
------------------------------------------------------------------------
vim/usr/local/webserver/nginx/sbin/cut_nginx_log.sh
=====================================================
#!/bin/bash
#这个脚本必须每天的00:00 Run

#Nginx日志文件的存放路径
Logs_path= "/data1/logs/"

Mkdir-p ${logs_path}$ (date-d "Yesterday" + "%Y")/$ (date-d "Yesterday" + "%m")/
MV ${logs_path}access.log ${logs_path}$ (date-d "Yesterday" + "%Y")/$ (date-d "Yesterday" + "%m")/access_$ (date-d "yester Day "+"%y%m%d "). Log
KILL-USR1 ' Cat/usr/local/webserver/nginx/nginx.pid '
=====================================================
Configure Crontab to execute this script at 00:00 every day:
Crontab-e
XX * * * */bin/bash/usr/local/webserver/nginx/sbin/cut_nginx_log.sh
-----------------------------------------------------------------------------------

Nginx (one)

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.