本篇文章主要介紹apache和nginx的相關配置,tomcat的相關安裝配置我在前面有寫過一篇,詳細介紹通過兩種配置方法配置nginx。
tomcat配置參考:http://www.cnblogs.com/chenmh/p/5048893.html
源碼安裝
./configure --prefix=/usr/local/apache (安裝目錄) make make install
對於2.4以上版本的apache在進行源碼安裝的時候有的機器會提示缺少部分插件例如:apr、apr-util、pcre,需要先將這些插件安裝好然後再安裝apache
YUM安裝
yum install httpd
配置tomcate負載均衡
進入安裝目錄cnf.d文件夾下面,創建一個.conf後綴的文件
touch apa.conf
vim apa.cnf
Listen 8051 <VirtualHost *:8051> ServerAdmin root@localhost ServerName localhost ErrorLog "/etc/httpd/logs/app_error.log" CustomLog "/etc/httpd/logs/app_access.log" common ProxyPass / balancer://cluster/ stickysession=JSESSIONID|jsessionid nofailover=On lbmethod=byrequests timeout=5 maxattempts=3 ProxyPassReverse / balancer://cluster/ ProxyRequests Off ProxyPreserveHost On <proxy balancer://cluster> #BalancerMember ajp://localhost:8009 route=tomcat_a BalancerMember http://localhost:8080/Front #BalancerMember ajp://localhost:8010 route=tomcat_b BalancerMember http://localhost:8081/Front </proxy> </VirtualHost>
配置文件一開始配置了apache的端口8051,然後在最下面配置了連接tomcat的項目端口,我這裡的配置的apache和tomcat都在一台服務器上面分別使用了不同的端口,在tomcat的webapps路徑下面創建了一個Front項目,項目下面存放了一個test.jsp的測試頁面
cd /usr/local/tomcat1/webapps mkdir Front cd Front touch test.jsp vim test.jsp
<font color=red>testa</font><b>
同樣在tomcat2中也使用同樣的方法創建測試頁面,但是將testa改成testb
接下來確保8051端口被啟用,也可以關閉防火牆,tomcat1、tomcat2都已啟動,啟動方法參考前面我寫的關於tomcat的文章,確保httpd也以啟動,如果已經將httpd加入了啟動服務,
啟動http服務 service httpd start 查看服務啟動狀態 service httpd status
接下來在浏覽器中輸入:http://localhost:8051/test.jsp


如果刷新連接頁面結果是在testa和testb之間切換,說明配置成功。
源碼安裝
創建nginx用戶組 groupadd nginx 創建nginx用戶 useradd -g nginx -s /sbin/nologin nginx 安裝相關插件 yum install –y make zlib-devel openssl-devel pcre-devel 解壓nginx安裝包 tar zxvf nginx-1.8.0.tar.gz 進入安裝包 cd nginx-1.8.0 安裝 ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module make && make install
單個文件配置方法
創建測試頁面
cd /usr/local/tomcat1/webapps mkdir MFront cd MFront touch index.jsp vim index.jsp
<font color=red>MFronttesta</font><b>
tomcat2也同樣操作,將MFronttesta改成MFronttestb
配置文件
cd /usr/local/nginx/conf vim nginx.conf
user nginx nginx;
worker_processes 8;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 102400;
events
{
use epoll;
worker_connections 102400;
}
http
{
include mime.types;
default_type application/octet-stream;
fastcgi_intercept_errors on;
charset utf-8;
server_names_hash_bucket_size 512;
client_header_buffer_size 1024k;
large_client_header_buffers 4 128k;
client_max_body_size 300m;
sendfile on;
tcp_nopush on;
keepalive_timeout 600;
tcp_nodelay on;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 600;
proxy_send_timeout 50;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
###2012-12-19 change nginx logs
log_format main '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $request_time $remote_addr';
upstream Front {
server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s;
server 127.0.0.1:8081 weight=1 max_fails=2 fail_timeout=30s;
}
upstream MFront {
server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s;
server 127.0.0.1:8081 weight=1 max_fails=2 fail_timeout=30s;
}
####chinaapp.sinaapp.com
server {
listen 80;
server_name localhost;
location /Front
{
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://Front;
expires 3d;
}
location /MFront
{
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://MFront;
expires 3d;
}
}
}
注意標紅色的地方,配置文件中我配置了兩個項目分別是Front和MFront,在下面定義server裡面的項目名稱一定要跟上面配置負載的姓名名稱保持一致否則會出錯。
多個配置文件配置方法
對於需要配置多個項目的時候如果所有的信息都配置在一個nginx配置文件當中會導致配置文件內容過長,不好查看,下面就使用多個配置文件的配置方法
cd /usr/local/nginx/conf 創建相關聯的配置文件 touch gzip.conf proxy.conf host.conf web.conf
vim nginx.conf
user nginx nginx;
worker_processes 4; # 工作進程數,為CPU的核心數或者兩倍
error_log logs/error.log crit; # debug|info|notice|warn|error|crit
pid logs/nginx.pid;
events {
use epoll; #Linux最常用支持大並發的事件觸發機制
worker_connections 65535;
}
http {
include mime.types; #設定mime類型,類型由mime.type文件定義
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;
#設定請求緩沖
server_names_hash_bucket_size 256; #增加,原為128
client_header_buffer_size 256k; #增加,原為32k
large_client_header_buffers 4 256k; #增加,原為32k
types_hash_max_size 2048;
proxy_headers_hash_bucket_size 1024;
proxy_headers_hash_max_size 512;
#size limits
client_max_body_size 50m; #允許客戶端請求的最大的單個文件字節數
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
sendfile on;
tcp_nopush on;
keepalive_timeout 120;
tcp_nodelay on;
server_tokens off; #不顯示nginx版本信息
# client_body_buffer_size 1024K;
# client_header_buffer_size 128k;
# client_max_body_size 512m;
# large_client_header_buffers 8 128k;
# client_body_timeout 10;
# client_header_timeout 10;
# keepalive_timeout 60;
# send_timeout 10;
limit_conn_zone $binary_remote_addr zone=perip:10m; #添加limit_zone,限制同一IP並發數
#fastcgi_intercept_errors on; #開啟錯誤頁面跳轉
include gzip.conf; #壓縮配置文件
include proxy.conf; #proxy_cache參數配置文件
include host.conf; #nginx虛擬主機包含文件目錄
include web.conf; #後端WEB服務器列表文件
}
注意最後的include這4個相關配置文件,其它的一些相關配置分別在這四個配置文件中配置。
cat gzip.conf
#啟動預壓縮功能,對所有類型的文件都有效 gzip_static on; #開啟nginx_static後,對於任何文件都會先查找是否有對應的gz文件 #找不到預壓縮文件,進行動態壓縮 gzip on; gzip_min_length 1k; #設置最小的壓縮值,單位為bytes.超過設置的min_length的值會進行壓縮,小於的不壓縮. gzip_comp_level 3; #壓縮等級設置,1-9,1是最小壓縮,速度也是最快的;9剛好相反,最大的壓縮,速度是最慢的,消耗的CPU資源也多 gzip_buffers 16 64k; #設置系統的緩存大小,以存儲GZIP壓縮結果的數據流,它可以避免nginx頻煩向系統申請壓縮空間大小 gzip_types text/plain application/x-javascript text/css text/javascript; #關於gzip_types,如果你想讓圖片也開啟gzip壓縮,那麼用以下這段吧: #gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php image/jpeg image/gif image/png; #gzip公共配置 gzip_http_version 1.1; #識別http的協議版本(1.0/1.1) gzip_proxied any; #設置使用代理時是否進行壓縮,默認是off的 gzip_vary on; #和http頭有關系,加個vary頭,代理判斷是否需要壓縮 gzip_disable "MSIE [1-6]."; #禁用IE6的gzip壓縮
cat proxy.conf
proxy_temp_path /tmp/proxy_temp; proxy_cache_path /tmp/proxy_cache levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=3g; #client_body_buffer_size 512k; #原為512k proxy_connect_timeout 50; #代理連接超時 proxy_read_timeout 600; #代理發送超時 proxy_send_timeout 600; #代理接收超時 proxy_buffer_size 128k; #代理緩沖大小,原為32k proxy_buffers 16 256k; #代理緩沖,原為4 64k proxy_busy_buffers_size 512k; #高負荷下緩沖大小,原為128k proxy_temp_file_write_size 1024m; #proxy緩存臨時文件的大小原為128k #proxy_ignore_client_abort on; #不允許代理端主動關閉連接 proxy_next_upstream error timeout invalid_header http_500 http_503 http_404 http_502 http_504; ~
cat host.conf
server {
listen 80;
server_name localhost;
#charset GB2312;
location /MFront {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_pass http://MFront;
}
location /Front {
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_pass http://Front;
}
}
cat web.conf
upstream MFront {
server 127.0.0.1:8080 max_fails=1 fail_timeout=60s;
server 127.0.0.1:8081 max_fails=1 fail_timeout=60s;
}
upstream Front {
server 127.0.0.1:8080 max_fails=1 fail_timeout=60s;
server 127.0.0.1:8081 max_fails=1 fail_timeout=60s;
}
特別要注意web.conf配置文件中的項目要和host.conf配置文件中的項目名稱保持一致
接下來在url中輸入:http://localhost/MFront/


同樣顯示內容會在MFronttesta和MFronttestb之間切換說明配置正確
配置nginx啟動
vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: 345 99 20
# description: nginx
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
echo "Nginx servicestart success."
;;
stop)
kill -s QUIT $(cat $PIDF)
echo "Nginx service stopsuccess."
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
echo"reload Nginx configsuccess."
;;
*)
echo "Usage: $0{start|stop|restart|reload}"
exit 1
esac
授予可執行文件 chmod +x /etc/init.d/nginx
#添加到啟動服務 chkconfig --add nginx #配置自動啟動 chkconfig --level 2345 nginx on
對於配置文件中還有很多參數的調配這裡就暫時不做細說,如果後面有時間的話會單獨講。
備注:
作者:pursuer.chen
博客:http://www.cnblogs.com/chenmh
本站點所有隨筆都是原創,歡迎轉載;但轉載時必須注明文章來源,且在文章開頭明顯處給明鏈接。
《歡迎交流討論》