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

铁佛  论坛元老 | 2024-11-9 14:42:51 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 1822|帖子 1822|积分 5466

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

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


1 下载nginx

官网地址:https://nginx.org/en/download.html

可以选择本身想要的操作系统和版本,小编这里使用的是windows的1.24版本 
解压后在文件夹目录下点击nginx.exe,这里应该会一闪而过,不用担心,应该是启动成功了,我们可以鼠标右键打开使命管理器输入nginx

然后输入127.0.0.1出现这个页面,体现nginx已经安装成功
 
2 准备一个简朴的项目架构

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


快捷键ctrl + d复制一个实例然后修改端口后启动,这样就可以启动多个实例了。
 这里我启动了三个实例,端口分别是8086 8087 8088 
4 修改nginx的设置文件

文件路径在nginx:tag/conf/nginx.conf 这里是我的全部设置文件,你们可以自定义修改
  1. #user  nobody;
  2. worker_processes  1;
  3. #error_log  logs/error.log;
  4. #error_log  logs/error.log  notice;
  5. #error_log  logs/error.log  info;
  6. #pid        logs/nginx.pid;
  7. events {
  8.     worker_connections  1024;
  9. }
  10. http {
  11.     include       mime.types;
  12.     default_type  application/octet-stream;
  13.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  14.     #                  '$status $body_bytes_sent "$http_referer" '
  15.     #                  '"$http_user_agent" "$http_x_forwarded_for"';
  16.     #access_log  logs/access.log  main;
  17.     sendfile        on;
  18.     #tcp_nopush     on;
  19.     #keepalive_timeout  0;
  20.     keepalive_timeout  65;
  21.     #gzip  on;
  22.      # 定义负载均衡的后端服务器
  23.     upstream backend_servers {
  24.         server 127.0.0.1:8086;  # 后端服务器1
  25.         server 127.0.0.1:8087;  # 后端服务器2
  26.         server 127.0.0.1:8088;  # 后端服务器3
  27.     }
  28.     server {
  29.         listen       8888;
  30.         server_name  localhost;
  31.         #charset koi8-r;
  32.         #access_log  logs/host.access.log  main;
  33.         # location / {
  34.         #     root   html;
  35.         #     index  index.html index.htm;
  36.         # }
  37.          location / {
  38.             proxy_pass http://backend_servers;  # 将请求转发到upstream定义的服务器组
  39.             proxy_set_header Host $host;  # 保持请求头的Host
  40.             proxy_set_header X-Real-IP $remote_addr;  # 保持客户端真实IP
  41.             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  # 传递客户端真实IP链
  42.             proxy_set_header X-Forwarded-Proto $scheme;  # 保持协议(http/https)
  43.         }
  44.         #error_page  404              /404.html;
  45.         # redirect server error pages to the static page /50x.html
  46.         #
  47.         error_page   500 502 503 504  /50x.html;
  48.         location = /50x.html {
  49.             root   html;
  50.         }
  51.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  52.         #
  53.         #location ~ \.php$ {
  54.         #    proxy_pass   http://127.0.0.1;
  55.         #}
  56.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  57.         #
  58.         #location ~ \.php$ {
  59.         #    root           html;
  60.         #    fastcgi_pass   127.0.0.1:9000;
  61.         #    fastcgi_index  index.php;
  62.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  63.         #    include        fastcgi_params;
  64.         #}
  65.         # deny access to .htaccess files, if Apache's document root
  66.         # concurs with nginx's one
  67.         #
  68.         #location ~ /\.ht {
  69.         #    deny  all;
  70.         #}
  71.     }
  72.     # another virtual host using mix of IP-, name-, and port-based configuration
  73.     #
  74.     #server {
  75.     #    listen       8000;
  76.     #    listen       somename:8080;
  77.     #    server_name  somename  alias  another.alias;
  78.     #    location / {
  79.     #        root   html;
  80.     #        index  index.html index.htm;
  81.     #    }
  82.     #}
  83.     # HTTPS server
  84.     #
  85.     #server {
  86.     #    listen       443 ssl;
  87.     #    server_name  localhost;
  88.     #    ssl_certificate      cert.pem;
  89.     #    ssl_certificate_key  cert.key;
  90.     #    ssl_session_cache    shared:SSL:1m;
  91.     #    ssl_session_timeout  5m;
  92.     #    ssl_ciphers  HIGH:!aNULL:!MD5;
  93.     #    ssl_prefer_server_ciphers  on;
  94.     #    location / {
  95.     #        root   html;
  96.     #        index  index.html index.htm;
  97.     #    }
  98.     #}
  99. }
复制代码
然后重新启动nginx ,你也可以自定义一些负载均衡策略:轮询、最小毗连数这些。
5 测试访问

此时我们通过访问127.0.0.1:8888/路径的一些资源就会代理到127.0.0.1:8086/8087/8088 上
此时我们访问127.0.0.1:8888/captchaImage 注:这里替换成本身的请求路径
返回控制台看见每个服务都有输出

 


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

铁佛

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表