LoadBalancer-Nginx-01-Nginx

前提条件

CentOS7:2009

环境

Nginx:1.20.1

安装

# 安装epel(Extra Packages for Enterprise Linux)企业Linux扩展包
yum install -y epel-release
# 验证epel成功加入repo中
yum repolist
# 安装nginx
yum install -y nginx

配置

自动启动#

# 启用开机启动
systemctl enable nginx
# 确认开机启动已启用
systemctl is-enabled nginx

启动#

# 启动
systemctl start nginx
# 查看状态
systemctl status nginx

防火墙#

增加http https 例外规则

firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

配置文件#

修改默认配置文件/etc/nginx/nginx.conf

# 修改文件 注释或者删除http下server部分的代码
# 以便使/etc/nginx/conf.d/default.conf配置生效
server {
...
}

在目录/etc/nginx/conf.d/ 新增配置文件default.conf

upstream <ServerName1>{
        server <HostIP1>:<Port1>;
        server <HostIP2>:<Port1>;
}

upstream <ServerName2>{
        server <HostIP1>:<Port2>;
        server <HostIP2>:<Port2>;
}

server {
    listen       80;
    server_name  yourdomainname.com;
    root /var/www/nancydemo;

    location /Content/ {
        alias /var/www/nancydemo/Content/;
        location ~*  \.(jpg|jpeg|png|gif|ico|css|js|ttf)$ {
            expires 365d;
        }
    }

    location / {
            proxy_pass http://<ServerName1>;
            proxy_http_version 1.1;
            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_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            proxy_connect_timeout 10s;
            proxy_read_timeout 3600s;
    }

    location ~ ^/apigateway/<ServerName2>/* {
            proxy_pass http://<ServerName2>;
            proxy_http_version 1.1;
            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_connect_timeout 10s;
            proxy_read_timeout 3600s;
    }
}

重启nginx服务以便生效

systemctl restart nginx

作者:KSPT

出处:https://www.cnblogs.com/KSPT/p/LoadBalancer-Nginx-01-Nginx.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   知科行技  阅读(41)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
menu