网站的目录结构如下:
www/
├─ index.html
├─blog/
│ ├─dist/
│ ├─index.html
blog下是打包好的vue+vue-router项目。
此时nginx配置如下
server {
listen 443;
server_name localhost;
ssl on;
root /var/ftp/www/;
index index.html;
#ssl配置省略
location /api/ {
proxy_pass https://localhost:3010;
}
}
直接访问https://域名/blog/,是没问题的,可是当刷新的时候,blog的子页面就会404,查阅后发现是因为vue-router开启了history模式,于是按照官方文档的方法在server里添加了
try_files $uri $uri/ /index.html;
这样虽然不会404,但是页面全部转到了根目录的index.html,于是调整了nginx的配置如下:
server {
listen 443;
server_name localhost;
ssl on;
root /var/ftp/www/;
index index.html;
#ssl配置省略
location /blog/ {
root /var/ftp/www/blog/;
index index.html;
}
location /api/ {
proxy_pass https://localhost:3010;
}
}
这时候访问https://域名/blog/ 直接就报了404错误,然后试着修改nginx配置如下:
server {
listen 443;
server_name localhost;
ssl on;
#ssl配置省略
location /blog/ {
root /var/ftp/www/blog/;
index index.html;
}
location /api/ {
proxy_pass https://localhost:3010;
}
location / {
root /var/ftp/www/;
index index.html;
}
}
仍然会报404错误,请问是哪里搞错了,折腾了一晚上头发都快掉光了Q A Q
哈哈...头发掉光了再回答你!
注意
location
后面写成/api/
或/api
是不一样的(特别注意哦),你的node启动了服务,例如是8080的端口, nginx代理直接代理过去就好了,如果你想把/blog代理不需要加后面的斜杠哦,而且域名访问的时候应该是https://www.xxx.com/blog
即可,另外root
的路径指向,应该从根目录开始哦