搭建nginx 负载均衡

使用nginx 搭建负载均衡


 首先我们需要一台服务器来安装nginx

 安装见:安装链接


安装完成之后;我们开始搭建负载均衡

打开 nginx.conf 文件

upstream haiyong{  
        server 172.16.170.138;  
        server 172.16.170.139;  
}  
server {  
    listen       80;  
    server_name  localhost;  
  
    #charset koi8-r;  
    #access_log  /var/log/nginx/log/host.access.log  main;  
  
    location / {  
        root   /usr/share/nginx/html;  
        index  index.html index.htm;  
        proxy_pass http://haiyong;  
    } 

此时我们就搭建成功了!

这种是默认方法轮询


其他方法见:其他方式

 







搭建nginx负载均衡可以按照以下步骤进行: ### 准备工作 安装tomcat,安装完成后,在`./webapps`目录下添加`test`目录,在`test`目录下添加`index.html`文件,并编辑`index.html`文件,例如填写`11111`用于测试 [^3]。 ### 下载Nginx 可以从官网(https://nginx.org/en/download.html/nginx - 1.18.0 )下载Nginx,也可以使用`wget`在线下载。将下载好的`nginx.tar.gz`文件复制到Linux的`/usr/local/`目录下,然后使用`tar -zxvf`命令进行解压 [^3]。 ### 配置Nginx 使用`nginx/nginx.conf`配置文件,示例配置如下: ```plaintext user nginx; worker_processes auto; error_log /var/log/nginx/error.log notice; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/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 /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; } ``` 此配置文件对Nginx的基本参数、日志格式、网络连接等进行了设置 [^2]。 ### 测试配置 使用命令`nginx -t`来测试Nginx配置文件是否正确。若输出类似`nginx: the configuration file /etc/nginx/nginx.conf syntax is ok`和`nginx: configuration file /etc/nginx/nginx.conf test is successful`的信息,则表示配置文件语法正确且测试成功 [^4]。 ### 启动Nginx 完成上述步骤后,启动Nginx服务,Nginx即可实现负载均衡功能,分担流量,确保高效响应,提升系统的可用性和扩展性,帮助企业应对高并发请求,提升用户体验 [^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值