设为首页 加入收藏

TOP

nginx反向代理与负载均衡(二)
2023-07-23 13:36:20 】 浏览:86
Tags:nginx 向代理
#安装所需要的依赖包 [root@130 ~]# dnf -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget vim #下载nginx源码包,并解压配置 [root@130 ~]# wget https://nginx.org/download/nginx-1.22.0.tar.gz [root@130 ~]# tar -xf nginx-1.22.0.tar.gz [root@130 ~]# cd nginx-1.22.0/ [root@130 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 #编译安装 [root@130 nginx-1.22.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) [root@130 nginx-1.22.0]# make install #配置环境变量 [root@130 nginx-1.22.0]# echo "export PATH=$PATH:/usr/local/nginx/sbin" > /etc/profile.d/nginx.sh [root@130 nginx-1.22.0]# source /etc/profile.d/nginx.sh #编写servic文件 [root@130 nginx-1.22.0]# cd /usr/lib/systemd/system/ [root@130 system]# vim 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@130 system]# systemctl daemon-reload #启动服务并开机自启 [root@130 ~]# systemctl enable --now nginx.service Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service. [root@130 ~]# 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 [::]:22 [::]:*

部署web页面

131端

#修改主机名
[root@localhost ~]# hostnamectl set-hostname 131
[root@localhost ~]# bash

#关闭防火墙和selinux
[root@131 ~]# setenforce 0
[root@131 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@131 ~]# systemctl disable --now firewalld.service

#yum安装nginx
[root@131 ~]# dnf -y install nginx
[root@131 ~]# echo "web111" > /usr/share/nginx/html/index.html
[root@131 ~]# systemctl enable --now nginx.service
[root@131 ~]# 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                 [::]:80                 [::]:*                
LISTEN   0        128                 [::]:22                 [::]:* 
[root@131 ~]# curl 192.168.118.131
web111

132端

#修改主机名
[root@localhost ~]# hostnamectl set-hostname 132
[root@localhost ~]# bash

#关闭防火墙和selinux
[root@132 ~]# setenforce 0
[root@132 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@132 ~]# systemctl disable --now firewalld.service

#yum安装apache
[root@132 ~]# dnf -y install httpd
[root@132 ~]# echo "web222" > /var/www/html/index.html
[root@132 ~]# systemctl enable --now httpd.service
[root@132 ~]# 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                 [::]:*                
[root@132 ~]# curl 192.168.118.132
web222

实现nginx复制均衡

129端

[root@129 ~]# cd /usr/local/nginx/conf/
[root@129 conf]# vim nginx.conf
...
http {
...
    upstream web {                        #添加此处内容
    server 192.168.118.131;
    server 192.168.118.132;
    }

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            proxy_pass http://web;         #修改为proxy_pass http://web;
        }
...
}
首页 上一页 1 2 3 4 5 下一页 尾页 2/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Linux 文件系统与日志分析 下一篇CentOS8 LAMP的实现以及相关应用

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目