1 2 3 4

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

 

posted @ 2019-07-18 11:27  一缕清风丶  阅读(2215)  评论(0编辑  收藏  举报