首页 > 解决方案 > 资产上的 Nginx 和 Angular 404

问题描述

我有一个基本 href 为“./”的 Angular 9 应用程序。Nginx 仅在 /route1/route2/ 中服务 该应用程序与 nginx 一起正常工作,我可以转到 localhost:8080/route1/route2 并且角度路由开始正常工作。我发现的问题是找不到资产。所以现在,组件被正确加载但没有资产。

assets 文件夹在 src/ 下

Nginx 配置文件:

worker_processes 4;

events { worker_connections 4096; }

http {
    server {
        listen 8080;
        root  /usr/share/nginx/html;
        include /etc/nginx/mime.types;

        location /route1/route2 {
            try_files $uri /app/index.html;
        }
    }
}

Dockerfile:

ARG mode
### STAGE 1: Build ###

FROM node:alpine AS build

COPY package.json package-lock.json ./

RUN npm install && mkdir /app-ui && mv ./node_modules ./app-ui

WORKDIR /app-ui

COPY . .

RUN npm run ng build -- --deploy-url=/app/

### STAGE 2: Run ###

FROM nginx:alpine

# Copy the default nginx.conf provided by the proyect
COPY ./nginx/nginx.conf /etc/nginx/nginx.conf

## Remove default nginx index page
RUN rm -rf /usr/share/nginx/html/*

COPY --from=build /app-ui/dist /usr/share/nginx/html

更新!

我发现图像在 /localhost:8080/app/assets/... 现在的问题是服务器正在尝试从 ...8080/route1/route2/assets... 加载资产

标签: node.jsangulardockernginx

解决方案


问题可能出在angular.json文件上。确保包含src/assetsin assets 数组angular.json


推荐阅读