首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于Vue.js分发项目的配置nginx

问用于Vue.js分发项目的配置nginx
EN

Stack Overflow用户
提问于 2018-05-12 02:09:21
回答 2查看 1.9K关注 0票数 0

我在Vue.js dist服务器上使用nginx。

我的Nginx配置如下:

代码语言:javascript
复制
server {
    listen       80;
    server_name  qy.abc.xyz;

    charset utf-8;
    access_log  /var/log/nginx/qy.abc.xyz.access.log  main;

    location / {
        access_log /data/nginx_log/access.log main;
        error_log  /data/nginx_log/error.log error;
        root /data/ldl/repo/vue_user_site/dist;
        index  index.html;

        try_files $uri $uri/ /index.html;

    }
    ...

我可以在浏览器成功的情况下访问qy.abc.xyz,但是您可以知道在我的Vue.js中有很多路由,如果我访问qy.abc.xyz/index或qy.abc.xyz/xxx/xxx,Nginx将得到404 Not Found错误。

您知道,dist目录是由许多hashed-name.js和index.html组成的。

如何为我的项目配置我的Nginx?

编辑-1

我试过使用这个配置,但没有工作。

代码语言:javascript
复制
location / {
    access_log /data/nginx_log/access.log main;
    error_log  /data/nginx_log/error.log error; 
    #root /data/ldl/repo/vue_user_site/dist;
    #index  index.html;

    #try_files $uri $uri/ /index.html;
    return 200 /index.html;
}
EN

回答 2

Stack Overflow用户

发布于 2018-05-12 02:40:55

我需要配置URL重写

你可以试试这个

代码语言:javascript
复制
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
    root <your static file root>
}
location / {
    return 200 /index.html
    # or use rewrite
    rewrite ^.*$ /index.html
}

或者使用诸如node、asp.net core等路由的服务器代码来发送html。

票数 0
EN

Stack Overflow用户

发布于 2018-05-12 09:48:30

按照我在Nginx中的单页应用程序配置设置:

代码语言:javascript
复制
server {

    listen 80;
    server_name qy.abc.xyz;

    #access_log /var/log/logs/qy.abc.xyz.access.log  main; 

    charset utf-8;

    index index.html;
    root /data/ldl/repo/vue_user_site/dist;


    location / {
       try_files $uri $uri/ @rewrites;

    }

    location @rewrites {
        rewrite ^(.+)$ /index.html last;
    }

    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50302273

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档