原文鏈接
本來說這一篇文章就足夠用了,但是因?yàn)槲覀儗?shí)際的代碼是前后端分離了。我們的是單頁(yè)面應(yīng)用。所以說還有有一點(diǎn)點(diǎn)的不一樣的。我們一般的配置是模型如下圖所示:

但是我們實(shí)際情況是這樣的:

首先我們準(zhǔn)備了兩臺(tái)服務(wù)器
服務(wù)器一:主服務(wù)器,服務(wù)器二:次服務(wù)器
第一步當(dāng)然是安裝環(huán)境了jvm+tomcat+ngnix環(huán)境。兩臺(tái)服務(wù)器都安裝好ngnix和tomcat,具體就不描述如何安裝所需環(huán)境了下面我給出原文地址
第二步我們?cè)诜?wù)一中的tomcat中部署后臺(tái)代碼并運(yùn)行,服務(wù)器一中的ngnix配置運(yùn)行前端靜態(tài)頁(yè)面代碼
單頁(yè)面應(yīng)用ngnix配置說明:
server {
#端口號(hào)
listen? ? ? 5000;
#訪問域名
server_name? localhost;
#charset koi8-r;
#access_log? logs/host.access.log? main;
#靜態(tài)頁(yè)面地址
root /usr/local/nginx/html/salary_h5/dist;
#配置入口頁(yè)面
index index.html index.htm;
location ~* \.(?:manifest|appcache|html?|xml|json)$ {
expires -1;
# access_log logs/static.log; # I don't usually include a static log
}
#配置js,css
location ~* \.(?:css|js)$ {
try_files $uri =404;
access_log off;
}
# Any route containing a file extension (e.g. /devicesfile.js)
location ~ ^.+\..+$ {
try_files $uri =404;
}
# Any route that doesn't have a file extension (e.g. /devices)
#解決單頁(yè)面應(yīng)用刷新問題
location / {
try_files $uri $uri/ /index.html;
}
error_page? 500 502 503 504? /50x.html;
location = /50x.html {
root? html;
}
}
第三步我們?cè)诜?wù)器二中的tomcat部署運(yùn)行后臺(tái)代碼,服務(wù)器二中的ngnix配置后臺(tái)分流
服務(wù)器分流配置
#user? nobody;
#工作進(jìn)程的個(gè)數(shù),一般與計(jì)算機(jī)的cpu核數(shù)一致
worker_processes? 1;
#error_log? logs/error.log;
#error_log? logs/error.log? notice;
#error_log? logs/error.log? info;
#pid? ? ? ? logs/nginx.pid;
events {
#單個(gè)進(jìn)程最大連接數(shù)(最大連接數(shù)=連接數(shù)*進(jìn)程數(shù))
worker_connections? 1024;
}
http {
#文件擴(kuò)展名與文件類型映射表
include? ? ? mime.types;
#默認(rèn)文件類型
default_type? application/octet-stream;
#log_format? main? '$remote_addr - $remote_user [$time_local] "$request" '
#? ? ? ? ? ? ? ? ? '$status $body_bytes_sent "$http_referer" '
#? ? ? ? ? ? ? ? ? '"$http_user_agent" "$http_x_forwarded_for"';
#access_log? logs/access.log? main;
#開啟高效文件傳輸模式,sendfile指令指定nginx是否調(diào)用sendfile函數(shù)來輸出文件,對(duì)于普通應(yīng)用設(shè)為 on,如果用來進(jìn)行下載等應(yīng)用磁盤IO重負(fù)載應(yīng)用,可設(shè)置為off,以平衡磁盤與網(wǎng)絡(luò)I/O處理速度,降低系統(tǒng)的負(fù)載。注意:如果圖片顯示不正常把這個(gè)改成off。
sendfile? ? ? ? on;
#tcp_nopush? ? on;
#keepalive_timeout? 0;
#長(zhǎng)連接超時(shí)時(shí)間,單位是秒
keepalive_timeout? 65;
#啟用Gizp壓縮
#gzip? on;
#==================================================================================
#服務(wù)器集群 a.server.com;為集群服務(wù)器名
upstream a.server.com;{
#服務(wù)器1 weight是權(quán)重的意思,權(quán)重越大,分配的概率越大。
server 服務(wù)器:端口?weight=2;
#服務(wù)器2
server 服務(wù)器2:端口號(hào)?weight=1;
}
server {
#端口號(hào)
listen? ? ? 8888;
#訪問域名
server_name ?提供給前端使用的域名
location / {
#交給名字為infud.server.com的集群去處理
proxy_pass http://a.server.com;
proxy_redirect default;
}
#=====================================================================================
第四步,將服務(wù)器二中ngnix配置分流后的ip地址提供前端調(diào)用。這樣就能實(shí)現(xiàn)分流了。