nginx做负载均衡服务器,配置动静分离

nginx做负载均衡服务器,配置动静分离

文章目录

    • nginx做负载均衡服务器,配置动静分离
      • 1. 题目:
      • 2. 环境和提供软件包
        • 2.1 提供软件包
        • 2.2 环境
      • 3. 在134主机主机部署lnmp,做动态资源
        • 3.1 源码安装nginx
        • 3.2 二进制安装MySQL
        • 3.3 源码安装PHP
        • 3.4 配置nginx
        • 3.5 配置PHP网络界面
      • 4. 部署
        • 4.1 在128主机安装httpd,做静态资源
        • 4.2 在129主机源码安装nginx并配置负载均衡器,进行调度
      • 5. 配置负载均衡,129主机
      • 6. 实现动静分离

1. 题目:

后端RS服务器⼀台部署LNMP(nginx1.22+mysql8.0+php8.0),⼀台部署
httpd。
要求nginx和php使⽤编译安装
最后要通过访问nginx负载均衡服务器的IP看到动静分离的效果。

2. 环境和提供软件包

2.1 提供软件包
[root@node6 ~]# wget https://nginx.org/download/nginx-1.22.0.tar.gz https://www.php.net/distributions/php-8.0.23.tar.gz
[root@node6 ~]# ls
anaconda-ks.cfg                             nginx-1.22.0.tar.gz
mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz  php-8.0.23.tar.gz
[root@node6 ~]# 
2.2 环境
主机 ip 安装的服务
node6 192.168.232.134 lnmp,动态资源
node3 192.168.232.128 nginx,静态资源
node2 192.168.232.129 nginx,做负载均衡
  • 关闭防火墙
[root@node6 ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@node6 ~]# vim /etc/selinux/config 
[root@node6 ~]# setenforce 0


[root@node3 ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@node3 ~]# setenforce 0
[root@node3 ~]# vi /etc/selinux/config


[root@node2 ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@node2 ~]# setenforce 0
[root@node2 ~]# vi /etc/selinux/config

3. 在134主机主机部署lnmp,做动态资源

3.1 源码安装nginx
创建系统用户nginx
[root@node6 ~]# useradd -r -M -s /sbin/nologin nginx
[root@node6 ~]# 


安装依赖环境
[root@node6 ~]# yum -y groups mark install 'Development Tools'
[root@node6 ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make 


创建日志存放目录
[root@node6 ~]# mkdir -p /var/log/nginx
[root@node6 ~]# chown -R nginx.nginx /var/log/nginx
[root@node6 ~]# ll -d /var/log/nginx
drwxr-xr-x. 2 nginx nginx 6 Sep  5 18:43 /var/log/nginx


编译安装
[root@node6 ~]# tar xf nginx-1.22.0.tar.gz 
[root@node6 ~]# cd nginx-1.22.0
[root@node6 nginx-1.22.0]# ./configure \
    --prefix=/usr/local/nginx \
    --user=nginx \
    --group=nginx \
    --with-debug \
    --with-http_ssl_module \
    --with-http_realip_module \
    --with-http_image_filter_module \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
    --with-http_stub_status_module \
    --http-log-path=/var/log/nginx/access.log \
    --error-log-path=/var/log/nginx/error.log

[root@node6 nginx-1.22.0]# make 
[root@node6 nginx-1.22.0]# make install


nginx安装后配置
[root@node6 ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@node6 ~]# source /etc/profile.d/nginx.sh



启动nginx
[root@node6 ~]# nginx 
[root@node6 ~]# ss -antl
State  Recv-Q Send-Q  Local Address:Port   Peer Address:Port Process 
LISTEN 0      128           0.0.0.0:22          0.0.0.0:*            
LISTEN 0      128           0.0.0.0:80          0.0.0.0:*            
LISTEN 0      128              [::]:22             [::]:*            
[root@node6 ~]# nginx -s stop
[root@node6 ~]# ss -antl
State  Recv-Q Send-Q  Local Address:Port   Peer Address:Port Process 
LISTEN 0      128           0.0.0.0:22          0.0.0.0:*            
LISTEN 0      128              [::]:22             [::]:*            
[root@node6 ~]# 
  • 可以访问

nginx做负载均衡服务器,配置动静分离_第1张图片

3.2 二进制安装MySQL
安装依赖包,创建用户,并解压
[root@node6 ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel ncurses-compat-libs

[root@node6 ~]# useradd -r -M -s /sbin/nologin mysql

[root@node6 ~]# tar xf mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz -C /usr/local/



修改属主
[root@node6 ~]# cd /usr/local/
[root@node6 local]# ls
bin    include  libexec                              sbin
etc    lib      mysql-8.0.20-linux-glibc2.12-x86_64  share
games  lib64    nginx                                src
[root@node6 local]# mv mysql-8.0.20-linux-glibc2.12-x86_64 mysql
[root@node6 local]# chown -R mysql.mysql mysql
[root@node6 local]# ll -d mysql
drwxr-xr-x. 9 mysql mysql 129 Sep  5 19:02 mysql



配置环境变量,man文档,lib库,头文件
[root@node6 ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@node6 ~]# source /etc/profile.d/mysql.sh
[root@node6 ~]# vim /etc/ld.so.conf.d/mysql.conf
[root@node6 ~]# ldconfig 
[root@node6 ~]# cat /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@node6 ~]# ln -s /usr/local/mysql/include /usr/include/mysql
[root@node6 ~]# 
[root@node6 ~]# vim /etc/man_db.conf
MANDATORY_MANPATH                       /usr/local/mysql/man



建立数据存放目录,并修改属主
[root@node6 ~]# mkdir -p /opt/data
[root@node6 ~]# chown -R mysql.mysql /opt/data
[root@node6 ~]# ll -d /opt/data
drwxr-xr-x. 2 mysql mysql 6 Sep  5 19:09 /opt/data
[root@node6 ~]# 


初始化数据库,并保存密码
[root@node6 ~]# mysqld --initialize --user mysql --datadir /opt/data
2022-09-05T11:10:42.151293Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.20) initializing of server in progress as process 209429
2022-09-05T11:10:42.160150Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-09-05T11:10:43.610708Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-09-05T11:10:44.652264Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: w-ETXrfTV1wE
[root@node6 ~]# echo 'w-ETXrfTV1wE' > passwd
[root@node6 ~]# cat passwd
w-ETXrfTV1wE
[root@node6 ~]# 



生成配置文件
[root@node6 ~]# > /etc/my.cnf
[root@node6 ~]# vim /etc/my.cnf
[root@node6 ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
[root@node6 ~]# 



配置服务启动脚本
[root@node6 ~]# cd /usr/local/mysql/support-files/
[root@node6 support-files]# cp mysql.server mysqld
[root@node6 support-files]# vim mysqld
basedir=/usr/local/mysql
datadir=/opt/data
[root@node6 support-files]# chown -R mysql.mysql mysqld
[root@node6 support-files]# ll -d mysqld
-rwxr-xr-x. 1 mysql mysql 10602 Sep  2 19:06 mysqld
[root@node6 support-files]# 


先启动测试一下
[root@node6 ~]# /usr/local/mysql/support-files/mysqld start
Starting MySQL.Logging to '/opt/data/node6.err'.
 SUCCESS! 
[root@node6 ~]# ss -antl
State  Recv-Q Send-Q Local Address:Port    Peer Address:Port Process 
LISTEN 0      128          0.0.0.0:22           0.0.0.0:*            
LISTEN 0      128          0.0.0.0:80           0.0.0.0:*            
LISTEN 0      128             [::]:22              [::]:*            
LISTEN 0      70                 *:33060              *:*            
LISTEN 0      128                *:3306               *:*            
[root@node6 ~]# /usr/local/mysql/support-files/mysqld stop
Shutting down MySQL.. SUCCESS! 
[root@node6 ~]# ss -antl
State  Recv-Q Send-Q  Local Address:Port   Peer Address:Port Process 
LISTEN 0      128           0.0.0.0:22          0.0.0.0:*            
LISTEN 0      128           0.0.0.0:80          0.0.0.0:*            
LISTEN 0      128              [::]:22             [::]:*            
[root@node6 ~]# 


配置service文件,设置开机自启
[root@node6 ~]# cd /usr/lib/systemd/system
[root@node6 system]# cp sshd.service mysqld.service
[root@node6 system]# ll sshd.service 
-rw-r--r--. 1 root root 456 Jul 12  2021 sshd.service
[root@node6 system]# ll mysqld.service
-rw-r--r--. 1 root root 456 Sep  5 19:15 mysqld.service
[root@node6 system]# vim mysqld.service 
[root@node6 system]# cat mysqld.service
[Unit]
Description=mysqld server daemon
After=network.target sshd-keygen.target

[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysqld start
ExecStop=/usr/local/mysql/support-files/mysqld stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
[root@node6 system]# systemctl daemon-reload


[root@node6 system]# systemctl enable --now mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service 鈫� /usr/lib/systemd/system/mysqld.service.
[root@node6 system]# ss -antl
State  Recv-Q Send-Q Local Address:Port    Peer Address:Port Process 
LISTEN 0      128          0.0.0.0:22           0.0.0.0:*            
LISTEN 0      128          0.0.0.0:80           0.0.0.0:*            
LISTEN 0      128             [::]:22              [::]:*            
LISTEN 0      70                 *:33060              *:*            
LISTEN 0      128                *:3306               *:*            
[root@node6 system]# 



设置密码
[root@node6 ~]# mysql -uroot -p'w-ETXrfTV1wE';

mysql> alter user 'root'@'localhost' identified by 'run123123';
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye
[root@node6 ~]# mysql -uroot -p'run123123';
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.20 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> quit
Bye
[root@node6 ~]# 
3.3 源码安装PHP
解压
[root@node6 ~]# tar xf php-8.0.23.tar.gz

安装依赖包
[root@node6 ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel  pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd sqlite-devel libzip-devel https://vault.centos.org/centos/8/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm



编译安装:
[root@node6 ~]# cd php-8.0.23
[root@node6 php-8.0.23]# ./configure --prefix=/usr/local/php7  \
  --with-config-file-path=/etc \
  --enable-fpm \
  --enable-inline-optimization \
  --disable-debug \
  --disable-rpath \
  --enable-shared \
  --enable-soap \
  --with-openssl \
  --enable-bcmath \
  --with-iconv \
  --with-bz2 \
  --enable-calendar \
  --with-curl \
  --enable-exif  \
  --enable-ftp \
  --enable-gd \
  --with-jpeg \
  --with-zlib-dir \
  --with-freetype \
  --with-gettext \
  --enable-json \
  --enable-mbstring \
  --enable-pdo \
  --with-mysqli=mysqlnd \
  --with-pdo-mysql=mysqlnd \
  --with-readline \
  --enable-shmop \
  --enable-simplexml \
  --enable-sockets \
  --with-zip \
  --enable-mysqlnd-compression-support \
  --with-pear \
  --enable-pcntl \
  --enable-posix


[root@node6 php-8.0.23]# make
[root@node6 php-8.0.23]# make install


配置环境变量,lib,头文件
[root@node6 ~]# cd /usr/local/php7/
[root@node6 php7]# ls
bin  etc  include  lib  php  sbin  var
[root@node6 php7]# echo 'export PATH=/usr/local/php7/bin:/usr/local/php7/:sbin:$PATH' > /etc/profile.d/php7.sh
[root@node6 php7]# source /etc/profile.d/php7.sh
[root@node6 php7]# 
[root@node6 php7]# ln -s /usr/local/php7/include /usr/include/php
[root@node6 php7]# 
[root@node6 php7]# vim /etc/ld.so.conf.d/php.conf
[root@node6 php7]# cat /etc/ld.so.conf.d/php.conf
/usr/local/php7/lib
[root@node6 php7]# ldconfig 

[root@node6 ~]# php -v
PHP 7.4.30 (cli) (built: Sep  2 2022 19:31:45) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
[root@node6 ~]# 



配置php-fpm
[root@node6 ~]# cd php-8.0.23
[root@node6 php-8.0.23]# \cp php.ini-production /etc/php.ini
[root@node6 php-8.0.23]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@node6 php-8.0.23]# chmod +x /etc/rc.d/init.d/php-fpm
[root@node6 php-8.0.23]# ll /etc/rc.d/init.d/php-fpm
-rwxr-xr-x. 1 root root 2402 Sep  5 19:56 /etc/rc.d/init.d/php-fpm
[root@node6 php-8.0.23]# 
[root@node6 php-8.0.23]# pwd
/root/php-8.0.23
[root@node6 php-8.0.23]# cd /usr/local/php7/etc/
[root@node6 etc]# ls
pear.conf  php-fpm.conf.default  php-fpm.d
[root@node6 etc]# cp php-fpm.conf.default php-fpm.conf
[root@node6 etc]# cd php-fpm.d/
[root@node6 php-fpm.d]# ls
www.conf.default
[root@node6 php-fpm.d]# cp www.conf.default www.conf
[root@node6 php-fpm.d]# 
[root@node6 php-fpm.d]# vim www.conf
listen = 127.0.0.1:9000


启动
[root@node6 ~]# cd /etc/init.d
[root@node6 init.d]# ls
README  functions  php-fpm
[root@node6 init.d]# service php-fpm start
Starting php-fpm  done
[root@node6 init.d]# service php-fpm stop
Gracefully shutting down php-fpm . done
[root@node6 init.d]# service php-fpm start
Starting php-fpm  done
[root@node6 init.d]# ss -antl
State  Recv-Q Send-Q Local Address:Port    Peer Address:Port Process 
LISTEN 0      128          0.0.0.0:22           0.0.0.0:*            
LISTEN 0      128        127.0.0.1:9000         0.0.0.0:*            
LISTEN 0      128          0.0.0.0:80           0.0.0.0:*            
LISTEN 0      128             [::]:22              [::]:*            
LISTEN 0      70                 *:33060              *:*            
LISTEN 0      128                *:3306               *:*            
[root@node6 init.d]# 
3.4 配置nginx
[root@node6 ~]# cd /usr/local/nginx/conf/
[root@node6 conf]# vim nginx.conf
[root@node6 conf]# vim nginx.conf
[root@node6 conf]# cat nginx.conf

user  nginx;
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 {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.php index.html index.htm; //在index后面添加index.php,表示优先访问php页面
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
//将以下内容取消注释并修改    
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
[root@node6 conf]# nginx -s stop
[root@node6 conf]# nginx
3.5 配置PHP网络界面
[root@node6 ~]# cd /usr/local/nginx/html/
[root@node6 html]# vi index.php
[root@node6 html]# cat index.php
<?php
    phpinfo();
?>
[root@node6 html]# 

重启
[root@node6 ~]# nginx -s stop
[root@node6 ~]# nginx
  • 访问:http://192.168.232.134/

nginx做负载均衡服务器,配置动静分离_第2张图片

4. 部署

4.1 在128主机安装httpd,做静态资源
[root@node3 ~]# yum -y install httpd
[root@node3 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[root@node3 ~]# ss -antl
State  Recv-Q Send-Q  Local Address:Port   Peer Address:Port Process 
LISTEN 0      128           0.0.0.0:22          0.0.0.0:*            
LISTEN 0      128                 *:80                *:*            
LISTEN 0      128              [::]:22             [::]:*            

nginx做负载均衡服务器,配置动静分离_第3张图片

4.2 在129主机源码安装nginx并配置负载均衡器,进行调度
[root@node2 ~]# wget https://nginx.org/download/nginx-1.22.0.tar.gz
--2022-09-05 20:47:25--  https://nginx.org/download/nginx-1.22.0.tar.gz
Resolving nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5704::6, ...
Connecting to nginx.org (nginx.org)|3.125.197.172|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1073322 (1.0M) [application/octet-stream]
Saving to: 'nginx-1.22.0.tar.gz'

nginx-1.22.0.tar. 100%[==========>]   1.02M  29.1KB/s    in 20s     

2022-09-05 20:47:47 (51.5 KB/s) - 'nginx-1.22.0.tar.gz' saved [1073322/1073322]


[root@node2 ~]# useradd -r -M -s /sbin/nologin nginx
[root@node2 ~]# yum -y groups mark install 'Development Tools'
[root@node2 ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make 


[root@node2 ~]# mkdir -p /var/log/nginx
[root@node2 ~]# chown -R nginx.nginx /var/log/nginx
[root@node2 ~]# ll -d /var/log/nginx
drwxr-xr-x. 2 nginx nginx 6 Sep  5 20:51 /var/log/nginx
[root@node2 ~]# tar xf nginx-1.22.0.tar.gz 
[root@node2 ~]# cd nginx-1.22.0
[root@node2 nginx-1.22.0]# ./configure \
     --prefix=/usr/local/nginx \
     --user=nginx \
     --group=nginx \
     --with-debug \
     --with-http_ssl_module \
     --with-http_realip_module \
     --with-http_image_filter_module \
     --with-http_gunzip_module \
     --with-http_gzip_static_module \
     --with-http_stub_status_module \
     --http-log-path=/var/log/nginx/access.log \
     --error-log-path=/var/log/nginx/error.log

[root@node2 nginx-1.22.0]# make
[root@node2 nginx-1.22.0]# make install


[root@node2 ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@node2 ~]# source /etc/profile.d/nginx.sh


[root@node2 ~]# nginx 
[root@node2 ~]# ss -antl
State  Recv-Q Send-Q  Local Address:Port   Peer Address:Port Process 
LISTEN 0      128           0.0.0.0:22          0.0.0.0:*            
LISTEN 0      128           0.0.0.0:80          0.0.0.0:*            
LISTEN 0      128              [::]:22             [::]:*            
[root@node2 ~]# 

5. 配置负载均衡,129主机

[root@node6 sbin]# ss -antl
State  Recv-Q Send-Q Local Address:Port    Peer Address:Port Process 
LISTEN 0      128          0.0.0.0:80           0.0.0.0:*            
LISTEN 0      128          0.0.0.0:22           0.0.0.0:*            
LISTEN 0      128        127.0.0.1:9000         0.0.0.0:*            
LISTEN 0      128             [::]:22              [::]:*            
LISTEN 0      70                 *:33060              *:*            
LISTEN 0      128                *:3306               *:*            
[root@node6 sbin]# 


[root@node2 ~]# cd /usr/local/nginx/conf/
[root@node2 conf]# vim nginx.conf



#gzip  on;

    upstream backend  #配置负载均衡
        server 192.168.232.128;
        server 192.168.232.134;
    }   

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://backend;#配置反向代理
        }

  
[root@node2 conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@node2 conf]# nginx -s reload
[root@node2 conf]# nginx -s stop
[root@node2 conf]# nginx  
  • 访问:http://192.168.232.129/

nginx做负载均衡服务器,配置动静分离_第4张图片

  • 访问:http://192.168.232.129/

nginx做负载均衡服务器,配置动静分离_第5张图片

6. 实现动静分离

[root@node2 conf]# pwd
/usr/local/nginx/conf
[root@node2 conf]# vim nginx.conf
[root@node2 conf]# nginx -s reload
[root@node2 conf]# vim nginx.conf
[root@node2 conf]# nginx -s reload

#gzip  on;

    upstream static {
        server 192.168.232.128;#httpd主机的ip
    }

    upstream dynamic {
        server 192.168.232.134;#lnmp主机的ip
    }

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://static;#访问静态资源会自动跳转到进行访问
        }

   # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        location ~ \.php$ {
            proxy_pass   http://dynamic;#访问动态资源会自动跳转到进行访问
        }
[root@node2 conf]# nginx -s reload
[root@node2 conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@node2 conf]# 
  • 访问静态资源:http://192.168.232.129/

nginx做负载均衡服务器,配置动静分离_第6张图片

  • 访问动态资源:http://192.168.232.129/index.php

nginx做负载均衡服务器,配置动静分离_第7张图片

你可能感兴趣的:(nginx,服务器,负载均衡)