Installation, deployment and load balancing configuration of centos6 Nginx

  

1. Introduction to Nginx

Nginx is a web server that can also be used for load balancing and reverse proxy use. At present, the most used is load balancing. I will not introduce the specific introduction. Baidu has a lot. Let’s go directly to the installation steps.

2. Nginx installation

1. Download Nginx and related components

The Linux system is Centos 6.5 64-bit, I directly switch to the root user to install

 

Enter the user directory to download the program

Download the relevant components, first check if they have been installed, check the installation directory

[root@localhost]whereis openssl 
[root@localhost]whereis nginx
[root@localhost]whereis zlib
[root@localhost]whereis pcre If
the path is empty, start the following installation
[root@localhost src]# wget http: // nginx.org /download/nginx-1.10.2.tar.gz Omit installation content...[root @ localhost src]# wget http://www.openssl.org/source/openssl-fips-2.0.10.tar.gz Omit installation content... [root@localhost src]# wget http: // zlib.net/zlib-1.2.11.tar.gz Omit installation content... [root@localhost src]# wget ftp : // ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz omit installation content...

 

Install the c++ compilation environment, if it is already installed, it can be skipped

[root@localhost src]# yum install gcc-c++
Omit installation content...
During the confirmation prompt, enter y and press Enter
Is this ok [y/N]:y
Omit installation content...

 

2. Install Nginx and related components

openssl installation

[root@localhost src]# tar zxvf openssl-fips-2.0.10.tar.gz
Omit installation content...
[root@localhost src]# cd openssl -fips- 2.0 . 10 
[root@localhost openssl -fips- 2.0 . 10 ]# ./config && make && make  install 
Omit installation content...

 

pcre install

[root@localhost src]# tar zxvf pcre-8.40.tar.gz
Omit installation content...
[root@localhost src]# cd pcre - 8.40 
[root@localhost pcre - 8.40 ]# ./configure && make && make  install 
Omit installation content...

 

zlib install

[root@localhost src]# tar zxvf zlib-1.2.11.tar.gz
Omit installation content...
[root@localhost src]# cd zlib - 1.2 . 11 
[root@localhost zlib - 1.2 . 11 ]# ./configure && make && make  install 
Omit installation content...

 

nginx installation

[root@localhost src]# tar zxvf nginx-1.10.2.tar.gz
Omit installation content...
[root@localhost src]# cd nginx - 1.10 . 2 
[root@localhost nginx - 1.10 . 2 ]# ./configure && make && make  install 
omit installation content...

 

3. Start Nginx

Find out where nginx is installed

 

Go to the nginx directory and start

An error is reported, error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory, solve it as follows

1. Use whereis libpcre.so.1 command to find where libpcre.so.1 is
 2. Use ln -s /usr/local/lib/libpcre.so. 1 / lib64 command to make a soft link
 3. Use sbin / nginx starts Nginx
 4. Check status with ps -aux | grep nginx

[root@localhost nginx]# whereis libpcre.so.1
[root@localhost nginx]# ln -s /usr/local/lib/libpcre.so.1 /lib64
[root@localhost nginx]# sbin/nginx
[root@localhost nginx]# ps -aux | grep nginx 

 

Basic operation of nginx

start up
[root@localhost ~]# /usr/local/nginx/sbin/nginx
stop / restart
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s stop(quit、reload)
command help
[root@localhost ~]# /usr/local/nginx/sbin/nginx -h
Validate the configuration file
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
configuration file
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

 

4. Simple configuration of Nginx

Open the nginx configuration file located in the conf folder in the nginx directory

A brief introduction to the syntax of vim

By default, after vim is opened, it cannot be entered, and you need to press keys to operate, as follows:
Start editing: Press "i" or "Insert" key
Exit editing: "Esc" key
Quit vim: ":q"
Save vim: ": w "
Save and exit vim: ":wq"
Exit vim without saving: ":q !"

 

Because I installed a dual environment, apache uses the default port 80, then I changed nginx to port 8080

 

 

 

 

To set up multiple sites, configure the following:

"#" stands for comments. The most important thing is that the server{} block represents each web site. For detailed configuration introduction, please refer to my other configuration article. Here we temporarily set up three sites

 

Use different ports 80, 81, 82 to save and exit and restart nginx

 

If the following error occurs:
nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
Solution:
[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
Use the nginx -c parameter to specify the location of the nginx.conf file
[root@localhost nginx]# cd logs/
[root@localhost logs]# ll
Total dosage 12
-rw-r--r-- 1 root root 1246 12月  9 18:10 access.log
-rw-r--r-- 1 root root  516 12月 10 15:39 error.log
-rw-r--r-- 1 root root    5 12月 10 15:38 nginx.pid
看nginx.pid文件已经有了。
 如下图:
那我们再试试:
/usr/local/nginx/sbin/nginx -s reload

 

然后打开zhiqiao.wang:8080

 

出现以上信息就代表着安装运行成功!

 

5、Nginx负载均衡配置

 Nginx集反向代理和负载均衡于一身,在配置文件中修改配就可以实现

首先我们打开配置文件

[root@localhost nginx]# vim conf/nginx.conf

 每一个server就是一个虚拟主机,我们有一个当作web服务器来使用

listen 80;代表监听80端口
server_name xxx.com;代表外网访问的域名
location / {};代表一个过滤器,/匹配所有请求,我们还可以根据自己的情况定义不同的过滤,比如对静态文件js、css、image制定专属过滤
root html;代表站点根目录
index index.html;代表默认主页

 

这样配置完毕我们输入域名就可以访问到该站点了。

负载均衡功能往往在接收到某个请求后分配到后端的多台服务器上,那我们就需要upstream{}块来配合使用

upstream xxx{};upstream模块是命名一个后端服务器组,组名必须为后端服务器站点域名,内部可以写多台服务器ip和port,还可以设置跳转规则及权重等等
ip_hash;代表使用ip地址方式分配跳转后端服务器,同一ip请求每次都会访问同一台后端服务器
server;代表后端服务器地址

server{};server模块依然是接收外部请求的部分
server_name;代表外网访问域名
location / {};同样代表过滤器,用于制定不同请求的不同操作
proxy_pass;代表后端服务器组名,此组名必须为后端服务器站点域名

server_name和upstream{}的组名可以不一致,server_name是外网访问接收请求的域名,upstream{}的组名是跳转后端服务器时站点访问的域名

 

配置一下Windows的host将我们要访问的域名aaa.test.com指向Linux

因为硬件有限,我是将Windows中的IIS作为Nginx的后端服务器,所以配置一下IIS的站点域名

打开cmd再ping一下aaa.test.com确实指向Linux系统了,再打开浏览器输入aaa.test.com会显示bbb这个站点就代表负载成功了。

Nginx的负载功能就配置完成了,这只是简单设置了一下,生产环境中还有很多详细的调整,后续再逐渐增加,本人水平有限,如有不对之处还望指导谢谢。

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325309890&siteId=291194637