vue3 快速入门 (七) : Vue打包并部署到Nginx服务器上

1. 本文环境

  • Vue版本 : 3.4.29
  • Node.js版本 : v20.15.0
  • 系统 : Windows11 64位
  • IDE : VsCode

2. vue打包,减少体积

打包之前我们可以对包的体积进行一些优化,比如可以实现自动按需引入、开启图片压缩、文件压缩等,具体详见这篇文章 :

分享基于vite对vue3项目打包优化经验(持续更新)

3. nginx 部署 vue网页

那么怎么将vue网页部署到nginx上呢 ? 我们这里以windows nginx为例。

linuxnginx部署也是类似的,不过Linux上需要注意的一步是,需要使用chmod 755 文件名设置好每个文件的权限

3.1 打包Vue

vue项目根目录下,使用npm run build打包vue项目,然后会得到dist文件夹,这个就是vue打包的产物。

在这里插入图片描述

3.2 下载Nginx并解压

下载地址 : nginx for Windows

解压后双击 nginx.exe,可以看到一个cmd框一闪而过。
接着打开浏览器,输入http://localhost/,回车,可以看到如下画面,说明nginx启动成功了。

在这里插入图片描述

3.3 将dist文件夹复制到Nginx

dist文件夹复制到Nginx的根目录下,并重命名为myvue
在这里插入图片描述

3.4 配置nginx

用记事本打开/conf/nginx.conf文件,在server节点下配置如下代码

location / {
    root   myvue;
    index  index.html index.htm;
}

3.5 reload nginx

cmd进入nginx根目录下,然后执行nginx -reload,更改配置,使用新配置启动新的工作进程。

nginx -s reload

如果nginx -s reload没有生效,可以使用taskkill /IM nginx.exe /F杀死全部nginx进程,然后再双击nginx.exe重启nginx

3.6 运行项目

在浏览器里点击获取数据按钮,可以发现,会报跨域的错误。

在这里插入图片描述
所以,同样的,我们需要对nginx做跨域的处理,修改nginx.conf

location /myapi {
	proxy_pass http://www.baidu.com/;
	add_header Access-Control-Allow-Origin *;
}

在上面的配置中,/myapi 是要跨域访问的接口路径,http://www.baidu.com/ 是允许跨域访问的源地址。这样,来自 http://www.baidu.com/ 的请求就可以跨域访问该接口。
完整的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   myvue;
			index  index.html index.htm;
		}

		location /myapi {
			proxy_pass http://www.baidu.com/;
			add_header Access-Control-Allow-Origin *;
		}

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


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

再次nginx -s reload,刷新网页,点击获取数据按钮,可以看到HTTP请求成功了。

在这里插入图片描述

该问题具体可以看 怎么利用 nginx 解决跨域问题?

4. vue快速入门系列文章

vue3 快速入门 (一) : 环境配置与搭建
vue3 快速入门 (二) : 第一个Vue网页
vue3 快速入门 (三) : vue中的图片路径
Vue3 快速入门 (四) : 使用路由实现页面跳转
vue3 快速入门 (五) : Flex布局
vue3 快速入门 (六) : vue中调用HTTP请求
vue3 快速入门 (七) : Vue打包并部署到Nginx服务器上

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

氦客

你的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值