在开发Vue.js项目时,Nginx常用于反向代理、静态资源服务或部署。以下是安装与配置指南:


1. 安装Nginx

  • Ubuntu/Debian

    sudo apt update && sudo apt install nginx
    

    📌 安装完成后,可通过 http://localhost 验证服务是否正常启动

    nginx
  • CentOS/RHEL

    sudo yum install nginx
    

    📌 启动服务:sudo systemctl start nginx


2. 配置Nginx代理Vue.js服务

编辑配置文件:

location / {
    proxy_pass http://localhost:8080; # Vue开发服务器端口
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
}

📄 保存后运行 sudo nginx -t 检查语法,再执行 sudo systemctl reload nginx


3. 集成Nginx与Vue.js项目

  • 静态资源部署
    将Vue构建后的 dist 目录复制到Nginx的 html 目录

    配置文件
  • 反向代理配置
    在Nginx中设置代理到后端API:

    location /api {
      proxy_pass http://backend_server:3000;
    }
    

4. 常见问题排查

  • 服务未启动?检查日志:tail -f /var/log/nginx/error.log
  • 配置错误?重启Nginx:sudo systemctl restart nginx
  • 需要防火墙开放端口:sudo ufw allow 80

📌 扩展阅读了解更多Nginx配置技巧
🎉 成功配置后,你的Vue应用将可通过Nginx高效部署!