Nginx动静分离架构架构介绍:
Nginx动静分离简单来说是把动态跟静态请求分开,不能理解成只是单纯地把动态页面和静态页面物理分离。严格意义上说应该是动态请求跟静态请求分开,可以理解成使用Nginx处理静态页面, Tomcat、Resin、PHP、ASP处理动态页面。
动静分离从实现角度来讲大致分为两种:一种是纯粹地把静态文件独立成单独的域名,放在独立的服务器上,也是目前主流推崇的方案;另外一种方法就是动态跟静态文件混合在一起发布,通过Nginx来分开。
Nginx上Web服务器动静分离及完整配置文件代码如下
#定义 Nginx运行的用户和用户组
user www www
#启动进程,通常设置成和CPU的数量相等
worker processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
worker_rlimit_nofile 102400;
pid /usr/local/nginx/logs/nginx.pid;
events{
use epoll;
worker_connections 102400;
multi_accept on;
}
#设定HTTP服务器,利用它的反向代理功能提供负载均衡
http{
include mime .types;
default_type application/octet-stream;
FastCGI_intercept_errors on;
Charset utf-8;
Server_names_hash_bucket_size 128;
client_header_buffer_size 4KB;
large_client_header_buffers 4 32KB;
client_max_body_size 300MB;
client_body_buffer_size 128KB;
tcp_nopush on;
sendfile on;
keepalive_timeout 60;
tcp_nodelay on;
gzip on;
gzip_nin_length 1KB;
gzip_buffers 4 16KB;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4KB;
proxy_butters 4 32KB;
proxy_busy_buffers_size 64KB;
log_format main ‘$remote_addr -
r
e
m
o
t
e
u
s
e
r
[
remote_user [
remoteuser[time_local] “
r
e
q
u
e
s
t
”
‘
‘
request” ‘ ‘
request”‘‘status
b
o
d
y
b
y
t
e
s
s
e
n
t
“
h
t
t
p
r
e
f
e
r
e
r
”
‘
‘
“
body_bytes_sent “http_referer” ‘ ‘ “
bodybytessent“httpreferer”‘‘“http_user_agent” $request_time’;
#负载均衡模块
upstream jvm_webl{
server 192.168.0.134:8080 weight=1 max_fails=2 fail_timeout=30s;
#server 192.168.0.134:8081 weight=1 max_fails=2 fail_timeout=30s;
}
server {
listen 80;
server_name www.yunwei1.com;
access_log logs/yunwei1.log;
#charset koi8-r;
#access_log logs/host.access.log main;
#access_log logs/host.access.log main;
root html/img;
location / {
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://jvm_webl;
}
location ~ .*\.(php|jsp|cgi|shtml)$
{
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://jvm_webl;
}
location ~ .*\.(html|htm|gif|jpeg|jpg|bmp|png|ico|txt|js|css)$
{
root html/img;
expires 30d;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
access_log /data/logs/jvm_web1/access. log main;
error_log /data/logs/jvm_web1/error.log crit;
}
}
配置文件代码中 location ~ . * .( php | jsp | cgi | shtml)表示匹配动态页面请求,然后将通
过 proxy_pass到后端服务器,而 location ~ .*.( html | htm | gif | jpeg | ico | txt | js | css)表示
匹配静态页面请求本地返回。
检查 Nginx配置是否正确即可,然后测试动静分离是否成功,在192.168.0.134服务器启动8080、8081 Tomcat服务或者LAMP服务,删除后端 Tomcat或者LAMP服务器上的某个静态文件,测试是否能访问该文件,如果可以访问,则说明静态资源通过Nginx直接返回了,如果不能访问,则证明动静分离不成功。