nginx實現(xiàn)負(fù)載均衡和動靜分離
nginx配置(windows配置),供大家參考,具體內(nèi)容如下
以下是我的項目用到的一份配置文件
#user nobody;
worker_processes 4; #進(jìn)程數(shù),一般cpu是幾核就寫多少
#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;#單個進(jìn)程的最大連接數(shù)
}
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;
proxy_connect_timeout 15s;
proxy_send_timeout 15s;
proxy_read_timeout 15s;
fastcgi_buffers 8 128k;
gzip on;
client_max_body_size 30m;
gzip_min_length 1k;
gzip_buffers 16 64k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain application/x-javascript text/css application/xml application/javascript image/jpeg image/gif image/png image/webp;
gzip_vary on;
#第一個集群
upstream xdx.com{
server 119.10.52.28:8081 weight=100;
server 119.10.52.28:8082 weight=100;
}
#第二個集群,用于上傳圖片所用
upstream xdxfile.com{
server 119.10.52.28:8081;#關(guān)于文件上傳的請求均訪問這個集群
}
#第三個集群
upstream xdx8082.com{
server 119.10.52.28:8082;#8082
}
#第四個集群
upstream xdxali.com{
server 139.196.235.228:8082;#阿里云
}
#第五個集群
upstream xdxaliws.com{
server 139.196.235.228:8886;#阿里云websocket
}
#第一個代理服務(wù)器,監(jiān)聽的是80端口,監(jiān)聽的域名是www.wonyen.com或者wonyen.com
server {
listen 80;#監(jiān)聽的端口
server_name www.wonyen.com wonyen.com;#監(jiān)聽的域名
#charset koi8-r;
#access_log logs/host.access.log main;
#location指的是訪問的路徑,下面這條配置表示當(dāng)訪問網(wǎng)站的根目錄,即訪問wonyen.com或者www.wonyen.com的時候,就去根目錄為html的下面去尋找index.html或者index.htm。在index.html這個頁面里面你可以做一些重定向的工作,跳轉(zhuǎn)到指定頁面
#也可以自定義到某個集群
# location / {
# root html;
# index index.html index.htm;
#}
#所有靜態(tài)請求都交由nginx處理,存放目錄為webapps下的root,過期時間為30天
location ~ \.(css|js|gif|jpg|jpeg|png|bmp|swf|eot|svg|ttf|woff|mp3|mp4|wav|wmv|flv|f4v|json)$ {
root apache-tomcat-8.0.9-windows-x86-yipin-8081/apache-tomcat-8.0.9/webapps/ROOT;
expires 30d;
}
#配置以Att結(jié)尾的請求的處理集群為http://xdxfile.com
location ~ ^/\w+Att{
proxy_pass http://xdxfile.com;
}
#配置以Fill結(jié)尾的請求的處理集群為http://xdxfile.com
location ~ ^/\w+Fill{
proxy_pass http://xdxfile.com;
}
#精準(zhǔn)配置,如果請求名為/crowdFundSave,則
location = /crowdFundSave{
proxy_pass http://xdxfile.com;
}
#精確配置,同上
location = /crowdFundRewardSave{
proxy_pass http://xdxfile.com;
}
#精確配置,同上
location = /garbageCategorySave{
proxy_pass http://xdxfile.com;
}
#精確配置,同上
location = /mailTestAjax{
proxy_pass http://xdx8082.com;
}
#精確配置,同上
location = /mailSendAjax{
proxy_pass http://xdx8082.com;
}
#精確配置,同上
location = /mailOldAjax{
proxy_pass http://xdx8082.com;
}
#精確配置,同上
#location = /wechatAuthority{
#proxy_pass http://xdxali.com;
#}
location ~ ^/ueditor1_4_3{
proxy_pass http://xdxfile.com;
}
#其他所有請求都訪問 http://xdx.com的集群
location ~ .*$ {
index index;
proxy_pass http://xdx.com;
}
#404頁面訪問/Error404.jsp這個location
error_page 404 /Error404.jsp;
#500等頁面也訪問 /Error404.jsp這個location
error_page 500 502 503 504 /Error404.jsp;
#配置請求/Error404.jsp就訪問http://xdxfile.com集群
location = /Error404.jsp {
proxy_pass http://xdxfile.com;
}
# 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
#另外一個代理服務(wù)器,監(jiān)聽8886接口,監(jiān)聽的域名為www.wonyen.com或者wonyen.com
server {
listen 8886;
server_name www.wonyen.com wonyen.com;
#配置若請求為wonyen.com:8086(根目錄),就讓他去訪問http://xdxaliws.com這個集群,這邊配置的是websocket的服務(wù)端
location / {
proxy_pass http://xdxaliws.com;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
# 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;
# }
#}
}
以上就是我的一個配置?;旧闲枰⒁獾亩荚谂渲梦募凶⒔?。我把幾個重要的地方單獨拿出來講一下。
1.集群的配置,我在上面的配置里有定義多個集群,集群按字面的意思理解就是由多臺服務(wù)器構(gòu)成的一個集合,典型的例子如
upstream xdx.com{
server 119.10.52.28:8081 weight=100;
server 119.10.52.28:8082 weight=100;
}
這樣的一個配置,這個集群包含了兩個分支,我們可以在兩臺服務(wù)器上搭建相同的項目(上述的例子是在同樣的服務(wù)器,不同的端口部署相同的項目,因為筆者的服務(wù)器有限),當(dāng)有請求是需要這個集群來處理的時候,nginx會隨機分配,當(dāng)然也可以配置權(quán)重來設(shè)置兩個server的訪問概率。這就是負(fù)載均衡的原理。我們在多臺服務(wù)器上部署相同的項目,利用nginx對請求進(jìn)行轉(zhuǎn)發(fā),這樣可以降低只有一臺服務(wù)器所造成的的負(fù)載過大,而且當(dāng)其中一臺服務(wù)器掛掉以后,nginx會分配另外一臺服務(wù)器來工作,這樣就不會造成服務(wù)停止了。
2.server配置項代表的是一個代理服務(wù)器,上面的文件中我們配置了兩個文件,分別監(jiān)聽wonyen.com(www.wonyen.com)這兩個域名的80和8886端口,所有訪問wonyen.com:80(即wonyen.com)這個域名下的請求,都按照第一個server所定義的規(guī)則去轉(zhuǎn)發(fā),而所有訪問wonyen.com:8886下的請求,則會按照第二個server所定義的規(guī)則去轉(zhuǎn)發(fā)。
3.我們甚至可以通過配置來處理多個域名,看以下的例子。下面的例子我配置了兩個域名的規(guī)則,一個是iis服務(wù)器,一個是tomcat服務(wù)器,主要目的是為了解決80端口只能被一個程序使用的問題。如果iis用了80,tomcat就用不了,反之亦然。所以我給iis和tomcat都分配除了80以外的端口,而把80端口留給niginx。由nginx來分配請求給不同的網(wǎng)站。
復(fù)制代碼
#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;
client_max_body_size 30m;
gzip_min_length 1k;
gzip_buffers 16 64k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain application/x-javascript text/css application/xml application/javascript image/jpeg image/gif image/png image/webp;
gzip_vary on;
upstream achina.com{
server 120.76.129.218:81;
}
upstream qgrani.com{
server 120.76.129.218:8080;
}
server {
listen 80;
server_name www.achinastone.com achinastone.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#其他請求
location ~ .*$ {
index index;
proxy_pass http://achina.com;
}
#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;
}
# 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 80;
server_name www.qgranite.com qgranite.com;
location / {
root html;
index index.html index.htm;
}
#所有靜態(tài)請求都交由nginx處理,存放目錄為webapp
location ~ \.(css|js|gif|jpg|jpeg|png|bmp|swf|eot|svg|ttf|woff|mp3|mp4|wav|wmv|flv|f4v)$ {
root apache-tomcat-8.0.9\webapps\ROOT;
expires 30d;
}
#其他請求
location ~ .*$ {
index index;
proxy_pass http://qgrani.com;
}
}
# 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;
# }
#}
}
4.還有一個就是動靜分離,說得通俗一點就是,把請求數(shù)據(jù)(動)與請求圖片(靜)分開,在tomcat里,當(dāng)我們沒有做動靜分離的時候,tomcat把對圖片的請求也會當(dāng)成一個動態(tài)的請求,而處理動態(tài)請求是比較費性能的(至于為什么,我也不太清楚)。所以我們可以使用nginx配置來實現(xiàn)動靜分離。
我的做法是把其中一個tomcat項目放在nginx的根目錄下,這樣,我們就可以通過以下方式來配置,實現(xiàn)當(dāng)我們訪問圖片,js,css等靜態(tài)資源的時候,都到一個指定的目錄去訪問。這樣做的好處除了節(jié)省性能,還有一個就是我們不需要在所有的負(fù)載均衡服務(wù)器中都同步保留這些靜態(tài)資源,只需要在一個地方保留就好了。配置如下
#所有靜態(tài)請求都交由nginx處理,存放目錄為webapps下的root,過期時間為30天
location ~ \.(css|js|gif|jpg|jpeg|png|bmp|swf|eot|svg|ttf|woff|mp3|mp4|wav|wmv|flv|f4v|json)$ {
root apache-tomcat-8.0.9-windows-x86-yipin-8081/apache-tomcat-8.0.9/webapps/ROOT;
expires 30d;
}
5.既然讀取靜態(tài)資源是從這個目錄讀取的,那么我們必須考慮如何存儲靜態(tài)資源,特別是當(dāng)我們做了負(fù)載均衡以后,在我們的 項目中上傳圖片的請求有可能在任意一個集群的分支中被調(diào)用,比如我們的集群中有A,B兩臺服務(wù)器,他們都有可能做上傳圖片這件事情,如果A調(diào)用了上傳圖片這個請求,則圖片則被上傳到了A這臺服務(wù)器上,反之就是B上面。這樣勢必導(dǎo)致A,B兩臺服務(wù)器上的靜態(tài)圖片是不同步的,當(dāng)我們要訪問這些圖片的時候,(假設(shè)此時我們還沒做動靜分離)就有可能出現(xiàn)訪問不到的情況。由于上一步我們做了動靜分離,現(xiàn)在的問題就演變?yōu)?,如何把這些A,B服務(wù)器上傳的圖片,都同步到我們做動靜分離的那個文件夾下。人工或者程序去同步都很麻煩,我的做法是指定一臺服務(wù)器(也就是nginx安裝的那臺服務(wù)器)的tomcat項目(也就是部署在nginx根目錄下的那個tomcat項目),讓它專門來負(fù)責(zé)上傳圖片的工作,這樣所有的圖片都由這個tomcat項目來上傳,也就保證了靜態(tài)庫中的圖片就是完整的圖片。為此我配置了一個集群,如下。
#第二個集群,用于上傳圖片所用
upstream xdxfile.com{
server 119.10.52.28:8081;#關(guān)于文件上傳的請求均訪問這個集群
}
然后在location中我這樣配置:
#配置以Att結(jié)尾的請求的處理集群為http://xdxfile.com
location ~ ^/\w+Att{
proxy_pass http://xdxfile.com;
}
#配置以Fill結(jié)尾的請求的處理集群為http://xdxfile.com
location ~ ^/\w+Fill{
proxy_pass http://xdxfile.com;
}
因為我把所有涉及到附件上傳的請求都加上了Att或者Fill的后綴,當(dāng)nginx捕獲這些后綴名的請求的時候,就會把他們都交給 http://xdxfile.com這個集群,也就是119.10.52.28:8081這個項目。
6.做了負(fù)載均衡以后,有一個不得不面臨的問題就是內(nèi)存數(shù)據(jù)的同步,我們在程序中有時候會把一些數(shù)據(jù)存放在內(nèi)存中,典型的一類數(shù)據(jù)就是session。如何讓session數(shù)據(jù)在集群的各個分支中共享session呢,這邊要用到一個新的東西,叫做redis。我會在下一篇文章中詳細(xì)地介紹。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Nginx上配置Basic Authorization登錄認(rèn)服務(wù)證的教程
現(xiàn)在我們所使用的包括社交網(wǎng)絡(luò)API等開放平臺授權(quán)獲得用戶的用戶名和密碼一般有兩種認(rèn)證方式,一種是Basic Auth,一種是OAuth,這里我們就來看一下Nginx上配置Basic Authorization登錄認(rèn)服務(wù)證的教程2016-06-06
linux設(shè)置Nginx自動重啟的實現(xiàn)
在Linux系統(tǒng)中,設(shè)置Nginx服務(wù)開機自動啟動及意外停止后自動重啟是保持服務(wù)穩(wěn)定運行的關(guān)鍵步驟,本文詳細(xì)介紹了如何使用systemctl命令和配置systemd服務(wù)文件來實現(xiàn)這一功能,感興趣的可以了解一下2024-09-09
使用referer指令配置Nginx服務(wù)器來防止圖片盜鏈
這篇文章主要介紹了使用referer指令配置Nginx服務(wù)器來防止圖片盜鏈的方法,文中也簡單介紹了referer指令的一些語法和常用參數(shù),需要的朋友可以參考下2015-12-12
Nginx服務(wù)500:Internal Server Error原因之一
這篇文章主要介紹了Nginx服務(wù)500:Internal Server Error原因之一,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
Nginx常用配置以及代理轉(zhuǎn)發(fā)操作詳解
這篇文章主要給大家介紹了關(guān)于Nginx常用配置以及代理轉(zhuǎn)發(fā)的相關(guān)資料,nginx一般被用來做反向代理,將請求轉(zhuǎn)發(fā)到應(yīng)用服務(wù)器上,比如tomcat的應(yīng)用,需要的朋友可以參考下2023-09-09
關(guān)于使用Keepalived實現(xiàn)Nginx的自動重啟及雙主熱備高可用問題
這篇文章主要介紹了使用Keepalived實現(xiàn)Nginx的自動重啟及雙主熱備高可用,本文通過幾個問題解析幫助大家學(xué)習(xí)Keepalived實現(xiàn)Nginx的自動重啟的相關(guān)知識,需要的朋友可以參考下2021-09-09
Nginx性能優(yōu)化之Gzip壓縮設(shè)置詳解(最大程度提高頁面打開速度)
這篇文章主要介紹了Nginx性能優(yōu)化之Gzip壓縮設(shè)置詳解(最大程度提高頁面打開速度),需要的朋友可以參考下2022-01-01

