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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM