nginx

来源:互联网 发布:windows任务管理工具 编辑:程序博客网 时间:2024/05/30 02:52

 

 

红帽linux

Nginx反向代理

实现高可用负载群集

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

广州中星信息技术服务股份有限公司

 

红帽产业事业部

 

 

 

 

 

 

 

 

 

 

 

        

Nginx反向实现高可用负载群集

 

实验环境:

主机

ip地址(内网IP)

外网IP

主机名

Nginx反向代理:

10.104.143.65

119.29.11.175

Node1

第一组:

10.186.54.177

139.199.201.48

Node2

第二组:

10.104.44.242

123.207.34.156

Node3

第三组:

10.135.189.227

119.29.202.95

Node4

第四组:

10.186.1.1

119.29.108.15

Node5

第五组:

10.135.123.162

139.199.231.226

Node6

第六组:

10.135.33.193

139.199.229.67

Node7

第七组:

10.186.0.219

123.207.70.155

Node8

第八组:

10.135.117.183

119.29.21.109

Node9

 

##这里的外网IP是最后我们访问的时候用到的,在大家的环境里面只要使用内网IP即可。

实验拓扑:

 

 

 

 

 

 

 

 

 

 

 

 

 

Setup1:

在各自分配的主机上操作:

 

1、  安装软件依赖包:

yum -y groupinstall "DevelopmentTools" "Server Platform Deveopment"

yum -y install openssl-devel pcre-devel

 

2、  在所有的主机上面都操作,下载nginx包:

cd /usr/local/src/

[root@node1 src]# yum install wget –y               ##安装wget工具

 

3、  下载nginx包:

[root@node1 src]# wget http://nginx.org/download/nginx-1.12.0.tar.gz

 

4、添加nginx运行的用户:

[root@node1 sbin]# useradd nginx

 

5解压nginx包,并且安装:

[root@node1 src]# tar zxvfnginx-1.12.0.tar.gz

[root@node1 src]# cd nginx-1.12.0/

 

6、安装nginx包:

[root@node1nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx--with-http_ssl_module --with-http_flv_module --with-http_stub_status_module--with-http_gzip_static_module --with-pcre

 

###编译安装

##rpm安装

 

www.baidu.com   ###www.hao123.com

 

[root@node1nginx-1.12.0]# make && make install

 

6、然后我们可以启动nginx:

[root@node2nginx]# cd /usr/local/nginx/sbin/

[root@node1sbin]# ./nginx

 

##假如已经存在进程:

[root@node2sbin]# ps -ef | grep nginx | awk '{print $2}' | xargs kill -9

kill: 向 9132 发送信号失败: 没有那个进程

 

 

7、我们可以查看我们的IP,然后访问web界面查看一下:

[root@node1sbin]# ifconfig

eth0:flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

        inet 119.29.11.175  netmask 255.255.192.0  broadcast 10.104.191.255

        ether 52:54:00:9b:4b:a0  txqueuelen 1000  (Ethernet)

        RX packets 113077  bytes 123521930 (117.7 MiB)

        RX errors 0  dropped 0 overruns 0  frame 0

       TX packets 45620  bytes 4211893(4.0 MiB)

        TX errors 0  dropped 0 overruns 0  carrier 0 collisions 0

8、查看一下是否监听到端口:

[root@node1sbin]# netstat -ntlp

ActiveInternet connections (only servers)

Proto Recv-QSend-Q Local Address           ForeignAddress         State       PID/Program name   

tcp        0     0 0.0.0.0:80             0.0.0.0:*               LISTEN     8035/nginx: master 

 

访问web:

 

##以上步骤在每台上面都执行:

 

Setup2:

 

1、  下载依赖包:

cd /usr/local/src

[root@node2 src]# yum -y installlibmcrypt-devel bzip2-devel gcc openssl-devel php-mcrypt libmcrypt libxml2-devellibjpeg-devel libpng-devel freetype-devel

 

2、 下载Php包:

[root@node2src]# wget http://cn2.php.net/distributions/php-5.5.38.tar.gz

 

3、 安装php包:

[root@node2src]# tar zxvf php-5.5.38.tar.gz

[root@node2src]# cd php-5.5.38/

[root@node2php-5.5.38]# ./configure --prefix=/usr/local/php --with-mysql=mysqlnd--with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-mbstring--with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib--with-libxml-dir=/usr --enable-xml --enable-sockets --with-mcrypt  --with-bz2 --enable-fpm --with-gd

 

要显示成这样才是正常的:

 

4、  开始编译安装:

[root@node2php-5.5.38]# make && make install

 

##注意要编译没有报错:

 

 

5、  开始一些配置步骤:

[root@node2php-5.5.38]# cp /usr/local/src/php-5.5.38/php.ini-production/usr/local/php/etc/php.ini

[root@node2php-5.5.38]#  mv/usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

 

6、 创建Php用户:

[root@node2php-5.5.38]# useradd -M -s /sbin/nologin php

 

7、 修改相关配置:

[root@node2php-5.5.38]# sed -i -e 's\;pid = run/php-fpm.pid\pid = run/php-fpm.pid\g' -e's\nobody\php\g' -e 's\listen = 127.0.0.1:9000\listen = 0.0.0.0:9000\g'/usr/local/php/etc/php-fpm.conf

[root@node2php-5.5.38]# sed -i 's\;daemonize = yes\daemonize = no\g'/usr/local/php/etc/php-fpm.conf

 

8、 启动php:

[root@node2php-5.5.38]# /usr/local/php/sbin/php-fpm &

 

9、 查看一下是否已经启动了php:

[root@node2php-5.5.38]# netstat -ntlp

ActiveInternet connections (only servers)

Proto Recv-QSend-Q Local Address           ForeignAddress         State       PID/Program name   

tcp        0     0 0.0.0.0:111            0.0.0.0:*               LISTEN      1/systemd          

tcp        0     0 0.0.0.0:80             0.0.0.0:*               LISTEN      31587/nginx: master

tcp        0     0 0.0.0.0:22             0.0.0.0:*               LISTEN      734/sshd           

tcp        0     0 0.0.0.0:9000            0.0.0.0:*               LISTEN      1114/php-fpm: maste

tcp6       0     0 :::111                 :::*                    LISTEN      14694/rpcbind      

tcp6       0     0 :::22                  :::*                    LISTEN      734/sshd

 

##注意是9000端口:

 

Setup3:

[root@node2 php-5.5.38]# yum installmariadb-server mariadb –y

 

2、启动mysql:

Systemctl start mariadb

 

2、设置root账户,用户名密码:

[root@node2 php-5.5.38]#mysql_secure_installation

Enter current password for root (enter fornone):

OK, successfully used password, movingon...

 

Setting the root password ensures thatnobody can log into the MariaDB

root user without the proper authorisation.

 

Set root password? [Y/n]Y

New password:

Re-enter new password:

Password updated successfully!

Reloading privilege tables..

 ...Success!

 

 

By default, a MariaDB installation has ananonymous user, allowing anyone

to log into MariaDB without having to havea user account created for

them. This is intended only for testing, and to make the installation

go a bit smoother.  You should remove them before moving into a

production environment.

 

Remove anonymous users?[Y/n]

 ...Success!

 

Normally, root should only be allowed toconnect from 'localhost'.  This

ensures that someone cannot guess at theroot password from the network.

 

Disallow root loginremotely? [Y/n]

 ...Success!

 

By default, MariaDB comes with a databasenamed 'test' that anyone can

access. This is also intended only for testing, and should be removed

before moving into a productionenvironment.

 

Remove test database andaccess to it? [Y/n]

 -Dropping test database...

 ...Success!

will take effect immediately.

 

Reload privilege tablesnow? [Y/n]

 ...Success!

Thanks for using MariaDB!

 

4、 创意一个wordpress数据库:

[root@node2php-5.5.38]# mysql -uroot -p123456 -e "create database wordpress"

[root@node2php-5.5.38]# mysql -uroot -p123456 -e "show databases"

+--------------------+

|Database           |

+--------------------+

|information_schema |

| mysql              |

|performance_schema |

|wordpress          |

+--------------------+

##验证拥有数据库了即可。

给数据库授权:

[root@node2conf]# mysql -uroot -p123456 -e "grant all privileges on *.* to 'root'@'%'identified by '123456'; FLUSH PRIVILEGES;"

Setup4:

在web01,web02上面设置nginx相关web相关服务,然后上传wordpress代码:

[root@node2 php-5.5.38]# mkdir /web

[root@node2 php-5.5.38]# cd /web/

[root@node2 web]# tar zxvf /usr/local/src/wordpress-4.7.2-zh_CN.tar.gz-C /web/

[root@node2 web]# cp –r wordpress/* /web

[root@node2 web]# cp wordpress/wp-adminwordpress/wp-content wordpress/wp-includes -r /web

 

2、上传配置文件到nginx目录,我已经配置好,从本机上传即可:

[root@node2 web]# cd /usr/local/nginx/conf/

[root@node2 web]# rm nginx.conf

[root@node2 web]# rz    ##上传文件到目录下

 

###我们看一下我们应该如何配置:#########

   location /{

           root   /web;

           index  index.php index.htmlindex.htm;

       }

      location ~ \.php$ {

           root           /web;

           fastcgi_pass   127.0.0.1:9000;

           fastcgi_index  index.php;

           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

           include        fastcgi_params;

        }

 

3、然后启动nginx:

[root@node2conf]# /usr/local/nginx/sbin/nginx

要是已经存在服务会提示:

nginx: [emerg]bind() to 0.0.0.0:80 failed (98: Address already in use)

就执行一下操作:

[root@node2conf]# /usr/local/nginx/sbin/nginx -s reload

 

5、 然后输入浏览器地址:IP/index.php即可:

 

 

自动跳到上面这个页面之后点击页面上的现在就开始:

 

##注意数据库主机:这里填写你们自己的iP地址:

这个提示是不能创建,我们直接复制,然后粘贴到文件下即可:

 

然后点击进行安装即可(站点标题,填网络,用户名填组名(如02、03)):

 

填写相关主题等等操作,然后点击进行安装,这样我们一个论坛就安装完成了:

到此我们安装一个论坛就已经完成了。

 

 

Setup5:

反向代理主机操作、参考即可,我已经配置好所有向后反向代理的主机:

 

最后我们在前端再加一个反向代理,让让问前端的服务器,跳转到后端的服务器:

我们上传:相关操作文件到自己服务器即可:

1、

[root@node1 conf]# cd /usr/local/nginx/conf

[root@node1 conf]#rm nginx.conf

[root@node1 conf]#rz   ##上传我们提供的反向代理文件。

 

2、vi修改文件的内容,改成你们自己的IP:

      upstream web_up{

       server node2  max_fails=3fail_timeout=20s weight=2;

       server node3  max_fails=3fail_timeout=20s weight=2;

       server node4  max_fails=3fail_timeout=20s weight=2;

       server node5  max_fails=3fail_timeout=20s weight=2;

       server node6  max_fails=3fail_timeout=20s weight=2;

       server node7  max_fails=3fail_timeout=20s weight=2;

       server node8  max_fails=3fail_timeout=20s weight=2;

       server node9  max_fails=3fail_timeout=20s weight=2;

     }

 

 

 

 

 

 

 

 

意思是访问这个IP的时候都是反向代理到后端的这些主机:

server {

        listen       80;

       server_name  www.wangluo.com;

 location / {

                proxy_pass http://web_up;

                proxy_set_header Host $host;

                proxy_set_header X-Real-IP$remote_addr;

                proxy_set_header X-Forwared-For$proxy_add_x_forwarded_for; 

 

       }

 

我们在windows绑定一个host域名,然后访问即可: