nginx+apache动静分离/负载均衡

Posted 夕颜@伤雨痕

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx+apache动静分离/负载均衡相关的知识,希望对你有一定的参考价值。

【主从】

mysql-Master】

log-bin=mysql-bin
server-id = 1

MariaDB [(none)]> grant replication slave on *.* to \'slave\'@\'192.168.2.158\' identified by \'123.com\';

MariaDB [(none)]> flush privileges;

MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 551 | | |
+------------------+----------+--------------+------------------+

【Mysql-slave】

server-id = 2

MariaDB [(none)]> slave stop;
Query OK, 0 rows affected, 1 warning (0.00 sec)

MariaDB [(none)]> change master to
-> master_host=\'192.168.2.157\',
-> master_user=\'slave\',
-> master_password=\'123.com\',
-> master_log_file=\'mysql-bin.000001\',
-> master_log_pos=551;
Query OK, 0 rows affected (0.09 sec)

MariaDB [(none)]> slave start;
Query OK, 0 rows affected (0.00 sec)

 

主从成功~

【两台服务器站点虚拟主机配置】

# vim /etc/httpd/conf/httpd.conf

ServerRoot "/etc/httpd"
Listen 80
AddType     application/x-httpd-php  .php
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost
<Directory />
    AllowOverride none
    Require all denied
</Directory>
DocumentRoot "/var/www/html"
<Directory "/var/www">
    AllowOverride None
    Require all granted
</Directory>
<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
<IfModule dir_module>
    DirectoryIndex index.php index.html
</IfModule>
<Files ".ht*">
    Require all denied
</Files>
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
    LogFormat "%h %l %u %t \\"%r\\" %>s %b \\"%{Referer}i\\" \\"%{User-Agent}i\\"" combined
    LogFormat "%h %l %u %t \\"%r\\" %>s %b" common
    <IfModule logio_module>
      LogFormat "%h %l %u %t \\"%r\\" %>s %b \\"%{Referer}i\\" \\"%{User-Agent}i\\" %I %O" combinedio
    </IfModule>
    CustomLog "logs/access_log" combined
</IfModule>
<IfModule alias_module>
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>
<IfModule mime_module>
    TypesConfig /etc/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
    MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on
IncludeOptional conf.d/*.conf
Include  conf/vhosts.conf

 

mkdir -p  /var/www/html/dz.bxy.net/

mkdir -p  /var/www/html/wps.bxy.net/

#vim  /etc/httpd/conf/vhosts.conf 

<VirtualHost *:80>
    ServerAdmin admin@amsilence.com
    DocumentRoot "/var/www/html/dz.bxy.net/"
    ServerName dz.bxy.net
    ErrorLog "/etc/httpd/logs/www-error_log"
    CustomLog "/etc/httpd/logs/www-access_log" common
</VirtualHost>
NameVirtualHost *:80
<VirtualHost *:80>
    ServerAdmin admin@amsilence.com
    DocumentRoot "/var/www/html/wps.bxy.net"
    ServerName wps.bxy.net
    ErrorLog "/etc/httpd/logs/www-error_log"
    CustomLog "/etc/httpd/logs/www-access_log" common
</VirtualHost>

 

192.168.2.157站点

【Discuz和workpress部署】

# cd /var/www/html/

#wget http://download.comsenz.com/DiscuzX/3.1/Discuz_X3.1_SC_UTF8.zip

#wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz

#unzip Discuz_X3.1_SC_UTF8.zip -d Discuz/

[root@localhost Discuz]# mv upload/* .
[root@localhost Discuz]# chmod o+x uc_server/ config/ uc_client/

MariaDB [(none)]> create database DiscuzDB charset=utf8;

MariaDB [(none)]> grant all on DiscuzDB.* to \'discuz\'@\'%\' identified by \'123.com\';

MariaDB [(none)]> flush privileges

hosts解析:因为要通过域名连接数据库地址,那么我们需要将数据库的IP地址和自定义域名解析

 

 

【wordpress网站部署】

#tar zxvf wordpress-4.9.4-zh_CN.tar.gz -C wordpress/

【数据库授权】
MariaDB [(none)]> create database wpsDB charset=utf8;

MariaDB [(none)]> grant all on *.* to \'wpsuser\'@\'%\' identified by \'123.com\';

MariaDB [(none)]> flush privileges;

host解析:我们将wordpress域名解析到2.157主库ip地址跟后面即可

 

[root@lamp-master wps.bxy.net]# vim wp-config.php

<?php
/**
 * WordPress基础配置文件。
 *
 * 这个文件被安装程序用于自动生成wp-config.php配置文件,
 * 您可以不使用网站,您需要手动复制这个文件,
 * 并重命名为“wp-config.php”,然后填入相关信息。
 *
 * 本文件包含以下配置选项:
 *
 * * MySQL设置
 * * 密钥
 * * 数据库表名前缀
 * * ABSPATH
 *
 * @link https://codex.wordpress.org/zh-cn:%E7%BC%96%E8%BE%91_wp-config.php
 *
 * @package WordPress
 */

// ** MySQL 设置 - 具体信息来自您正在使用的主机 ** //
/** WordPress数据库的名称 */
define(\'DB_NAME\', \'wpsDB\');

/** MySQL数据库用户名 */
define(\'DB_USER\', \'wpsuser\');

/** MySQL数据库密码 */
define(\'DB_PASSWORD\', \'123.com\');

/** MySQL主机 */
define(\'DB_HOST\', \'wps.mysql.net\');

/** 创建数据表时默认的文字编码 */
define(\'DB_CHARSET\', \'utf8mb4\');

/** 数据库整理类型。如不确定请勿更改 */
define(\'DB_COLLATE\', \'\');

/**#@+
 * 身份认证密钥与盐。
 *
 * 修改为任意独一无二的字串!
 * 或者直接访问{@link https://api.wordpress.org/secret-key/1.1/salt/
 * WordPress.org密钥生成服务}
 * 任何修改都会导致所有cookies失效,所有用户将必须重新登录。
 *
 * @since 2.6.0
 */
define(\'AUTH_KEY\',         \'kXeW~n6gq[_u;k=49+z=Am9a15F@D)|?8~SKt(A0K,^!U/u4yOvb ]6J._LJ!KPg\');
define(\'SECURE_AUTH_KEY\',  \'ui|@1SGGC!Wpi)lMdBuh^~.ueevR$ar]B_X8#|&&U.z4YGH02RVSK(`8X2hho)Xm\');
define(\'LOGGED_IN_KEY\',    \'cd)V9bEPLk4|Q=Z<Ri_Sfp*nvg/Ui+BgHXGL./o@AUP!dMRE=PYV7nJ_&ijK*#y~\');
define(\'NONCE_KEY\',        \':-sd O<^pJvN. ;:SuxaSx5kd%W`.Y.z_xN6Nc@zhz&d_^SM*X4(K@&;aQo2wAko\');
define(\'AUTH_SALT\',        \'O[@Bx|zqp@7{^NF2Q,N6y4d]9&|+Qy$qB6zknzm4tkiOqa4Si5;)/lst@&;!r8%;\');
define(\'SECURE_AUTH_SALT\', \'~ r8gh?MF$I-)I~9kCC0>p(|nx7vF34<^ZN~AS~lb1Tzx-0SEvf3V1vi0+kQS#>_\');
define(\'LOGGED_IN_SALT\',   \'-CgDBdxb$N9Kw:MI+>c5WUxW,W141KP-6/tDV!I5s]m/H:#ltw(dP?0Ar]0cz]Yw\');
define(\'NONCE_SALT\',       \'ze9RdLoKcC-Bsl%<n6VjzDVz#Gu(h&t ^UkuqIpAz<$kN.7Cm|4p|{!Cyg[lOia;\');

/**#@-*/

/**
 * WordPress数据表前缀。
 *
 * 如果您有在同一数据库内安装多个WordPress的需求,请为每个WordPress设置
 * 不同的数据表前缀。前缀名只能为数字、字母加下划线。
 */
$table_prefix  = \'wp_\';

/**
 * 开发者专用:WordPress调试模式。
 *
 * 将这个值改为true,WordPress将显示所有用于开发的提示。
 * 强烈建议插件开发者在开发环境中启用WP_DEBUG。
 *
 * 要获取其他能用于调试的信息,请访问Codex。
 *
 * @link https://codex.wordpress.org/Debugging_in_WordPress
 */
define(\'WP_DEBUG\', false);

/**
 * zh_CN本地化设置:启用ICP备案号显示
 *
 * 可在设置→常规中修改。
 * 如需禁用,请移除或注释掉本行。
 */
define(\'WP_ZH_CN_ICP_NUM\', true);

/* 好了!请不要再继续编辑。请保存本文件。使用愉快! */

/** WordPress目录的绝对路径。 */
if ( !defined(\'ABSPATH\') )
    define(\'ABSPATH\', dirname(__FILE__) . \'/\');

/** 设置WordPress变量和包含文件。 */
require_once(ABSPATH . \'wp-settings.php\');
View Code

 

到此为止,2.157服务器上的两个网站已经全部发布完成,接下来,将该站点上的内容全部同步到2.158上,为后续的负载均衡做准备

 rsync -aP --delete /var/www/html/dz.bxy.net/ root@192.168.2.158:/var/www/html/dz.bxy.net/
rsync -aP --delete /var/www/html/wps.bxy.net/ root@192.168.2.158:/var/www/html/wps.bxy.net/

同理2.158也需要做本地hosts域名解析

两条命令搞定,2.158只需要将httpp虚机主机搭建起来,配置好与之相同的虚拟主机即可,然后将2.157部署的好网站源码同步过去即可,不需要重新解压~

【Nginx前端负载均衡+动静分离】

 

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    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  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;
        include /etc/nginx/default.d/*.conf;
        location / {
        }
        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
include vhosts/*.conf;
}

[root@dz nginx]# mkdir -p /etc/nginx/vhosts/

[root@dz nginx]# egrep -v "#|^$" /etc/nginx/vhosts/upstream.conf 
upstream httpd_webapp{
    server 192.168.2.157;
    server 192.168.2.158;
}
[root@dz nginx]# egrep -v "#|^$" /etc/nginx/vhosts/wps.conf
server {
listen 80;
server_name wps.bxy.net;
location /  {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://httpd_webapp;
              }
location ~ .*\\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
       root /var/www/html/wps.bxy.net;
      expires 3d;
}
}

 

[root@dz nginx]# egrep -v "#|^$" /etc/nginx/vhosts/dz.conf 
server {
listen 80;
server_name dz.bxy.net;
location /  {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://httpd_webapp;
              }
location ~ .*\\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
       root /var/www/html/dz.bxy.net;
      expires 3d;
}
}

 

 

 

 

以上是关于nginx+apache动静分离/负载均衡的主要内容,如果未能解决你的问题,请参考以下文章

Nginx反向代理动静分离负载均衡及rewrite隐藏路径详解(Nginx Apache MySQL Redis)–第三部分

nginx+apache+redis实现负载均衡动静分离session共享

Nginx反向代理动静分离负载均衡及rewrite隐藏路径详解(Nginx Apache MySQL Redis)–第二部分

Nginx反向代理动静分离负载均衡及rewrite隐藏路径详解(Nginx Apache MySQL Redis)–第一部分(LAMP)

Nginx动静分离

利用nginx实现动静分离的负载均衡集群实战