vue - 项目部署到 nginx中

本文详细介绍如何将Vue项目通过Nginx进行部署,包括服务器配置、Nginx配置、接口与WebSocket支持、静态文件配置及Vue项目请求配置。同时,提供从构建到部署的完整流程。

Web学习笔记

一、vue项目部署到nginx

  1. 确定服务器ip地址,和端口地址
    a. ip:123.123.123.123
    b. nginx监听的端口:8085
    c. 服务部署端口:8125

  2. 编写nginx的配置文件
    a. 注意监听的端口是8085
    b. 服务接口与websocket的支持配置
    #接口
    location ^~ /api {
    proxy_pass http://127.0.0.1:8125;

    			#websocket支持配置
    			proxy_http_version 1.1;  
    			proxy_set_header Upgrade $http_upgrade;  
    			proxy_set_header Connection "Upgrade";
    			proxy_read_timeout 7200;#必须设置很长时间,否则断掉		
    		}	
    

    c. 静态文件的配置
    #静态文件
    location ^~ /files{
    add_header Access-Control-Allow-Origin *;
    alias C:\Project\systemFiles;
    autoindex off;
    }

    d. 完整配置如下
    #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       8085;
            server_name  127.0.0.1;
    
    		#页面 放在 D:\DevelopmentService\nginx\vue\nginx-1.16.1\html文件夹中
            location / {
                root   html;
                index  index.html index.htm;
            }
    		
    		#接口
    		location ^~ /api {
    			proxy_pass http://127.0.0.1:8125;
    			
    			#websocket支持配置
    			proxy_http_version 1.1;  
    			proxy_set_header Upgrade $http_upgrade;  
    			proxy_set_header Connection "Upgrade";
    			proxy_read_timeout 7200;#必须设置很长时间,否则断掉		
    		}
    		
    		#静态文件
    		location ^~ /files{
    			add_header Access-Control-Allow-Origin *;
    			alias C:\Project\systemFiles;
    			autoindex off;
    		}
        }
    }
    
  3. 接口服务application.yml的名称配置
    #端口和访问路径配置
    server:
    port: 8125
    servlet:
    context-path: /api

  4. vue项目request.js的请求配置
    a. baseURL的地址 = 服务器的ip + nginx的监听访问端口
    import axios from ‘axios’;

    const request = axios.create({
        baseURL: 'http://123.123.123.123:8085',//服务器配置 - 必须改为
        withCredentials: false,
        timeout: 120 * 1000,
    });
    
    export default request;
    
  5. vue项目打包以及部署
    a. 控制台运行npm命令
    npm run build
    b. 复制生成的dist文件夹下的所有文件到nginx文件目录中的html文件夹下

### Vue 项目部署Nginx 的配置步骤 在部署 Vue 项目Nginx 服务器之前,确保已经完成了以下准备工作:确保服务器上安装了 Nginx 和 Node.js,并且拥有一个可以正常运行的 Vue 项目 [^2]。 以下是部署 Vue 项目Nginx 的详细步骤: #### 1. 构建 Vue 项目部署之前,需要将 Vue 项目打包为生产环境可用的静态文件。通常使用 Vue CLI 提供的构建命令: ```bash npm run build ``` 该命令会生成一个 `dist` 目录,其中包含了所有静态资源文件(HTML、CSS、JavaScript 等),这些文件是部署Nginx 所需的内容 [^2]。 #### 2. 将 Vue 项目文件复制到 Nginx 的 Web 根目录 Nginx 默认的 Web 根目录通常是 `/usr/local/var/www` 或 `/usr/share/nginx/html`,具体路径可能因系统配置而异 [^3]。可以将 Vue 项目中生成的 `dist` 文件夹内容复制到该目录中: ```bash cp -r dist/* /usr/local/var/www/ ``` 确保 Nginx 能够访问这些文件,并且文件权限设置正确。 #### 3. 配置 Nginx 服务器块(Server Block) 编辑 Nginx 的配置文件,通常位于 `/etc/nginx/nginx.conf` 或 `/usr/local/etc/nginx/nginx.conf`,也可以在 `/usr/local/etc/nginx/servers/` 中创建一个新的配置文件 [^3]。 一个基本的配置示例如下: ```nginx server { listen 80; # 可根据需要修改为其他端口 server_name example.com; # 域名或 IP 地址 location / { root /usr/local/var/www; # Vue 项目部署的根目录 index index.html; try_files $uri $uri/ =404; # 支持 Vue Router 的 history 模式 } } ``` 如果使用了 Vue Router 的 `history` 模式,必须确保配置中包含 `try_files` 指令,以避免刷新页面时出现 404 错误 [^4]。 #### 4. 重启或重新加载 Nginx 配置 完成配置后,需要重新加载 Nginx 以使更改生效: ```bash sudo nginx -s reload ``` 如果 Nginx 未运行,可以使用以下命令启动: ```bash sudo nginx ``` #### 5. 访问部署Vue 应用 在浏览器中输入服务器的 IP 地址或域名,例如 `http://localhost` 或 `http://your-domain.com`,即可访问部署好的 Vue 项目 [^4]。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值