0 Load Balancing
Load balancing, based on the existing network architecture, it provides a cheap and effective and transparent method 扩展网络设备
, and 服务器的带宽
, 增加吞吐量
, 加强网络数据处理能力
, 提高网络的灵活性和可用性
.
Load balancing, the English name for Load Balance
its meaning is assessed to be executed on multiple operating units, such as Web servers, FTP servers, business-critical application servers and other mission-critical servers, so as to work together to complete the task.
1 Nginx
Nginx is a high performance http server / reverse proxy server and e-mail (IMAP / POP3) proxy server. The program developed by Russian designer Igor Sysoev, the official test nginx 能够 支撑 5 万并发链接
, and cpu
, 内存
etc 资源消耗却非常低
, 运行非常稳定
.
Mainland China use nginx web site users are: Sina, Netease, Tencent and so on.
Nginx is a 轻量级和高性能的 Web 服务器
and 反向代理服务器
it has had a lot of very superior characteristics:
作为 Web 服务器
: Compared to Apache, Nginx use fewer resources to support more concurrent connections, reflecting higher efficiency, especially by this point the Nginx web hosting provider welcome. In response capable of supporting up to 50,000 concurrent connections, thanks to Nginx we chose epoll and kqueue as a development model for us.作为负载均衡服务器
: Nginx either directly support Rails and PHP internally, can also support as an HTTP proxy server in foreign service. Nginx is written in C, regardless of the system or CPU resource consumption efficiency is much better than Perlbal.作为邮件代理服务器
: Nginx is also a very good mail proxy server (one of the earliest purpose is the development of this product as a mail proxy server), Last.fm describes a successful and wonderful experience.
Nginx installation is very simple, very simple configuration file (also supports perl syntax), Bugs very little server: Nginx starts are particularly vulnerable, and can be done almost 7 * 24 uninterrupted operation, even if a few months do not need to run again start up. You also can 不间断服务的情况下进行软件版本的升级
.
Nginx seven general user load balancing, throughput certain restrictions.
To increase overall throughput will be introduced between the DNS and Nginx LVS (software load balancer), the F5 (hard load balancer) may do four load balancing, DNS resolution is first to LVS (F5), and LVS (F5 ) forwarded to Nginx, and then forwarded to the real server Nginx.
2 Windows
InstallationNginx
Official Website: http://nginx.org/
1, Downloads:
2. Unzip the downloaded file:
3, start:
- Method 1:
Double-click:nginx.exe
- Second way:
PS E:\dev\nginx-1.16.1> start nginx
If you start Nginx is successful, the error log should not be content, otherwise there will be error message:
4, stop
./nginx.exe -s stop
3 MAC
InstallationNginx
3.1 Installation Tools
homebrew (not used little friends can point link or to find out about their own Baidu)
Step 3.2
1, open the terminal, enter the command:
brew update
2, the terminal continues to execute the command:
查询要安装的软件是否存在
brew search nginx
3, where we perform multi-step "waste" command, but will help us the following configuration:
brew info nginx
operation result:
We can see, nginx
it has not been installed locally ( Not installed
), nginx
source ( From
), Docroot
by default /usr/local/var/www
, the /usr/local/etc/nginx/nginx.conf
default port is configured as the configuration file 8080
so that nginx
does not require additional run-time sudo
, nginx
will /usr/local/etc/nginx/servers/
load all files in a directory, and we can the simplest command ' nginx
to start nginx
.
4, started the installation:
brew install nginx
5, see the nginx installation directory (such as whether the said info):
open /usr/local/etc/nginx/
Successfully opened nginx
directory can also be seen as info
mentioned servers目录
, and nginx.conf
configuration files (will be used later in the configuration file). But we did not find nginx
to be installed where.
Terminal continues:其实这个才是nginx被安装到的目录
open /usr/local/Cellar/nginx
To see a currently installed nginx
version number for the file name of the folder, this is what we install nginx
the root of the matter. Enter the 1.17.1/bin
directory, you will see nginx
the start executable files.
Similarly, we in 1.17.1/
the directory can also see a name for the html
shortcut folder (for the time being so called it), enter the directory we will find out that it is pointing to /usr/local/var/www
the directory, we see this in the above info
information, there is mention to Dcroot
( )
6, starts
nginx
, the input terminal following command:
nginx
No error is the start successfully.
7, access authentication:
If the normal display:
The following are exceptions:
Open a browser to access localhost:8080
, here with some online tutorials will be somewhat different, under normal circumstances, this step will be able to see nginx
the welcome screen you, but bloggers have encountered the situation pit father (if you can normally see the nginx
welcome screen you can skip this step).
Surely everyone is familiar with this error, I will not do more to explain, then direct that reason (the reason is only speculation, because no way to verify): installed nginx
by default in html
(that is /usr/local/var/www
) directory to generate a welcome page file , the emergence of this situation is above the welcome page file is not generated (as to why no generation is not known, everything is speculation).
Before that solve this problem, we first look at nginx
the profile bar ( nginx.conf
):
cat /usr/local/etc/nginx/nginx.conf
Code displays the configuration file is:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
#侦听8080端口
listen 8080;
#定义使用 localhost访问
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#定义服务器的默认网站根目录位置
root html;
#定义首页索引文件的名称
index index.html index.htm;
}
...
...
... (注释代码太多,就不全部贴出来了)
include servers/*;
}
By profiles, we can see that the default web root directory html
(that is /usr/local/var/www
), and the default index file index.html
and index.htm
, at this to find the cause, and that we were the root of less Home index files, then manually create a it:
cd /usr/local/var/www/ //进入到www目录下
touch index.html //创建一个新的index.html文件
vim index.html //编辑该文件
The following code is written index.html
file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>title</title>
</head>
<body>
<div>
<h1>我的nginx欢迎页面</h1>
</div>
</body>
</html>
Press the esc
button to enter :wq
launch edit and save (this I believe we will, but still write on the obsessive-compulsive disorder).
Back to the browser ( localhost:8080
) Refresh:
4 Linux
InstallationNginx
Official Website: http://nginx.org/
4.1 Environment Preparation
CentOS7
Turn off the firewall:
systemctl stop firewalld # 临时关闭
systemctl disable firewalld # 禁止开机启动
1. Install
gcc
gcc-c++
yum install -y gcc gcc-c++
2. Install
wget
yum -y install wget
3. Install the
PCRE
library
yum install -y pcre pcre-devel
5. Install
SSL
library
yum install -y openssl openssl-devel
6. Install
zlib
Inventory
yum install -y zlib zlib-devel
4.2 Installation Nginx
cd /usr/local/
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar -zxvf nginx-1.16.1.tar.gz
cd nginx-1.16.1
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
make && make install
Note: compiled
nginx
in/usr/local/nginx/
the following
[root@docker nginx-1.16.1]# cd /usr/local/nginx/
[root@docker nginx]# ll
total 4
drwxr-xr-x. 2 root root 4096 Dec 7 23:59 conf # 配置文件
drwxr-xr-x. 2 root root 40 Dec 7 23:59 html # 根目录欢迎页
drwxr-xr-x. 2 root root 19 Dec 7 23:59 sbin # nginx 启动命令
[root@docker nginx]#
4.3 start Nginx
cd /usr/local/nginx/sbin/
./nginx
[root@docker ~]# cd /usr/local/nginx/sbin/
[root@docker sbin]# ./nginx
nginx: [emerg] mkdir() "/var/temp/nginx/client" failed (2: No such file or directory)
[root@docker sbin]#
Note: Before starting nginx, on top of the temporary file directory is specified
/var/temp/nginx
, it is necessary/var
under the creationtemp
andnginx
directory
[root@localhost sbin]# mkdir /var/temp/nginx/client -p
Start
nginx
:
./nginx
View process:
ps aux | grep nginx
[root@docker sbin]# ps aux | grep nginx
root 4382 0.0 0.0 20556 616 ? Ss 00:06 0:00 nginx: master process ./nginx
nobody 4383 0.0 0.0 21008 1344 ? S 00:06 0:00 nginx: worker process
root 4386 0.0 0.0 112708 976 pts/0 S+ 00:07 0:00 grep --color=auto nginx
[root@docker sbin]#
ps aux
: View all processesgrep nginx
: Filtering out nginx
Close way to kill the process
nginx
kill -9 4382
Close nginx:
[root @ localhost sbin] # ./nginx -s STOP
Recommended Use:
[root@localhost sbin]# ./nginx -s quit
Restart
nginx
:
1, after closing the first start;
2, refresh the configuration file:
./nginx -s reload
[root@localhost sbin]# ./nginx -s reload
access
nginx
The default is 80 port.
** Note: ** whether to turn off the firewall.
5 Nginx
scenarios
1HTTP 服务器
. Nginx is a http http service can provide services independently. You can do a static web page server.
2虚拟主机
. You can achieve a virtual multiple sites on a single server, such as personal Web site using a virtual machine.
3反向代理,负载均衡
. When the site's traffic reaches a certain level, a single server can not meet the user's request, the need to use multiple servers clusters can be used to make nginx reverse proxy. Circumstances and multiple servers can share the load average, the load should not be high downtime for a server and a server idle.
4, nginx can also be configured 安全管理
, for example, can be used to build Nginx API interface gateway to intercept each interface services.
6 Nginx
Directory Structure
Nginx-
|_ conf 配置目录
|_ contrib
|_ docs 文档目录
|_ logs 日志目录
|_ temp 临时文件目录
|_ html 静态页面目录
|_ nginx.exe 主程序
7 Nginx
static resources
Access to resources stored in static html page nginx