动静分离
动静分离,就是将JSP、Servlet等动态资源交由Tomcat或其他Web服务器处理,将CSS、js、image等静态资源交由Nginx或其他Http服务器处理,充分发挥各自的优势,减轻其他服务器的压力,搭建更为高效的系统架构。
Nginx动静分析的实现¶
下面要搭建Nginx,环境中有三台Nginx主机;一台用于完成负载均衡,两台Nginx用于存放前面项目中的静态资源。另外,还包含前面的两台Tomcat主机。
复制并配置一台Nginx服务器¶
将原来安装有Nginx的主机作为母机,克隆一台Nginx主机,用于存放静态资源:css、js、image。
存放静态资源¶
复制并配置一台Nginx服务器¶
重复上面,复制的那台Nginx操作!!
修改原始那台nginx配置文件(负载均衡,重要!!!)¶
设置静态资源的负载均衡代理,两台nginx的ip分别为:192.168.31.212、192.168.31.213
web工程¶
linux搭建相关配置¶
服务器配置信息¶
nginx-1
1 #user nobody;
2 worker_processes 1;
3
4 #error_log logs/error.log;
5 #error_log logs/error.log notice;
6 #error_log logs/error.log info;
7
8 #pid logs/nginx.pid;
9
10
11 events {
12 worker_connections 1024;
13 }
14
15
16 http {
17 include mime.types;
18 default_type application/octet-stream;
19
20 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
21 # '$status $body_bytes_sent "$http_referer" '
22 # '"$http_user_agent" "$http_x_forwarded_for"';
23
24 #access_log logs/access.log main;
25
26 sendfile on;
27 #tcp_nopush on;
28
29 #keepalive_timeout 0;
30 keepalive_timeout 65;
31
32 #gzip on;
33 # 配置上传流,用于负载均衡
34 upstream tomcat.cyb.com{
35 server 192.168.31.214:8080 weight=1;
36 server 192.168.31.215:8080 weight=1;
37 }
38 upstream static.cyb.com{
39 server 192.168.31.212:80 weight=1;
40 server 192.168.31.213:80 weight=1;
41 }
42 server {
43 listen 80;
44 server_name localhost;
45
46 #charset koi8-r;
47
48 #access_log logs/host.access.log main;
49
50 location / {
51 # root html;
52 # index index.html index.htm;
53 proxy_pass http://tomcat.cyb.com;
54 }
55 location ~.*\.(jpg|jpeg|js|css|html)$ {
56 proxy_pass http://static.cyb.com;
57 }
58 #error_page 404 /404.html;
59
60 # redirect server error pages to the static page /50x.html
61 #
62 error_page 500 502 503 504 /50x.html;
63 location = /50x.html {
64 root html;
65 }
66
67 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
68 #
69 #location ~ \.php$ {
70 # proxy_pass http://127.0.0.1;
71 #}
72
73 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
74 #
75 #location ~ \.php$ {
76 # root html;
77 # fastcgi_pass 127.0.0.1:9000;
78 # fastcgi_index index.php;
79 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
80 # include fastcgi_params;
81 #}
82
83 # deny access to .htaccess files, if Apache's document root
84 # concurs with nginx's one
85 #
86 #location ~ /\.ht {
87 # deny all;
88 #}
89 }
90
91
92 # another virtual host using mix of IP-, name-, and port-based configuration
93 #
94 #server {
95 # listen 8000;
96 # listen somename:8080;
97 # server_name somename alias another.alias;
98
99 # location / {
100 # root html;
101 # index index.html index.htm;
102 # }
103 #}
104
105
106 # HTTPS server
107 #
108 #server {
109 # listen 443 ssl;
110 # server_name localhost;
111
112 # ssl_certificate cert.pem;
113 # ssl_certificate_key cert.key;
114
115 # ssl_session_cache shared:SSL:1m;
116 # ssl_session_timeout 5m;
117
118 # ssl_ciphers HIGH:!aNULL:!MD5;
119 # ssl_prefer_server_ciphers on;
120
121 # location / {
122 # root html;
123 # index index.html index.htm;
124 # }
125 #}
126
127 }
nginx-2
1 #user nobody;
2 worker_processes 1;
3
4 #error_log logs/error.log;
5 #error_log logs/error.log notice;
6 #error_log logs/error.log info;
7
8 #pid logs/nginx.pid;
9
10
11 events {
12 worker_connections 1024;
13 }
14
15
16 http {
17 include mime.types;
18 default_type application/octet-stream;
19
20 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
21 # '$status $body_bytes_sent "$http_referer" '
22 # '"$http_user_agent" "$http_x_forwarded_for"';
23
24 #access_log logs/access.log main;
25
26 sendfile on;
27 #tcp_nopush on;
28
29 #keepalive_timeout 0;
30 keepalive_timeout 65;
31
32 #gzip on;
33
34 server {
35 listen 80;
36 server_name localhost;
37
38 #charset koi8-r;
39
40 #access_log logs/host.access.log main;
41
42 # location / {
43 # root html;
44 # index index.html index.htm;
45 # }
46 # 通过扩展名方式拦截
47 location ~.*\.(jpg|jpeg|js|css|html)$ {
48 root /opt;
49 }
50
51 #error_page 404 /404.html;
52
53 # redirect server error pages to the static page /50x.html
54 #
55 error_page 500 502 503 504 /50x.html;
56 location = /50x.html {
57 root html;
58 }
59
60 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
61 #
62 #location ~ \.php$ {
63 # proxy_pass http://127.0.0.1;
64 #}
65
66 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
67 #
68 #location ~ \.php$ {
69 # root html;
70 # fastcgi_pass 127.0.0.1:9000;
71 # fastcgi_index index.php;
72 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
73 # include fastcgi_params;
74 #}
75
76 # deny access to .htaccess files, if Apache's document root
77 # concurs with nginx's one
78 #
79 #location ~ /\.ht {
80 # deny all;
81 #}
82 }
83
84
85 # another virtual host using mix of IP-, name-, and port-based configuration
86 #
87 #server {
88 # listen 8000;
89 # listen somename:8080;
90 # server_name somename alias another.alias;
91
92 # location / {
93 # root html;
94 # index index.html index.htm;
95 # }
96 #}
97
98
99 # HTTPS server
100 #
101 #server {
102 # listen 443 ssl;
103 # server_name localhost;
104
105 # ssl_certificate cert.pem;
106 # ssl_certificate_key cert.key;
107
108 # ssl_session_cache shared:SSL:1m;
109 # ssl_session_timeout 5m;
110
111 # ssl_ciphers HIGH:!aNULL:!MD5;
112 # ssl_prefer_server_ciphers on;
113
114 # location / {
115 # root html;
116 # index index.html index.htm;
117 # }
118 #}
119
120 }
nginx-3
1 #user nobody;
2 worker_processes 1;
3
4 #error_log logs/error.log;
5 #error_log logs/error.log notice;
6 #error_log logs/error.log info;
7
8 #pid logs/nginx.pid;
9
10
11 events {
12 worker_connections 1024;
13 }
14
15
16 http {
17 include mime.types;
18 default_type application/octet-stream;
19
20 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
21 # '$status $body_bytes_sent "$http_referer" '
22 # '"$http_user_agent" "$http_x_forwarded_for"';
23
24 #access_log logs/access.log main;
25
26 sendfile on;
27 #tcp_nopush on;
28
29 #keepalive_timeout 0;
30 keepalive_timeout 65;
31
32 #gzip on;
33
34 server {
35 listen 80;
36 server_name localhost;
37
38 #charset koi8-r;
39
40 #access_log logs/host.access.log main;
41
42 # location / {
43 # root html;
44 # index index.html index.htm;
45 # }
46 # 通过扩展名方式拦截
47 location ~.*\.(jpg|jpeg|js|css|html)$ {
48 root /opt;
49 }
50
51 #error_page 404 /404.html;
52
53 # redirect server error pages to the static page /50x.html
54 #
55 error_page 500 502 503 504 /50x.html;
56 location = /50x.html {
57 root html;
58 }
59
60 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
61 #
62 #location ~ \.php$ {
63 # proxy_pass http://127.0.0.1;
64 #}
65
66 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
67 #
68 #location ~ \.php$ {
69 # root html;
70 # fastcgi_pass 127.0.0.1:9000;
71 # fastcgi_index index.php;
72 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
73 # include fastcgi_params;
74 #}
75
76 # deny access to .htaccess files, if Apache's document root
77 # concurs with nginx's one
78 #
79 #location ~ /\.ht {
80 # deny all;
81 #}
82 }
83
84
85 # another virtual host using mix of IP-, name-, and port-based configuration
86 #
87 #server {
88 # listen 8000;
89 # listen somename:8080;
90 # server_name somename alias another.alias;
91
92 # location / {
93 # root html;
94 # index index.html index.htm;
95 # }
96 #}
97
98
99 # HTTPS server
100 #
101 #server {
102 # listen 443 ssl;
103 # server_name localhost;
104
105 # ssl_certificate cert.pem;
106 # ssl_certificate_key cert.key;
107
108 # ssl_session_cache shared:SSL:1m;
109 # ssl_session_timeout 5m;
110
111 # ssl_ciphers HIGH:!aNULL:!MD5;
112 # ssl_prefer_server_ciphers on;
113
114 # location / {
115 # root html;
116 # index index.html index.htm;
117 # }
118 #}
119
120 }
版权声明:
本文为智客工坊「陈彦斌」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。