nginx配置参考

这里只是参考文件,根据自己的项目需求自行配置

  1. # 后端服务地址
  2. upstream api_service {
  3. server xxx.xxx.xxx.xxx:xxxx;
  4. keepalive 2000;
  5. }
  6. server {
  7. listen 80;
  8. server_name xxx.com;
  9. root /usr/local/nginx/html;
  10. location / {
  11. index index.html;
  12. try_files $uri $uri/ /index.html; #react-router 防止页面刷新出现404
  13. }
  14. # 静态文件缓存,启用Cache-Control: max-age、Expires
  15. location ~ ^/(css|js|media)/ {
  16. expires 10y;
  17. access_log off;
  18. add_header Cache-Control "public";
  19. }
  20. # 代理ajax请求
  21. location ^~/api {
  22. rewrite ^/api/(.*)$ /$1 break; # 如果后端接口不是统一以api开头,去掉api前缀
  23. proxy_pass http://api_service/;
  24. proxy_set_header Host $http_host;
  25. proxy_set_header Connection close;
  26. proxy_set_header X-Real-IP $remote_addr;
  27. proxy_set_header X-Forwarded-Server $host;
  28. }
  29. }