使用nginx反向代理后端服务,实现负载均衡

2021/9/27 7:14:23

本文主要是介绍使用nginx反向代理后端服务,实现负载均衡,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

背景:生产环境部署后端服务,没有负载均导致服务压力过大,可以启用nginx进行反向代理。

准备:1)后端服务启动在8081(可以修改)

           2)nginx监听8080(可以修改),转发请求到8081(和后端服务端口一致)

nginx配置

user  root;
worker_processes  1;

events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;

    # server
    server {
        listen       8080;
        server_name  gateway;
        charset utf-8;
        location / {
          root html/www;
          index  index.html index.htm;
          # include /etc/nginx/uwsgi_params;
          proxy_pass http://xx.xx.xx.xx:8081; # 填写真实地址
        }
        error_page   500 502 503 504  /50x.html;
        # 日志,需要创建好目录
        #access_log /code/datafabric/apigateway/log/gateway.access.log;
        #error_log  /code/datafabric/apigateway/log/gateway.error.log;
    }
}



这篇关于使用nginx反向代理后端服务,实现负载均衡的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程