首页 > 系统相关 >nginx负载均衡读写分离

nginx负载均衡读写分离

时间:2022-10-20 00:01:24浏览次数:51  
标签:负载 -- local 读写 lnmp nginx php root

目录


nginx负载均衡读写分离

环境说明

主机名 IP地址 服务 系统
nginx 192.168.34.130 nginx centos8
lnmp 192.168.34.131 lnmp centos8
http 192.168.34.140 http centos8

nginx主机:源码部署nginx

[root@nginx ~]# systemctl  stop firewalld
[root@nginx ~]# getenforce 
Disabled
[root@nginx ~]# useradd -r -M -s /sbin/nologin nginx
[root@nginx ~]# yum -y install pcre-devel openssl openssl-devel make gd-devel gcc gcc-c++
[root@nginx ~]# mkdir -p /var/log/nginx
[root@nginx ~]# chown -R nginx.nginx /var/log/nginx/
[root@nginx ~]# cd /usr/src/
[root@nginx src]# wget https://nginx.org/download/nginx-1.22.0.tar.gz
[root@nginx src]# tar xf nginx-1.22.0.tar.gz -C /usr/local/
[root@nginx src]# cd /usr/local/nginx-1.22.0/
[root@nginx 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@nginx nginx-1.22.0]# make && make install 
[root@nginx nginx-1.22.0]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@nginx nginx-1.22.0]#  source /etc/profile.d/nginx.sh 
[root@nginx nginx-1.22.0]# nginx
[root@nginx nginx-1.22.0]# nginx -s stop
[root@nginx nginx-1.22.0]# vim /usr/lib/systemd/system/nginx.service
[root@nginx nginx-1.22.]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx server daemon
After=network.target 
 
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP \$MAINPID
 
[Install]
WantedBy=multi-user.target
[root@nginx nginx-1.22.0]# systemctl daemon-reload
[root@nginx nginx-1.22.0]# systemctl enable --now nginx
修改配置文件实现动静分离
[root@nginx html]# vim /usr/local/nginx/conf/nginx.conf
   upstream php.com {
        server 192.168.34.131:80;
    }

    upstream http.com {
        server 192.168.34.131:81;
        server 192.168.34.140:80;
    }
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://http.com;
        }
        location ~ \.php$ {
            proxy_pass http://php.com;
        }
     }
     server {
        listen          81;
        server_name     localhost;
        location / {
            index    index.html;
       }

        }

lnmp主机

源码安装nginx
[root@lnmp ~]# systemctl  stop firewalld
[root@lnmp ~]# cat /etc/sysconfig/selinux
SELINUX=disabled
[root@lnmp ~]# yum -y install pcre-devel openssl openssl-devel make gd-devel gcc gcc-c++ vim wget 
[root@lnmp yum.repos.d]# yum -y install pcre-devel openssl openssl-devel make gd-devel gcc gcc-c++ vim wget 
[root@lnmp src]# wget https://nginx.org/download/nginx-1.22.0.tar.gz
[root@lnmp src]# tar xf nginx-1.22.0.tar.gz -C /usr/local/
[root@lnmp src]# cd /usr/local/nginx-1.22.0/
[root@lnmp 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@lnmp nginx-1.22.0]# make && make install
[root@lnmp nginx-1.22.0]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@lnmp nginx-1.22.0]# source /etc/profile.d/nginx.sh 
[root@lnmp nginx-1.22.0]# nginx
[root@lnmp nginx-1.22.0]# nginx -s stop
[root@lnmp nginx-1.22.0]# vim /usr/lib/systemd/system/nginx.service
[root@lnmp nginx-1.22.0]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx server daemon
After=network.target 
 
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP \$MAINPID
 
[Install]
WantedBy=multi-user.target
[root@lnmp nginx-1.22.0]# systemctl daemon-reload
[root@lnmp nginx-1.22.0]# systemctl enable --now nginx
安装mysql
[root@lnmp ~]# yum -y install ncurses-devel ncurses-compat-libs  openssl-devel openssl cmake mariadb-devel
[root@lnmp ~]# groupadd -r -g 306 mysql
[root@lnmp ~]#  useradd -rMs /sbin/nologin -g 306 -u 306 mysql
[root@lnmp ~]#  cd /usr/src/
[root@lnmp src]# ls
debug    mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz  php-8.1.11.tar.gz
kernels  nginx-1.22.0.tar.gz
[root@lnmp src]# tar xf mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz -C /usr/local/
[root@lnmp src]# cd /usr/local/
[root@lnmp local]# ln -s mysql-8.0.28-linux-glibc2.12-x86_64/ mysql
[root@lnmp local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@lnmp local]# source /etc/profile.d/mysql.sh 
[root@lnmp local]#  ln -s /usr/local/mysql/include /usr/include/mysql
[root@lnmp local]# vim /etc/man_db.conf 		//添加以下内容
MANDATORY_MANPATH                       /usr/local/mysql/man
[root@lnmp local]#  mkdir /opt/data
[root@lnmp local]# chown -R mysql.mysql /opt/data/
[root@lnmp local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
[root@lnmp local]# 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@lnmp local]#  cd /usr/local/mysql
[root@lnmp mysql]# cd support-files/
[root@lnmp support-files]# cp mysql.server /etc/init.d/mysqld
[root@lnmp support-files]# vim /etc/init.d/mysqld
basedir=/usr/local/mysqld
datadir=/opt/data
[root@lnmp support-files]#  chmod +x /etc/init.d/mysqld
[root@lnmp support-files]# service mysqld start
[root@lnmp support-files]# mysql -uroot -p'1<ipz5lNWrXi'
mysql> alter user 'root'@'localhost' identified by '123456';
安装php
[root@lnmp ~]#  yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@lnmp ~]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@lnmp ~]#  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 http://mirror.centos.org/centos/8-stream/PowerTools/x86_64/os/Packages/oniguruma-devel-6.8.2-2.el8.x86_64.rpm sqlite-devel openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ libsqlite3x-devel libzip-devel
[root@lnmp ~]# cd /usr/src/
[root@lnmp src]# wget https://www.php.net/distributions/php-8.1.11.tar.gz
[root@lnmp src]# tar xf php-8.1.11.tar.gz -C /usr/local/
[root@lnmp src]# cd /usr/local/php-8.1.11/
[root@lnmp php-8.1.11]# ./configure --prefix=/usr/local/php  --with-config-file-path=/etc  --enable-fpm    --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-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@lnmp php-8.1.11]# make && make install
[root@lnmp php]# cd /usr/local/php
[root@lnmp php]# cp etc/php-fpm.conf.default etc/php-fpm.conf 
[root@lnmp php]# cp etc/php-fpm.d/www.conf.default etc/php-fpm.d/www.conf
[root@lnmp php]# echo 'export PATH=/usr/local/php/bin:$PATH' > /etc/profile.d/php.sh
[root@lnmp php]# source /etc/profile.d/php.sh
[root@lnmp php]# vim  /etc/ld.so.conf.d/php.conf
[root@lnmp php]# cat /etc/ld.so.conf.d/php.conf
/usr/local/php/lib
[root@lnmp php]# ln -s /usr/local/php/include/ /usr/include/php
[root@lnmp php]# vim /lib/systemd/system/php.service
[root@lnmp php]# systemctl start php.service 
[root@lnmp php]# systemctl enable php.service 
[root@lnmp html]# vim /usr/local/nginx/conf/nginx.conf
    server {
        listen      81;
        server_name  localhost;
        location / {
            root  html;
            index index.html;
        }
    }
    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;
        }
		        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }


添加php访问页面
[root@lnmp html]# cd /usr/local/nginx/html/
[root@lnmp html]# cat index.php 
<?php    
   phpinfo();
?>

http主机

安装http
[root@http ~]# dnf -y install httpd
[root@http ~]# cd /var/www/html/
[root@http html]# echo 'http' > index.html
[root@http html]# cat index.html
http

测试动静分离

标签:负载,--,local,读写,lnmp,nginx,php,root
From: https://www.cnblogs.com/TQingS/p/16808270.html

相关文章

  • nginx 上传文件报错 413 (Request Entity Too Large)
     最近开发时遇到了上传失败的情况,提示:413RequestEntityTooLarge(请求实体太大)因为上传通过代理服务器Nginx,因此可以修改代理服务器Nginx的相关配置来解决。作为......
  • nginx violates the following Content Security Policy directive: "default-src 'se
    violatesthefollowingContentSecurityPolicydirective:"default-src'self'". Nginx解决内容安全策略CSP(Content-Security-Policy)配置方式(漏洞修复)-龙凌云端-......
  • nginx+vite 项目打包及部署到服务器二级路由
    项目打包及部署到服务器二级路由例如:我希望将打包的项目部署到http://localhost:8088/web/上一.项目配置及打包项目部署到服务器二级路由需要配置基础路径base,即需要......
  • 读写锁还不会用StampedLock就Out了
    概述想到读写锁,大家第一时间想到的可能是​​ReentrantReadWriteLock​​。实际上,在jdk8以后,java提供了一个性能更优越的读写锁并发类​​StampedLock​​,该类的设计初衷是......
  • CPU高利用率及IO高负载故障定位分析
    一、系统表说明MySQL5.7版本起,performance_schema.threads线程表可以查询各个线程的信息,THREAD_OS_ID值对应OS中的线程,这就为故障定位提供了便捷,SQL如下:参数:30502为OS......
  • nginx本地配置web项目-layui
    nginx安装配置以及配置本地web项目nginx下载和安装介绍nginx(enginex)是一个高性能的HTTP和反向代理web服务器,其他的介绍自己百度去看下载https://nginx.org/en/downl......
  • Automatic Workload Repository (AWR)自动工作负载存储库
    自动工作负载存储库(AWR)是历史性能数据的存储库,其中包括系统、会话、单个SQL语句、段和服务的累积统计信息。AWR统计数据是性能调优的基础。通过自动收集用于问题检......
  • nginx负载均衡
    nginx负载均衡目录nginx负载均衡nginx负载均衡介绍反向代理与负载均衡nginx负载均衡配置Keepalived高可用nginx负载均衡器修改Web服务器的默认主页开启nginx负载均衡和反......
  • cpp ofstream ifstreram binary 文档读写
    includeincludeincludeincludeinclude<sys/stat.h>includevoidtest_copy_binary(std::stringsrc,std::stringdst){std::ofstreamostrm(dst,std::ios......
  • Windows下Nginx初始化
    官网下载地址:https://nginx.org/en/download.html 二、安装部署1、下载完成后,解压缩,运行cmd,使用命令进行操作,不要直接双击nginx.exe,不要直接双击nginx.exe,不要直接双......