其實本人比較喜歡nginx跑靜態和作負載反向代理,動態php仍是交給apache處理比較穩定,jsp就交給tomcat、resin或jboss。nginx跑靜態的能力是無與倫比的,是目前web服務器裏最強的。nginx和apache、tomcat、resin的動靜分離配置其實很簡單,就幾句配置,穩定性也很是好。javascript
一、nginx和apache的動靜分離配置:php
把下面配置放到nginx配置文件相應的server { }裏面,若是使用其餘端口號,改一下就行:css
#全部php的動態頁面均交由apache處理html
1
2
3
4
5
6
|
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直接讀取不通過apachejava
1
2
3
4
|
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的,把下面的配置註釋掉:nginx
1
2
3
4
5
6
7
8
|
# 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,說明動靜分離生效。web
2.niginx和tomcat的動靜分離配置:
#主配置文件配置apache
1
|
[root@localhost ~]
# vi /usr/local/nginx/conf/nginx.conf
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
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管理腳本,方便使用後端
1
|
[root@localhost ~]
# vi /etc/init.d/nginx
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
#!/bin/bash
# chkconfig: 345 99 20
# description: Nginx servicecontrol script
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
|
1
2
3
4
|
[root@localhost ~]
# chmod +x /etc/init.d/nginx
[root@localhost ~]
# service nginx restart
[root@localhost ~]
# chkconfig --add nginx
[root@localhost ~]
# chkconfig nginx on
|