關於nginx做負載均衡的配置以及各個配置的含義(簡)
阿新 • • 發佈:2019-02-17
最近在公司做了一個專案,整體是分為service層和web層兩個專案,中間通過springmvc的http介面呼叫.主要的業務邏輯都是在service層中去處理.而web層負責後臺資料的管理,主要是提供給後臺管理員使用.
專案的架構是兩臺阿里雲的linux伺服器,硬體配置為250G硬碟,8G記憶體,4核cpu;每臺機器上部署一個service服務和一個web服務,一個nginx負載均衡服務.
web服務來說,對外暴露的是LBS的外網地址,web呼叫service是呼叫nginx地址,nginx為service做的負載均衡.會將請求負載到兩臺linux服務上去處理,web請求的是本臺伺服器上的nginx地址.
service服務,對外提供的介面服務都是http介面.用LBS對兩臺伺服器上的nginx服務都做了重新負載分配.這樣做的原因是阿里雲做的LBS不能在內網之間呼叫的限制.service對外暴露的是LBS的內網地址,這樣是考慮到service服務的安全性,主要是提供給公司內部的業務系統使用的.
在web呼叫service的nginx負載服務時,就要對nginx進行配置.下面附上配置圖解:
#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協議的一些日誌格式和資料型別 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; #伺服器的叢集 (就是負載的配置資訊,請求來了之後需要nginx分發請求到哪些機器上) upstream test-service { #伺服器叢集名字,此處叫做 test-service server 192.168.13.227:9080;#伺服器配置 weight是權重的意思,權重越大,分配的概率越大。 server 192.168.13.80:8080; } #nginx伺服器的監聽ip和埠,使用的編碼格式 server { listen 80; server_name 192.168.13.80; charset utf-8; #charset koi8-r; #access_log logs/host.access.log main; #nginx的常用配置 location / { #location代表攔截的請求路徑/標識全部攔截 proxy_pass http://test-service; #固定格式標識負載到哪個upstream上,這裡選擇之前配置的test-service上.即http://test-service=http://+test-service proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 3; proxy_send_timeout 30; proxy_read_timeout 30; proxy_buffer_size 128k; proxy_buffers 4 256k; proxy_busy_buffers_size 256k; proxy_temp_file_write_size 256k; #root html; #index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html #錯誤頁面的跳轉到哪些html上在nginx目錄當中都是有的 error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # 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; # } #} }