nginx反向代理实现前后端分离

springboot+vue+nginx

vue项目打包

npm run build

springboot 打包启动

java -jar cjq.jar--httpPort=8080

nginx配置并启动

 
#user root;
worker_processes  1;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

 
 

    sendfile        on;
 
    keepalive_timeout  65;
 

    server {
        listen       8000; #监听8000端口
        server_name  localhost; # 配置域名


      #线上部署推荐使用root
      #location / {
        #root E:\java.vue\cjq\dist; #vue项目部署路径
        #root html;
        #try_files $uri $uri/ /index.html last; #解决页面刷新404问题
        #index index.html index.htm;
      #}

      #本地开发推荐使用本地端口
      location / {
        proxy_pass http://127.0.0.1:8081/;

 
  

       }


        
        location /login {                  # 当访问/login的时候 nginx反向代理请求为 http://47.143.7.134:8888;
                #proxy_pass http://47.143.7.134:8888;
                proxy_pass http://127.0.0.1:8080/;

        }
        location /hello {                     # 当访问/hello的时候 nginx反向代理请求为 http://47.143.7.134:8888;
               # proxy_pass http://47.104.7.104:8888;
               proxy_pass http://127.0.0.1:8080/hello;

        }

     
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        
    }


   
}

nginx启动:nginx.exe

nginx停止:taskkill /IM nginx.exe /f

nginx重启:nginx.exe -s reload

注:localhost:8000/login/list   ==  localhost:8080/list

localhost:8000/hello/world==  localhost:8080/hello/world

浏览器访问

localhost:8000

nginx实现负载均衡

需在nginx配置文件nginx.conf中加入upstream 

upstream myServer {    
  
    server 192.168.72.49:9090 down;   
    server 192.168.72.49:8080 weight=2;   
    server 192.168.72.49:6060;   
    server 192.168.72.49:7070 backup;   
}  

1)down

    表示单前的server暂时不参与负载

2)Weight

    默认为1.weight越大,负载的权重就越大。

3)max_fails

    允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误

4)fail_timeout

    max_fails 次失败后,暂停的时间。

5)Backup

    其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

nginx的高可用

keepalive+nginx

 

转载于:https://www.cnblogs.com/1234cjq/p/11206139.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值