其實(shí)本人比較喜歡nginx跑靜態(tài)和做負(fù)載反向代理,動(dòng)態(tài)php還是交給apache處理比較穩(wěn)定,jsp就交給tomcat、resin或jboss。nginx跑靜態(tài)的能力是無與倫比的,是目前web服務(wù)器里最強(qiáng)的。nginx和apache、tomcat、resin的動(dòng)靜分離配置其實(shí)很簡單,就幾句配置,穩(wěn)定性也非常好。
1、nginx和apache的動(dòng)靜分離配置:
把下面配置放到nginx配置文件相應(yīng)的server { }里面,如果使用其他端口號(hào),改一下就行:
#所有php的動(dòng)態(tài)頁面均交由apache處理
location ~ /.(php)?$ {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://127.0.0.1:88;} #所有靜態(tài)文件由nginx直接讀取不經(jīng)過apache
location ~ .*/.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)${ expires 15d; }location ~ .*/.(js|css)?${ expires 1h; } 如果之前設(shè)置了FastCGI的,把下面的配置注釋掉:
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ /.php$ {# root /var/www/html;# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# include fastcgi.conf;#}
重啟nginx就生效,如圖所示,標(biāo)頭顯示nginx,phpinfo里面顯示是apache,說明動(dòng)靜分離生效。
2.niginx和tomcat的動(dòng)靜分離配置:
#主配置文件配置
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
user nginx;worker_processes 1;error_log logs/error.log;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; keepalive_timeout 65; #gzip壓縮功能設(shè)置 gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/x-javascripttext/css application/xml; gzip_vary on; server { listen 80; server_name www.test.com; location / { #jsp網(wǎng)站程序根目錄,一般nginx與tomcat在同一個(gè)目錄 root /usr/local/tomcat/webapps/ROOT; index index.html index.jsp index.html; } location ~ .*.jsp$ { index index.jsp; proxy_pass http://127.0.0.1:8080; #來自jsp請(qǐng)求交給tomcat處理 proxy_redirect off; proxy_set_header Host $host; #后端的Web服務(wù)器可以通過X-Forwarded-For獲取用戶真實(shí)IP proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; #允許客戶端請(qǐng)求的最大單文件字節(jié)數(shù) client_body_buffer_size 128k; #緩沖區(qū)代理緩沖用戶端請(qǐng)求的最大字節(jié)數(shù) proxy_connect_timeout 90; #nginx跟后端服務(wù)器連接超時(shí)時(shí)間(代理連接超時(shí)) proxy_read_timeout 90; #連接成功后,后端服務(wù)器響應(yīng)時(shí)間(代理接收超時(shí)) proxy_buffer_size 4k; #設(shè)置代理服務(wù)器(nginx)保存用戶頭信息的緩沖區(qū)大小 proxy_buffers 6 32k; #proxy_buffers緩沖區(qū),網(wǎng)頁平均在32k以下的話,這樣設(shè)置 proxy_busy_buffers_size 64k;#高負(fù)荷下緩沖大?。╬roxy_buffers*2) proxy_temp_file_write_size 64k; #設(shè)定緩存文件夾大小,大于這個(gè)值,將從upstream服務(wù)器傳 } location ~ .*/.(gif|jpg|png|bmp|swf)$ #由nginx處理靜態(tài)頁面 { expires 30d; #使用expires緩存模塊,緩存到客戶端30天 } location ~ .*/.( jsp|js|css)?$ { expires 1d; } error_page 404 /404.html; #錯(cuò)誤頁面 error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } #編寫nginx啟動(dòng)、停止、重啟等SysV管理腳本,方便使用
[root@localhost ~]# vi /etc/init.d/nginx
#!/bin/bash# chkconfig: 345 99 20# description: Nginx servicecontrol scriptPROG="/usr/local/nginx/sbin/nginx"PIDF="/usr/local/nginx/logs/nginx.pid"case "$1" instart)$PROGecho "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 1esac [root@localhost ~]# chmod +x /etc/init.d/nginx[root@localhost ~]# service nginx restart[root@localhost ~]# chkconfig --add nginx[root@localhost ~]# chkconfig nginx on



















