其實本人比較喜歡nginx跑靜態和做負載反向代理,動態php還是交給apache處理比較穩定,jsp就交給tomcat、resin或jboss。nginx跑靜態的能力是無與倫比的,是目前web服務器里最強的。nginx和apache、tomcat、resin的動靜分離配置其實很簡單,就幾句配置,穩定性也非常好。
1、nginx和apache的動靜分離配置:
把下面配置放到nginx配置文件相應的server { }里面,如果使用其他端口號,改一下就行:
#所有php的動態頁面均交由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;}
#所有靜態文件由nginx直接讀取不經過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; }
如果之前設置了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就生效,如圖所示,標頭顯示nginx,phpinfo里面顯示是apache,說明動靜分離生效。
2.niginx和tomcat的動靜分離配置:
#主配置文件配置
[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壓縮功能設置 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網站程序根目錄,一般nginx與tomcat在同一個目錄 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請求交給tomcat處理 proxy_redirect off; proxy_set_header Host $host; #后端的Web服務器可以通過X-Forwarded-For獲取用戶真實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; #允許客戶端請求的最大單文件字節數 client_body_buffer_size 128k; #緩沖區代理緩沖用戶端請求的最大字節數 proxy_connect_timeout 90; #nginx跟后端服務器連接超時時間(代理連接超時) proxy_read_timeout 90; #連接成功后,后端服務器響應時間(代理接收超時) proxy_buffer_size 4k; #設置代理服務器(nginx)保存用戶頭信息的緩沖區大小 proxy_buffers 6 32k; #proxy_buffers緩沖區,網頁平均在32k以下的話,這樣設置 proxy_busy_buffers_size 64k;#高負荷下緩沖大小(proxy_buffers*2) proxy_temp_file_write_size 64k; #設定緩存文件夾大小,大于這個值,將從upstream服務器傳 } location ~ .*/.(gif|jpg|png|bmp|swf)$ #由nginx處理靜態頁面 { expires 30d; #使用expires緩存模塊,緩存到客戶端30天 } location ~ .*/.( jsp|js|css)?$ { expires 1d; } error_page 404 /404.html; #錯誤頁面 error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
#編寫nginx啟動、停止、重啟等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