铁佛 发表于 2024-11-9 14:42:51

Nginx实现负载服务之间的负载均衡

目录
1 下载nginx
2 准备一个简朴的项目架构
3 idea启动多个服务实例
4 修改nginx的设置文件
5 测试访问

1 下载nginx

官网地址:https://nginx.org/en/download.html
https://i-blog.csdnimg.cn/direct/368efa9d657044639e92b80081539607.png
可以选择本身想要的操作系统和版本,小编这里使用的是windows的1.24版本 
解压后在文件夹目录下点击nginx.exe,这里应该会一闪而过,不用担心,应该是启动成功了,我们可以鼠标右键打开使命管理器输入nginx
https://i-blog.csdnimg.cn/direct/3d569ec8412e4a3494eae9ad176aeb57.png
然后输入127.0.0.1出现这个页面,体现nginx已经安装成功
https://i-blog.csdnimg.cn/direct/068d1af7799a4671a3243de723d0520d.png 
2 准备一个简朴的项目架构

这里我们就正常启动我们的项目,源端口8088
3 idea启动多个服务实例

https://i-blog.csdnimg.cn/direct/dd43d52fd0c24bfcb4a5995799ec197e.png
快捷键ctrl + d复制一个实例然后修改端口后启动,这样就可以启动多个实例了。
https://i-blog.csdnimg.cn/direct/159a8de9bce246dbbe6dafcb86d31e1d.png 这里我启动了三个实例,端口分别是8086 8087 8088 
4 修改nginx的设置文件

文件路径在nginx:tag/conf/nginx.conf 这里是我的全部设置文件,你们可以自定义修改

#usernobody;
worker_processes1;

#error_loglogs/error.log;
#error_loglogs/error.lognotice;
#error_loglogs/error.loginfo;

#pid      logs/nginx.pid;


events {
    worker_connections1024;
}


http {
    include       mime.types;
    default_typeapplication/octet-stream;

    #log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_loglogs/access.logmain;

    sendfile      on;
    #tcp_nopush   on;

    #keepalive_timeout0;
    keepalive_timeout65;

    #gzipon;

   # 定义负载均衡的后端服务器
    upstream backend_servers {
      server 127.0.0.1:8086;# 后端服务器1
      server 127.0.0.1:8087;# 后端服务器2
      server 127.0.0.1:8088;# 后端服务器3
    }

    server {
      listen       8888;
      server_namelocalhost;

      #charset koi8-r;

      #access_loglogs/host.access.logmain;

      # location / {
      #   root   html;
      #   indexindex.html index.htm;
      # }

         location / {
            proxy_pass http://backend_servers;# 将请求转发到upstream定义的服务器组
            proxy_set_header Host $host;# 保持请求头的Host
            proxy_set_header X-Real-IP $remote_addr;# 保持客户端真实IP
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;# 传递客户端真实IP链
            proxy_set_header X-Forwarded-Proto $scheme;# 保持协议(http/https)
      }

      #error_page404            /404.html;

      # redirect server error pages to the static page /50x.html
      #
      error_page   500 502 503 504/50x.html;
      location = /50x.html {
            root   html;
      }

      # proxy the PHP scripts to Apache listening on 127.0.0.1:80
      #
      #location ~ \.php$ {
      #    proxy_pass   http://127.0.0.1;
      #}

      # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
      #
      #location ~ \.php$ {
      #    root         html;
      #    fastcgi_pass   127.0.0.1:9000;
      #    fastcgi_indexindex.php;
      #    fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
      #    include      fastcgi_params;
      #}

      # deny access to .htaccess files, if Apache's document root
      # concurs with nginx's one
      #
      #location ~ /\.ht {
      #    denyall;
      #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_namesomenamealiasanother.alias;

    #    location / {
    #      root   html;
    #      indexindex.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_namelocalhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_keycert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout5m;

    #    ssl_ciphersHIGH:!aNULL:!MD5;
    #    ssl_prefer_server_cipherson;

    #    location / {
    #      root   html;
    #      indexindex.html index.htm;
    #    }
    #}

}
然后重新启动nginx ,你也可以自定义一些负载均衡策略:轮询、最小毗连数这些。
5 测试访问

此时我们通过访问127.0.0.1:8888/路径的一些资源就会代理到127.0.0.1:8086/8087/8088 上
此时我们访问127.0.0.1:8888/captchaImage 注:这里替换成本身的请求路径
返回控制台看见每个服务都有输出https://i-blog.csdnimg.cn/direct/3aa5d3bc201e4efdbda7c6915763dd0d.png
https://i-blog.csdnimg.cn/direct/4f9b237e326e4c8f93eaa2e85d821936.png https://i-blog.csdnimg.cn/direct/11945fc5b4414159b7cb3481740c4a50.png

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: Nginx实现负载服务之间的负载均衡