npm run build
这会在项目目录下生成一个dist
文件夹,包含所有静态文件。
C:\nginx
)conf/nginx.conf
文件进行配置修改nginx.conf
文件中的server
部分:
server {
listen 80;
server_name localhost; # 可以改为你的域名或IP
location / {
root html; # 这里可以改为你的dist文件夹路径
index index.html index.htm;
try_files $uri $uri/ /index.html; # 处理Vue路由的history模式
}
# 可选:配置API代理
location /api/ {
proxy_pass http://your-api-server.com; # 你的后端API地址
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
将Vue项目的dist
文件夹内容复制到Nginx的html
目录下(默认是nginx-1.x.x/html
),或者修改root
指令指向你的dist
文件夹路径。
在Nginx目录下:
start nginx
打开浏览器访问http://localhost
,应该能看到你的Vue应用。
start nginx
nginx -s reload
nginx -s stop
nginx -s quit
try_files $uri $uri/ /index.html;
listen
指令使用其他端口config/index.js
的assetsPublicPath
正确(通常为/
或./
)gzip on;
gzip_min_length 1k;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary on;
server {
listen 443 ssl;
server_name yourdomain.com;
ssl_certificate cert.pem;
ssl_certificate_key cert.key;
# 其他配置...
}
通过以上步骤,你就可以在Windows系统下成功使用Nginx部署Vue2项目了。