Net Core API +Vue Nginx集成发布

1、在net core api  Startup.cs 中的Configure中设置默api默认页面 关键代码 app.UseFileServer

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
#if DEBUG
            app.UseStaticFiles(); //开启访问静态文件/wwwroot目录文件,要放在UseRouting前面
#else
            app.UseStaticFiles(new StaticFileOptions()
            {
                ServeUnknownFileTypes = true,
                FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(
                Path.Combine(Path.GetDirectoryName(typeof(Startup).Assembly.Location), "wwwroot")),//wwwroot相当于真实目录
            });

            app.UseFileServer(
            new FileServerOptions()
            {
                FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider( Path.Combine(Path.GetDirectoryName(typeof(Startup).Assembly.Location), "wwwroot")),
                EnableDirectoryBrowsing = true,
                DefaultFilesOptions ={
                    DefaultFileNames = new List<string>(){ "/index.html" }
                },
                EnableDefaultFiles = true
            });  
#endif
            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }

详细使用即讲解,参见官方文档:ASP.NET Core 中的静态文件 | Microsoft Docs

2、vue 项目路由配置base(vite.config.js)(注:本人使用vue3.0)

关键代码:base: '/certManage/', (certManage为nginx设置的代理名称,此处为示例)

注:api接口前缀也需要添加/certManage前缀,各位根据各自实际情况配置,此处不做过多描述

import { defineConfig, loadEnv } from 'vite'
import path from 'path'
import createVitePlugins from './vite/plugins'

// https://vitejs.dev/config/
export default defineConfig(({ mode, command }) => {
  const env = loadEnv(mode, process.cwd())
  
  const alias = {
    // 设置路径
    '~': path.resolve(__dirname, './'),
    // 设置别名
    '@': path.resolve(__dirname, './src')
  }
  if (command === 'serve') {
    // 解决警告You are running the esm-bundler build of vue-i18n.
    alias['vue-i18n'] = 'vue-i18n/dist/vue-i18n.cjs.js'
  }
  return {
    plugins: createVitePlugins(env, command === 'build'),
    resolve: {
      // https://cn.vitejs.dev/config/#resolve-alias
      alias: alias,
      // 导入时想要省略的扩展名列表
      // https://cn.vitejs.dev/config/#resolve-extensions
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
      dedupe: ['vue']
    },
    css: {
      devSourcemap: true //开发模式时启用
    },
    base: '/certManage/',
    // 打包配置
    build: {
      sourcemap: command === 'build' ? false : 'inline',
      outDir: 'dist', //指定输出目录
      assetsDir: 'assets', //指定静态资源存储目录(相对于outDir)
      // 将js、css文件分离到单独文件夹
      rollupOptions: {
        output: {
          chunkFileNames: "static/js/[name]-[hash].js",
          entryFileNames: "static/js/[name]-[hash].js",
          assetFileNames: "static/[ext]/[name]-[hash].[ext]"
        }
      },
    },
    // vite 相关配置
    server: {
      port: 8887,
      host: true,
      open: true,
      proxy: {
        // https://cn.vitejs.dev/config/#server-proxy
        '/dev-api': {
          target: 'http://192.168.10.188:9092/certManage',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/dev-api/, '')
        },
        // '/fileView': {
        //   target: 'http://192.168.10.188:9092/fileView',
        //   changeOrigin: true,
        // },
        // '/msghub': {
        //   target: 'http://localhost:8888',
        //   ws: true,
        //   rewrite: (path) => path.replace(/^\/msgHub/, '')
        // }
      },
    },
  }
})

3、nginx配置

4、net core api接口发布并在IIS中部署(部署略)

5、发布vue项目,并将发布包中的文件拷贝至 api接口发布包下的wwwroot目录下

 

6、启动nginx

7、使用配置的nginx代理地址,在浏览器中访问http://192.168.10.188:9092/certManage 

效果如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值