动态资源、静态资源分别放在不同服务器上
反向代理和负载均衡的结合使用
配置静态资源
server {
listen 80;
server_name pg.yuwei.cc;
root /opt/www;
index index.html;
location ~* .*\.(jpg|png|gif)$ {
root /opt/www/images;
}
}
配置动态资源
server {
listen 80;
server_name api.yuwei.cc;
access_log logs/book.log;
error_log logs/book.error;
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;
}
}
负载均衡调度
upstream static {
server pg.yuwei.cc;
}
upstream api {
server api.yuwei.cc;
}
server {
listen 80;
server_name yuwei.cc;
location ~* \.(jpg|png|gif)$ {
proxy_pass http://static;
proxy_set_header Host $http_host;
}
location ~ \.php {
proxy_pass http://api;
proxy_set_header Host $http_host;
}
}