安装nginx
- 下载
官网地址:http://nginx.org/en/download.html - 使用
nginx在windows中使用只需把压缩包解压,然后双击nginx.exe可执行文件,然后在浏览器中输入访问地址即可。
- 注意
(1)nginx文件必须放在英文文件下(即nginx访问路径中不能有中文),否则会启动失败
(2)如果双击nginx.exe可执行文件之后出现访问失败请检查端口是否被占用(默认为80端口)
操作nginx
- 命令行操作
打开命令行窗口,进入nginx.exe所在目录然后进行启动停止重启等操作,这里使用powershell窗口打开无法进行操作会报错。 - 常用命令
//启动nginx
start nginx.exe
//停止nginx
nginx -s stop
//安全退出nginx
nginx -s quit
//重启nginx
nginx -s reload
运行vue打包项目
将vue打包
在项目中运行npm run build命令将项目打包成静态文件,注意vue中处理跨域的代理服务器baseUrl不能为'/'具体原因后面介绍。另外防止vue打包项目打开空白页现象,还需对vue配置文件进行修改,具体配置方案参考百度。
将vue打包项目部署到nginx
- 将vue静态文件拷贝到nginx所在目录
将vue打包后的dist目录中所有的文件拷贝到nginx服务的html目录中
配置nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /api/ {
proxy_pass http://10.1.2.21:9999/;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
# location ~ \.php$ {
# proxy_pass http://10.1.2.21:9999;
# }
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
# location ~ \.php$ {
# root html;
# fastcgi_pass http://10.1.2.21:9999;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
#include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
解释:
(1)默认端口为80,可将server配置项中的listen修改为其它端口
(2)配置http项的server中location属性,如下:
location / {
root html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /api/ {
proxy_pass http://10.1.2.21:9999/;
}
- 其中'location / '属性中root为vue静态文件存放目录;index 为vue项目入口文件;try_files配置项的作用防止打开项目404;
- 'location /api/'配置项是为跨域环境下配置的代理服务器,其中/api/为vue项目中的代理服务器的baseUrl,注意:这里的location后面的路径不能为'/’或者'~',否则会出现访问404的情况,这就是为什么前面vue项目中baseUrl不能设置为'/'的原因。
- proxy_pass后面的路径为跨域的代理地址
使用nginx启动vue项目
双击nginx.exe可执行文件,如果nginx已经启动且修改了配置文件则使用nginx -s reload重加载服务。启动成功后在浏览器中输入访问地址即可在本地运行vue打包后的项目。
nginx本地运行vue项目的场景
这里主要结合sentry监控服务使用:vue接入sentry监控平台时,在使用sourcemaps功能定位项目中错误具体位置时,需要在生产环境中才能触发sentry的sourcemaps功能。此时需要在本地启动一个http服务模拟线上环境,而前后端分离项目大多存在跨域现象所以需要nginx服务启动vue打包后的项目解决跨域问题。