我为我的react应用程序准备了这样一个Dockerfile:
FROM node:16 AS builder
WORKDIR /app
COPY . /app
RUN npm ci
RUN npm run build
FROM nginx:1.23.2-alpine
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=builder /app/dist /usr/share/nginx/html
RUN touch /var/run/nginx.pid
RUN chown -R nginx:nginx /var/run/nginx.pid /usr/share/nginx/html /var/cache/nginx /var/log/nginx /etc/nginx/conf.d
USER nginx
EXPOSE 8000
CMD ["nginx", "-g", "daemon off;"]
我有这样一个nginx.conf
server_tokens off;
server {
listen 8000;
server_name localhost;
location /todofastapi {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri /index.html;
}
}
我用命令构建图像:docker build -t react-app .
我运行容器化的应用程序:docker run --name kotek -d -p 8000:8000 react-app
Nginx站起来了,但是当我转到locahlost:8000/todofastapi
或locahlost:8000/todofastapi/
时,我看到了默认的Nginx错误页面。
我希望此应用程序可以从locahlost:8000/todofastapi
或locahlost:8000/todofastapi/
访问
这是日志
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf differs from the packaged version
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2023/09/19 19:04:54 [warn] 1#1: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:2
nginx: [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:2
2023/09/19 19:04:54 [notice] 1#1: using the "epoll" event method
2023/09/19 19:04:54 [notice] 1#1: nginx/1.23.2
2023/09/19 19:04:54 [notice] 1#1: built by gcc 11.2.1 20220219 (Alpine 11.2.1_git20220219)
2023/09/19 19:04:54 [notice] 1#1: OS: Linux 5.15.49-linuxkit-pr
2023/09/19 19:04:54 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/09/19 19:04:54 [notice] 1#1: start worker processes
2023/09/19 19:04:54 [notice] 1#1: start worker process 29
2023/09/19 19:04:54 [notice] 1#1: start worker process 30
2023/09/19 19:04:54 [notice] 1#1: start worker process 31
2023/09/19 19:04:54 [notice] 1#1: start worker process 32
2023/09/19 19:04:54 [notice] 1#1: start worker process 33
172.17.0.1 - - [19/Sep/2023:19:04:58 +0000] "GET / HTTP/1.1" 404 548 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36" "-"
2023/09/19 19:04:58 [error] 30#30: *2 "/etc/nginx/html/index.html" is not found (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost:8000"
2023/09/19 19:04:59 [error] 30#30: *2 "/etc/nginx/html/index.html" is not found (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET / HTTP/1.1", host: "localhost:8000"
172.17.0.1 - - [19/Sep/2023:19:04:59 +0000] "GET / HTTP/1.1" 404 548 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36" "-"
172.17.0.1 - - [19/Sep/2023:19:05:03 +0000] "GET /todofastapi HTTP/1.1" 404 548 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36" "-"
2023/09/19 19:05:03 [error] 30#30: *2 open() "/etc/nginx/html/index.html" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /todofastapi HTTP/1.1", host: "localhost:8000"
2023/09/19 19:05:51 [error] 30#30: *2 open() "/etc/nginx/html/index.html" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /todofastapi HTTP/1.1", host: "localhost:8000"
172.17.0.1 - - [19/Sep/2023:19:05:51 +0000] "GET /todofastapi HTTP/1.1" 404 548 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36" "-"
2023/09/19 19:06:27 [error] 30#30: *2 open() "/etc/nginx/html/index.html" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /todofastapi HTTP/1.1", host: "localhost:8000"
172.17.0.1 - - [19/Sep/2023:19:06:27 +0000] "GET /todofastapi HTTP/1.1" 404 548 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36" "-"
172.17.0.1 - - [19/Sep/2023:19:06:27 +0000] "GET /favicon.ico HTTP/1.1" 404 548 "http://localhost:8000/todofastapi" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36" "-"
2023/09/19 19:06:27 [error] 30#30: *2 open() "/etc/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost:8000", referrer: "http://localhost:8000/todofastapi"
2023/09/19 19:06:29 [error] 30#30: *2 open() "/etc/nginx/html/index.html" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /todofastapi/ HTTP/1.1", host: "localhost:8000"
172.17.0.1 - - [19/Sep/2023:19:06:29 +0000] "GET /todofastapi/ HTTP/1.1" 404 548 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36" "-"
1条答案
按热度按时间7hiiyaii1#
好吧,找到答案了。我的nginx conf文件现在看起来像这样:
我在这里找到了帮助How can config Nginx properly, when base property added inside of Vite.config.js?
它需要同时使用
alias
和root
指令,alias
在内部,root
在location
外部