基本概念

顾名思义,动静分离是指将静态页面(内容接口)和动态页面(内容接口)分成不同 Web 服务器进行访问,进而提升整个服务的性能和可维护性。
动静分离
可以看作是:反向代理和负载均衡的结合使用

静态资源

1
2
3
4
5
6
7
8
9
server {
listen 80;
server_name res.yuwei.cc;
root /opt/www;
index index.html;
location ~* .*\.(gif|jpg|jpeg|png|bmp|ico|swf|css|js)$ {
root /opt/www/images;
}
}

动态资源

1
2
3
4
5
6
7
8
9
10
server {
listen 80;
server_name api.yuwei.cc;
location / {
proxy_pass http://127.0.0.1:9501;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
}
}

负载调度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
upstream static {
server res.yuwei.cc;
}
upstream api {
server api.yuwei.cc;
}
server {
listen 80;
server_name yuwei.cc;
location ~* \.(gif|jpg|jpeg|png|bmp|ico|swf|css|js)$ {
proxy_pass http://static;
proxy_set_header Host $http_host;
}
location ~ \.php {
proxy_pass http://api;
proxy_set_header Host $http_host;
}
}