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
效果如下: