nginx做负载均衡服务器,配置动静分离 保姆级教程( 三 )

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

  • 访问:http://192.168.232.134/

nginx做负载均衡服务器,配置动静分离 保姆级教程

文章插图
 
4. 部署4.1 在128主机安装httpd,做静态资源[root@node3 ~]# yum -y install httpd[root@node3 ~]# systemctl enable --now httpdCreated symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.[root@node3 ~]# ss -antlStateRecv-Q Send-QLocal Address:PortPeer Address:Port Process LISTEN 01280.0.0.0:220.0.0.0:*LISTEN 0128*:80*:*LISTEN 0128[::]:22[::]:*
nginx做负载均衡服务器,配置动静分离 保姆级教程

文章插图
 
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.gzResolving 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 OKLength: 1073322 (1.0M) [application/octet-stream]Saving to: 'nginx-1.22.0.tar.gz'nginx-1.22.0.tar. 100%[==========>]1.02M29.1KB/sin 20s2022-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/nginxdrwxr-xr-x. 2 nginx nginx 6 Sep5 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 -antlStateRecv-Q Send-QLocal Address:PortPeer Address:Port Process LISTEN 01280.0.0.0:220.0.0.0:*LISTEN 01280.0.0.0:800.0.0.0:*LISTEN 0128[::]:22[::]:*[root@node2 ~]#5. 配置负载均衡,129主机[root@node6 sbin]# ss -antlStateRecv-Q Send-Q Local Address:PortPeer Address:Port Process LISTEN 01280.0.0.0:800.0.0.0:*LISTEN 01280.0.0.0:220.0.0.0:*LISTEN 0128127.0.0.1:90000.0.0.0:*LISTEN 0128[::]:22[::]:*LISTEN 070*:33060*:*LISTEN 0128*:3306*:*[root@node6 sbin]# [root@node2 ~]# cd /usr/local/nginx/conf/[root@node2 conf]# vim nginx.conf#gzipon;upstream backend#配置负载均衡server 192.168.232.128;server 192.168.232.134;}server {listen80;server_namelocalhost;#charset koi8-r;#access_loglogs/host.access.logmain;location / {proxy_pass http://backend;#配置反向代理}[root@node2 conf]# nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: 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做负载均衡服务器,配置动静分离 保姆级教程

文章插图
 
  • 访问:http://192.168.232.129/

nginx做负载均衡服务器,配置动静分离 保姆级教程

文章插图
 
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#gzipon;upstream static {server 192.168.232.128;#httpd主机的ip}upstream dynamic {server 192.168.232.134;#lnmp主机的ip}server {listen80;server_namelocalhost;#charset koi8-r;#access_loglogs/host.access.logmain;location / {proxy_pass http://static;#访问静态资源会自动跳转到进行访问}# proxy the PHP scripts to Apache listening on 127.0.0.1:80#location ~ .php$ {proxy_passhttp://dynamic;#访问动态资源会自动跳转到进行访问}[root@node2 conf]# nginx -s reload[root@node2 conf]# nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@node2 conf]#