當前在SpringCloud微服務架構下,網關做爲服務的入口尤其重要,一旦網關發生單點故障會致使整個服務集羣癱瘓,爲了保證網關的高可用能夠經過Nginx的反向代理功能實現網關的高可用。html
項目源碼:https://github.com/taoweidong/Micro-service-learning/tree/SpringCloud-branchnginx
(C:\Windows\System32\drivers\etc)編輯下面這個文件,修改裏面ip對應的地址,由於要使用域名的不一樣來實現反向代理.
git
Nginx下載地址(Windows和Linux的配置同樣):http://nginx.org/en/download.htmlgithub
解壓後,找到配置文件\nginx-1.12.2\conf\nginx.conf
詳細配置算法
#user nobody; 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 { worker_connections 1024; } http { include mime.types; 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 on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; #配置上游服務器網關端口集羣 upstream backServer{ # weight 權重:誰的的權重多,訪問到哪一個服務的概率就大 server 127.0.0.1:8040 weight=1; server 127.0.0.1:8041 weight=1; } server { # 注意:若是使用域名進行反向代理的話,Nginx的端口必須是80 listen 80; # 入口地址-對應域名地址 server_name www.taowd123.com; location /ms { ### 指定上游服務器負載均衡服務器 proxy_pass http://backServer/; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # 錯誤頁面 error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
http://www.javashuo.com/article/p-rjlonsov-ep.html後端
歡迎訪問我的博客: http://www.taoweidong.com/服務器