Nginx

配置片段

静态资源缓存

  1. server {
  2. listen 80;
  3. server_name some-site.com;
  4. root /some/path/to/site;
  5. # 缓存图片
  6. location ~ \.(jpg|png|jpeg|bmp|gif|swf)$ {
  7. root /some/path/to/site/images;
  8. if (-f $request_filename) {
  9. expires 7d;
  10. break;
  11. }
  12. }
  13. # 缓存样式
  14. location ~ \.(css)$ {
  15. root /some/path/to/site/css;
  16. if (-f $request_filename) {
  17. expires 3d;
  18. break;
  19. }
  20. }
  21. # 缓存脚本
  22. location ~ \.(js)$ {
  23. root /some/path/to/site/js;
  24. if (-f $request_filename) {
  25. expires 1d;
  26. break;
  27. }
  28. }
  29. }

反向代理

  1. server {
  2. # 监听端口
  3. listen 80;
  4. # 代理对外域名
  5. server_name proxy-site.com;
  6. location / {
  7. # 转向服务器
  8. proxy_pass http://dest-site.com;
  9. proxy_redirect default;
  10. }
  11. }
  12. # 服务器集群及权重(可选)
  13. upstream dest-site.com {
  14. server 10.0.0.1:80 weight=1;
  15. }

Web Socket 转发

  1. location /wsapp/ {
  2. proxy_pass http://wsbackend;
  3. proxy_http_version 1.1;
  4. proxy_set_header Upgrade $http_upgrade;
  5. proxy_set_header Connection "Upgrade";
  6. }

下一步

订阅更新,获取更多学习资料,请关注我们的 微信公众号

../_images/wechat-mp-qrcode.png小菜学编程

参考文献