vue 打包部署

vue-cli版本:@vue/cli 4.2.3

执行 npm run build 命令

拷贝 生成的dist文件夹

将dist文件夹放到nginx 与conf的同级目录下即可

配置nginx

#user  nobody; #nginx使用用户
worker_processes  1; #设置值和CPU核心数一致

#日志位置和日志级别
#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虚拟主机的配置
    server {
        listen       80;#监听端口
        server_name  localhost;#域名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html; #站点目录
            index  index.html index.htm;
        }

        #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://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    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;
        #}
    }

    #配置自己的项目,如果是vue的需要是vue编译之后的文件
     server {
            listen       8080;#监听端口
            server_name  localhost;#域名

            #charset koi8-r;

            #access_log  logs/host.access.log  main;

            location / {
                try_files $uri $uri/ /index.html;
                root   dist; #站点目录
                index  index.html index.htm;
            }

            #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;
            }

        }

    # 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;
    #    }
    #}

}
vue Nginx配置

注意:配置中的  try_files $uri $uri/ /index.html; 我vue采用的是    history.pushState 的路由

官方解释:

  如果你在 history 模式下使用 Vue Router,是无法搭配简单的静态文件服务器的。例如,如果你使用 Vue Router 为 /todos/42/ 定义了一个路由,开发服务器已经配置了相应的 localhost:3000/todos/42响应,但是一个为生产环境构建架设的简单的静态服务器会却会返回 404。

为了解决这个问题,你需要配置生产环境服务器,将任何没有匹配到静态文件的请求回退到 index.html。Vue Router 的文档提供了常用服务器配置:https://router.vuejs.org/zh/guide/essentials/history-mode.html。

这样就不会出现404页面了

nginx 启动

D:\mySoftwareEnvironment\nginx-1.16.1> nginx.exe -c conf\vueconfig.conf

vueconfig.conf 我自定义的nginx配置文件

nginx -s reload  重新加载

posted @ 2020-03-19 19:40  Angry-rookie  阅读(149)  评论(0)    收藏  举报