动态资源、静态资源分别放在不同服务器上
Nginx 配置动静分离 - 图1
反向代理和负载均衡的结合使用

配置静态资源

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

配置动态资源

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

负载均衡调度

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