1.问题描述:
测试开发时一切正常,ng部署vue后网站可以访问,但是刷新后就会提示404。
2.解决方式:
ng暂停服务后,修改配置文件:nginx.conf 改为 以下内容即可解决。
worker_processes 1;
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 8106;#监听端口号,3LP网站配置端口
server_name localhost 172.0.0.1;#访问地址
location / {
root "D:\www\dist"; #前端vue打包的路径
try_files $uri $uri/ @router;
index index.html index.htm;
}
location @router {
rewrite ^.*$ /index.html last;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
#跨域配置
location ^~ /api/ { # 这里设的是location /api/,根据需要灵活修改
proxy_pass http://后端api请求地址/; # 后端域名 注意:最后“/”根据实际情况看是否需要删除
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS'; # 根据需要灵活修改HTTP请求方法
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
}
}
}