一.安装Nginx,并启动
Nginx 是一个高性能的HTTP 和反向代理 web服务器,也可以用来做负载均衡
brew install nginx
nginx
nginx -s nginx
nginx -s reload
brew info nginx
cd opt/homebrew/Cellar/nginx
cat opt/homebrew/etc/nginx/nginx.conf
二. 将spa应用配置到nginx并访问
- 对nginx配置,使其正常访问
- 调整nginx配置,刷新前端路由能正常访问
- 调整nginx配置,使demo页跨域访问外部接口,控制台显示 接口数据
<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0,minimum-scale=1, user-scalable=no"
/>
<meta name="format-detection" content="telephone=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta http-equiv="Expires" content="0" />
<meta http-equiv="Cache-Control" content="no-cache" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="x-rim-auto-match" content="none" />
<script src="https://unpkg.com/vue@3"></script>
<script src="https://unpkg.com/vue-router@4"></script>
</head>
<body>
<div id="app">
<h1>Hello App!</h1>
<p>
<router-link to="/foo">Go to Foo</router-link>
<router-link to="/bar">Go to Bar</router-link>
<button @click="getData">点击获取数据</button>
<button @click="getHotel">点击获取酒店</button>
</p>
<router-view></router-view>
</div>
<script>
const Foo = { template: "<div>foo</div>" };
const Bar = { template: "<div>bar</div>" };
const routes = [
{ path: "/foo", component: Foo },
{ path: "/bar", component: Bar },
];
const router = VueRouter.createRouter({
history: VueRouter.createWebHistory(),
routes,
});
const app = Vue.createApp({
methods: {
getData: async function (e) {
fetch("/api/health/").then((response) =>
console.log("apiRes:", response)
);
},
},
});
app.use(router);
app.mount("#app");
</script>
</body>
</html>
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8088;
server_name localhost;
location / {
root /opt/homebrew/var/www/demo;
index index.html index.html;
try_files $uri $uri/ /index.html;
autoindex on;
}
location /api {
proxy_pass https://hotel-international.fast-cn.wgine.com;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
include servers/*;
}
三. 安装Docker 并将包含demo spa 的nginx打包到docker,在本地启动访问
Docker 是一个开源的应用容器引擎 ,基于Go 语言并遵从 Apache2.0 协议开源。
Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。
容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),更重要的是容器性能开销极低。
安装docker
brew install --cask docker
创建并配置构建镜像的文本文件Dockerfile
FROM nginx
COPY ./nginx.conf /etc/nginx/nginx.conf
COPY ./index.html /usr/share/nginx/html
EXPOSE 8088
使用docker image build 根据Dockerfile打包成镜像
docker image build ./ -t docker-test:1.0.1
使用docker container create 根据镜像创景容器
docker container create -p 3001:8088 docker-test:1.0.1
使用docker container start 根据镜像创景容器
docker container start xxx
也可以在docker的面板上操作以上命令
|