无法使Angular在本地域上运行

huangapple go评论67阅读模式
英文:

Unable to get Angular to work on a local domain

问题

以下是您提供的内容的翻译部分:

我试图在自定义本地域名 blog.dev 上启动 Angular 应用程序。Angular 应用程序在 https://localhost:4200 上正常工作。但是在 blog.dev 域上无法正常工作。

我的 Angular 部署文件如下所示:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: client-depl
spec:
  replicas: 1
  selector:
    matchLabels:
      app: client
  template:
    metadata:
      labels:
        app: client
    spec:
      containers:
        - name: client
          image: pinakinc/client
          ports:
            - containerPort: 4200
---
apiVersion: v1
kind: Service
metadata:
  name: client-srv
spec:
  selector:
    app: client
  type: LoadBalancer
  ports:
    - name: client
      protocol: TCP
      port: 80
      targetPort: 4200
      nodePort: 31000

Nginx 配置文件如下所示:

worker_processes 1;

events {
  worker_connections 1024;
}

http {
  server {
    listen 80;
    server_name localhost;

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

    gzip on;
    gzip_min_length 1000;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types text/plain text/css application/json application/javascript 
    application/x-javascript text/xml application/xml application/xml+rss 
    text/javascript;

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

Angular.json 文件如下所示:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "Angular-Mean": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:application": {
          "strict": true
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/Angular-Mean",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
              "src/styles.css"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "500kb",
                  "maximumError": "1mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "2kb",
                  "maximumError": "4kb"
                }
              ],
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "outputHashing": "all"
            },
            "development": {
              "buildOptimizer": false,
              "optimization": false,
              "vendorChunk": true,
              "extractLicenses": false,
              "sourceMap": true,
              "namedChunks": true
            }
          },
          "defaultConfiguration": "production"
        },
        "serve": {
          "options": {
            "host": "blog.dev",
            "port": 80
          },
          "builder": "@angular-devkit/build-angular:dev-server",
          "configurations": {
            "production": {
              "browserTarget": "Angular-Mean:build:production"
            },
            "development": {
              "browserTarget": "Angular-Mean:build:development"
            }
          },
          "defaultConfiguration": "development"
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "Angular-Mean: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",
              "./node_modules/bootstrap/dist/css/bootstrap.css",
              "src/styles.css"
            ],
            "scripts": []
          }
        }
      }
    }
  },
  "defaultProject": "Angular-Mean"
}

Host 文件条目如下所示:

127.0.5.1 blog.dev

我正在使用 Minikube。

英文:

无法使Angular在本地域上运行I am trying to bring up angular application on custom local domain blog.dev. The angular application is working fine of https://localhost:4200. But on the blog.dev domain, it is not working.

My angular deployment file is given below

apiVersion: apps/v1
kind: Deployment
metadata: 
name: client-depl
spec: 
replicas: 1
selector: 
matchLabels:
app: client
template: 
metadata:
labels:
app: client 
spec:
containers:
- name: client
image: pinakinc/client
ports:
- containerPort: 4200
---
apiVersion: v1
kind: Service
metadata:
name: client-srv
spec:
selector:
app: client
type: LoadBalancer
ports: 
- name: client
protocol: TCP
port: 80
targetPort: 4200
nodePort: 31000

The nginx.conf is given below

worker_processes  1;
events {
worker_connections  1024;
}
http {
server {
listen 80;
server_name  localhost;
root   /usr/share/nginx/html;
index  index.html index.htm;
include /etc/nginx/mime.types;
gzip on;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css application/json application/javascript 
application/x-javascript text/xml application/xml application/xml+rss 
text/javascript;
location / {
try_files $uri $uri/ /index.html;
}
}
}

The angular.json is given below

{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"Angular-Mean": {
"projectType": "application",
"schematics": {
"@schematics/angular:application": {
"strict": true
}
},
"root": "",
"sourceRoot": "src",
"prefix": "app",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/Angular-Mean",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
"src/styles.css"
],
"scripts": []
},
"configurations": {
"production": {
"budgets": [
{
"type": "initial",
"maximumWarning": "500kb",
"maximumError": "1mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "2kb",
"maximumError": "4kb"
}
],
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"outputHashing": "all"
},
"development": {
"buildOptimizer": false,
"optimization": false,
"vendorChunk": true,
"extractLicenses": false,
"sourceMap": true,
"namedChunks": true
}
},
"defaultConfiguration": "production"
},
"serve": {
"options": {
"host": "blog.dev",
"port": 80
},
"builder": "@angular-devkit/build-angular:dev-server",
"configurations": {
"production": {
"browserTarget": "Angular-Mean:build:production"
},
"development": {
"browserTarget": "Angular-Mean:build:development"
}
},
"defaultConfiguration": "development"
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "Angular-Mean: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",
"./node_modules/bootstrap/dist/css/bootstrap.css",
"src/styles.css"
],
"scripts": []
}
}
}
}
},
"defaultProject": "Angular-Mean"
}

The host file entry is given below

127.0.5.1 blog.dev

I am using minikube

答案1

得分: 0

Your application running on port 4200 so the target port should be 4200 in service also.

So traffic flow like service port 80 > target port 4200 > container port 4200

Target point and container port should get matched.

  • name: client
    image: pinakinc/client
    ports:

    • containerPort: 4200

apiVersion: v1
kind: Service
metadata:
name: client-srv
spec:
selector:
app: client
type: LoadBalancer
ports:
- name: client
protocol: TCP
port: 80
targetPort: 4200
nodePort: 31000

英文:

Your application running on port 4200 so the target port should be 4200 in service also.

So traffic flow like service port 80 > target port 4200 > container port 4200

Target point and container port should get matched.

- name: client
image: pinakinc/client
ports:
- containerPort: 4200
---
apiVersion: v1
kind: Service
metadata:
name: client-srv
spec:
selector:
app: client
type: LoadBalancer
ports: 
- name: client
protocol: TCP
port: 80
targetPort: 4200
nodePort: 31000

答案2

得分: 0

我已经成功解决了问题。解决问题所采取的措施如下:

  1. 更改了nginx配置文件如下:
worker_processes  1;

events {
  worker_connections  1024;
}

http {
  server {
    listen 80;
    server_name  localhost;

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

    gzip on;
    gzip_min_length 1000;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types text/plain text/css application/json 
    application/javascript 
    application/x-javascript text/xml application/xml 
    application/xml+rss 
    text/javascript;

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

部署文件如下:

apiVersion: apps/v1
kind: Deployment
metadata: 
  name: client-depl
spec: 
  replicas: 1
  selector: 
    matchLabels:
      app: client
  template: 
    metadata:
      labels:
        app: client 
    spec:
      containers:
        - name: client
          image: pinakinc/client
          ports:
            - containerPort: 4200
---
apiVersion: v1
kind: Service
metadata:
  name: client-srv
spec:
  selector:
    app: client
  type: LoadBalancer
  ports: 
    - name: client
      protocol: TCP
      port: 80
      targetPort: 80
      nodePort: 32000

希望这对你有所帮助。

英文:

I was able to resolve the issue. Things that were done to resolve the issue

  1. nginx config file was changed as follows:

     worker_processes  1;
    events {
    worker_connections  1024;
    }
    http {
    server {
    listen 80;
    server_name  localhost;
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    include /etc/nginx/mime.types;
    gzip on;
    gzip_min_length 1000;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types text/plain text/css application/json 
    application/javascript 
    application/x-javascript text/xml application/xml 
    application/xml+rss 
    text/javascript;
    location / {
    try_files $uri $uri/ /index.html;
    }
    }
    }
    

The deployment file is given below

apiVersion: apps/v1
kind: Deployment
metadata: 
name: client-depl
spec: 
replicas: 1
selector: 
matchLabels:
app: client
template: 
metadata:
labels:
app: client 
spec:
containers:
- name: client
image: pinakinc/client
ports:
- containerPort: 4200
---
apiVersion: v1
kind: Service
metadata:
name: client-srv
spec:
selector:
app: client
type: LoadBalancer
ports: 
- name: client
protocol: TCP
port: 80
targetPort: 80
nodePort: 32000

huangapple
  • 本文由 发表于 2023年2月10日 15:51:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75408249.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定