Linux下Nginx+Tomcat負載均衡和動靜分離配置要點

本文使用的Linux發(fā)行版:CentOS6.7 下載地址:https://wiki.centos.org/Download
一、安裝Nginx
下載源:wget http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
安裝源:yum install nginx-release-centos-6-0.el6.ngx.noarch.rpm
安裝Nginx:yum install nginx
啟動Nginx服務(wù):service nginx start
停止Nginx服務(wù):service nginx stop
查看Nginx運行狀態(tài):service nginx status
檢查Nginx配置文件:nginx -t
服務(wù)運行中重新加載配置:nginx -s reload
添加Nginx服務(wù)自啟動:chkconfig nginx on
二、修改防火墻規(guī)則
修改Nginx所在主機的防火墻配置:vi /etc/sysconfig/iptables物赶,將nginx使用的端口添加到允許列表中泡态。
例如:-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT (表示允許80端口通過)
修改Tomcat所在主機的防火墻配置:vi /etc/sysconfig/iptables励烦,將tomcat使用的端口添加到允許列表中壤躲。
例如:-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT (表示允許8080端口通過)
如果主機上有多個tomcat的話霞掺,則按此規(guī)則添加多條砸紊,修改對應(yīng)的端口號即可传于。
保存后重啟防火墻:service iptables restart
三、Tomcat負載均衡配置
Nginx啟動時默認加載配置文件/etc/nginx/nginx.conf醉顽,而nginx.conf里會引用/etc/nginx/conf.d目錄里的所有.conf文件沼溜。
因此可以將自己定制的一些配置寫到單獨.conf文件里,只要文件放在/etc/nginx/conf.d這個目錄里即可游添,方便維護系草。
創(chuàng)建tomcats.conf:vi /etc/nginx/conf.d/tomcats.conf,內(nèi)容如下:

  1. upstream tomcats
  • {
  •  ip_hash;
    
  •  server 192.168.0.251:8080;
    
  •  server 192.168.0.251:8081;
    
  •  server 192.168.0.251:8082;
    
  • }

注釋原有的配置

  1. # location / {
  2. # root /usr/share/nginx/html;
  3. # index index.html index.htm;
  4. #}

新增配置默認將請求轉(zhuǎn)發(fā)到tomcats.conf配置的upstream進行處理

location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://tomcats; #與tomcats.conf里配置的upstream同名
}


保存后重新加載配置:nginx -s reload
四唆涝、靜態(tài)資源分離配置
修改default.conf:vi /etc/nginx/conf.d/default.conf找都,添加如下配置:

所有js,css相關(guān)的靜態(tài)資源文件的請求由Nginx處理

location ~.*.(js|css)$ {
root /opt/static-resources; #指定文件路徑
expires 12h; #過期時間為12小時
}

所有圖片等多媒體相關(guān)靜態(tài)資源文件的請求由Nginx處理

location ~.*.(html|jpg|jpeg|png|bmp|gif|ico|mp3|mid|wma|mp4|swf|flv|rar|zip|txt|doc|ppt|xls|pdf)$ {
root /opt/static-resources; #指定文件路徑
expires 7d; #過期時間為7天
}


五、修改SELinux安全規(guī)則
如果訪問Nginx時出現(xiàn)502 Bad Gateway錯誤廊酣,則可能是Nginx主機上的SELinux限制了其使用http訪問權(quán)限引起的能耻,輸入命令setsebool -P httpd_can_network_connect 1 開啟權(quán)限即可。
文件/etc/nginx/nginx.conf完整配置如下:

user nginx;
worker_processes auto;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
worker_rlimit_nofile 100000;

events {
use epoll;
multi_accept on;
worker_connections 1024;
}

http {
include /etc/nginx/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  /var/log/nginx/access.log  main;

sendfile        on;
server_tokens off;
#tcp_nopush     on;

keepalive_timeout  65;

gzip on;
gzip_disable "msie6";
gzip_static on;
gzip_proxied any;
gzip_min_length 1000;
gzip_comp_level 4;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

include /etc/nginx/conf.d/*.conf;

}

文件/etc/nginx/conf.d/default.conf完整配置如下:

server {
listen 80;
server_name localhost;

#charset koi8-r;
#access_log  /var/log/nginx/log/host.access.log  main;

#location / {
#    root   /usr/share/nginx/html;
#    index  index.html index.htm;
#}

location / {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://web_servers;
}

location ~.*\.(js|css)$ {
    root    /opt/static-resources;
    expires     12h;
}

location ~.*\.(html|jpg|jpeg|png|bmp|gif|ico|mp3|mid|wma|mp4|swf|flv|rar|zip|txt|doc|ppt|xls|pdf)$ {
    root    /opt/static-resources;
    expires     7d;
}

#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   /usr/share/nginx/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;
#}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末亡驰,一起剝皮案震驚了整個濱河市晓猛,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌凡辱,老刑警劉巖鞍帝,帶你破解...
    沈念sama閱讀 218,386評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異煞茫,居然都是意外死亡帕涌,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評論 3 394
  • 文/潘曉璐 我一進店門续徽,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蚓曼,“玉大人,你說我怎么就攤上這事钦扭∪野妫” “怎么了?”我有些...
    開封第一講書人閱讀 164,704評論 0 353
  • 文/不壞的土叔 我叫張陵客情,是天一觀的道長其弊。 經(jīng)常有香客問我癞己,道長,這世上最難降的妖魔是什么梭伐? 我笑而不...
    開封第一講書人閱讀 58,702評論 1 294
  • 正文 為了忘掉前任痹雅,我火速辦了婚禮,結(jié)果婚禮上糊识,老公的妹妹穿的比我還像新娘绩社。我一直安慰自己,他們只是感情好赂苗,可當我...
    茶點故事閱讀 67,716評論 6 392
  • 文/花漫 我一把揭開白布愉耙。 她就那樣靜靜地躺著,像睡著了一般拌滋。 火紅的嫁衣襯著肌膚如雪朴沿。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,573評論 1 305
  • 那天败砂,我揣著相機與錄音悯仙,去河邊找鬼。 笑死吠卷,一個胖子當著我的面吹牛锡垄,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播祭隔,決...
    沈念sama閱讀 40,314評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼货岭,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了疾渴?” 一聲冷哼從身側(cè)響起千贯,我...
    開封第一講書人閱讀 39,230評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎搞坝,沒想到半個月后搔谴,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,680評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡桩撮,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,873評論 3 336
  • 正文 我和宋清朗相戀三年敦第,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片店量。...
    茶點故事閱讀 39,991評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡芜果,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出融师,到底是詐尸還是另有隱情右钾,我是刑警寧澤,帶...
    沈念sama閱讀 35,706評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站舀射,受9級特大地震影響窘茁,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜脆烟,卻給世界環(huán)境...
    茶點故事閱讀 41,329評論 3 330
  • 文/蒙蒙 一山林、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧浩淘,春花似錦、人聲如沸吴攒。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,910評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽洼怔。三九已至署惯,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間镣隶,已是汗流浹背极谊。 一陣腳步聲響...
    開封第一講書人閱讀 33,038評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留安岂,地道東北人轻猖。 一個月前我還...
    沈念sama閱讀 48,158評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像域那,于是被迫代替她去往敵國和親咙边。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,941評論 2 355

推薦閱讀更多精彩內(nèi)容