【发布时间】:2023-02-08 19:32:35
【问题描述】:
如何使用 express api 和 mongodb 部署静态 Web?
我尝试了所有不同的方法来配置 nginx,但我无法让它在 /api 位置与 api 对话
我测试过我可以使用 api 访问 api 和 mongodb 但我无法从 nginx 服务器访问 api http://localhost:8082/api/ 给我 404
这是堆栈的 docker-compose。
version: "3.8"
services:
js-alist-api:
image: "js-alist-api:latest"
ports:
- "5005:5005"
restart: always
container_name: "js-alist-api"
env_file:
- ./server/.env
volumes:
- "./js-alist-data/public:/public"
- "./server/oldDb.json:/oldDb.json"
js-alist-client:
image: "js-alist-client:latest"
ports:
- "8082:80"
restart: always
container_name: "js-alist-client"
volumes:
#- ./nginx-api.conf:/etc/nginx/sites-available/default.conf
- ./nginx-api.conf:/etc/nginx/conf.d/default.conf
database:
container_name: mongodb
image: mongo:latest
restart: always
volumes:
- "./js-alist-data/mongodb:/data/db"
这是 js-alist-client.docker 文件:
FROM nginx:alpine
COPY ./client-vue/vue/dist/ /usr/share/nginx/html/ # here i copy my static web
EXPOSE 80/tcp
接下来是 nginx-api.conf:
server {
listen 80;
location / {
root /usr/share/nginx/html/;
index index.html index.htm;
}
location /api/ {
proxy_pass http://localhost:5005/;
}
}
如果我访问 http://localhost:5005 就可以了
如果我运行我的 api,它会将数据添加到 mongodb
如果我运行 http://localhost:8082/ 我可以看到静态网页
如果我运行 http://localhost:8082/api 或 http://localhost:8082/api/ 我得到 404。
我也注意到如果我改变:
location / {
root /usr/share/nginx/html/;
index index.html index.htm;
}
到
location / {
root /usr/share/nginx/html2/;
index index.html index.htm;
}
我仍然可以访问静态网站,即使路径不存在。这使我相信 conf 文件未启用。
但是我检查了js-alist-client容器:/etc/nginx # cat nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
它显示/etc/nginx/conf.d/中的所有内容都包含在内
现在我不知道发生了什么,而且我的 conf 文件似乎没有加载。我究竟做错了什么?
【问题讨论】: