Получите 404 Not Found от nginx при сборке ng - PullRequest
3 голосов
/ 18 февраля 2020

Я новичок в angular. Я пытаюсь запустить мое приложение angular в docker.

Когда я делаю ng build или ng build -prod и пытаюсь запустить его в docker с этим файлом docker:

### STAGE 1: Build ###
FROM node:12.7-alpine AS build
WORKDIR /usr/src/app
COPY package.json ./
RUN npm install
COPY . .
RUN npm run build

### STAGE 2: Run ###
FROM nginx:1.17.1-alpine
COPY --from=build /usr/src/app/dist/TBH-GUI /usr/share/nginx/html

моя стартовая страница отображается правильно. На этой стартовой странице у меня есть ссылки на некоторые другие компоненты. Когда я нажимаю на ссылку, я получаю 404 Not Found nginx/1.17.1.

Я думаю, у меня ошибка в процессе сборки.

Мой package.json

{
  "name": "tbh-gui",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~9.0.1",
    "@angular/cdk": "~9.0.0",
    "@angular/common": "~9.0.1",
    "@angular/compiler": "~9.0.1",
    "@angular/core": "~9.0.1",
    "@angular/forms": "~9.0.1",
    "@angular/material": "^9.0.0",
    "@angular/platform-browser": "~9.0.1",
    "@angular/platform-browser-dynamic": "~9.0.1",
    "@angular/router": "~9.0.1",
    "rxjs": "~6.5.4",
    "tslib": "^1.10.0",
    "zone.js": "~0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.900.2",
    "@angular/cli": "~9.0.2",
    "@angular/compiler-cli": "~9.0.1",
    "@angular/language-service": "~9.0.1",
    "@types/node": "^12.11.1",
    "@types/jasmine": "~3.3.8",
    "@types/jasminewd2": "~2.0.3",
    "codelyzer": "^5.1.2",
    "jasmine-core": "~3.4.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~4.1.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.1",
    "karma-jasmine": "~2.0.1",
    "karma-jasmine-html-reporter": "^1.4.0",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.15.0",
    "typescript": "~3.7.5"
  }
}

Мой angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "TBH-GUI": {
      "projectType": "application",
      "schematics": {},
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/TBH-GUI",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": true,
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
              "src/styles.css"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "TBH-GUI:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "TBH-GUI:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "TBH-GUI:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
              "src/styles.css"
            ],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "tsconfig.app.json",
              "tsconfig.spec.json",
              "e2e/tsconfig.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "TBH-GUI:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "TBH-GUI:serve:production"
            }
          }
        }
      }
    }
  },
  "defaultProject": "TBH-GUI"
}

Структура моего проекта:

project structure

My nginx .conf:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}


#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
# 
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

Ответы [ 5 ]

1 голос
/ 27 февраля 2020

Ваш ngnix.conf должен выглядеть так:

server {
        listen 0000; # port
        server_name <IP>; 
        index index.html;

        location /api/v1/ {
            proxy_pass http://127.0.0.1:8080; #rest api IP/URL and port
            proxy_http_version 1.1;
            proxy_set_header Connection "";
            proxy_set_header X-Real-IP     $proxy_add_x_forwarded_for;
        }
        location /{
            root "/usr/src/app/dist/TBH-GUI"; # path of angular application upto dist
            try_files $uri $uri/ /index.html;
        }

    }
1 голос
/ 20 февраля 2020

Предоставление порта в вашем Dockerfile :

### STAGE 1: Build ###
FROM node:12.7-alpine AS build
WORKDIR /usr/src/app
COPY package.json ./
RUN npm install
COPY . .
RUN npm run build

### STAGE 2: Run ###
FROM nginx:1.17.1-alpine
EXPOSE YOUR_PORT
COPY --from=build /usr/src/app/dist/TBH-GUI /usr/share/nginx/html

Это базовая c конфигурация для nginx:

server {

  listen YOUR_PORT;
  server_name YOUR_SERVER_NAME;

  location / {
    root /usr/share/nginx/html;

    index index.html index.htm;

    try_files $uri $uri/ /index.html;
  }
}
1 голос
/ 25 февраля 2020

Попробуйте это решение, и я думаю, что оно должно работать, потому что я также работаю с Nginx и Angular Dockerfile должен быть:

# The builder from node image
FROM node:alpine as builder

# build-time variables
RUN apk update && apk add --no-cache make git

# Move our files into directory name "app"
WORKDIR /yourworkdir
COPY /yourworkdir/package*.json /frontend/
RUN npm install
COPY ./yourworkdir /frontend/

RUN npm run build -- --output-path=./dist/out


FROM nginx:alpine

COPY nginx/default.conf /etc/nginx/nginx.conf
COPY --from=builder /frontend/dist/out/ /usr/share/nginx/html

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]

и Nginx конфигурация

 worker_processes  1;

    events {
        worker_connections  1024;
    }

    http {
        include /etc/nginx/mime.types;
        client_max_body_size 100m;

        server {
            listen 80;
            charset utf-8;
            server_name  localhost;

            root   /usr/share/nginx/html;
            index  index.html index.htm;
            include /etc/nginx/mime.types;

            gzip on;
            gzip_types text/css text/javascript application/x-javascript application/json;

            # frontend
            location / {
                try_files $uri $uri/ /index.html;
            }
        }
    }

Я думаю, будет лучше, если вы добавите также docker -композитный файл.

1 голос
/ 18 февраля 2020

Попробуйте добавить это в файл конфигурации ngnix:

http {
    ... other configs
    location / {
        try_files $uri $uri/ index.html
    }
}
0 голосов
/ 26 февраля 2020

Эта ошибка вызвана тем фактом, что маршруты являются виртуальными и фактически не существуют на сервере. Простым решением является использование стратегии маршрутизации ha sh.

В вашем app.module.ts найдите RouterModule.forRoot(routes) и установите для параметра useHash значение true, например: RouterModule.forRoot(routes, { useHash: true }).

Подробнее об этом в документации .

...