如何将nginx配置转换为入口kubernetes yaml配置?

vnzz0bqm  于 2023-02-18  发布在  Nginx
关注(0)|答案(3)|浏览(174)

我在Kubernetes上部署了Vue JS。我需要在此页面上配置nginx路由:
https://router.vuejs.org/guide/essentials/history-mode.html#example-server-configurations
配置如下所示:

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

如何将该配置转换为入口kubernetes yaml配置?
我试过了,但是不起作用

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /index.html
  name: backoffice
  namespace: default
  selfLink: /apis/extensions/v1beta1/namespaces/default/ingresses/backoffice
spec:
  rules:
    - host: dev.abc.com
      http:
        paths:
          - backend:
              serviceName: backoffice-svc
              servicePort: 443
            path: /

我试着做了这样的注解,但还是不行:

nginx.ingress.kubernetes.io/app-root: /app1
3zwjbxry

3zwjbxry1#

Ingress可能如下所示:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: some-nice-server
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
  - host: "some.nice.server.com"
    http:
      paths:
      - path: /something1/?(.*)
        backend:
          serviceName: something-1
          servicePort: 8080

这在Kubernetes Ingress-Nginx文档中有很好的解释。
或者,您也可以在注解中使用配置代码段:

...
nginx.ingress.kubernetes.io/configuration-snippet: |
      try_files $uri $uri/ /index.html; 
...
sxissh06

sxissh062#

很有可能,我今天遇到了和你一样的情况。由于nginx入口控制器在另一个隔离的pod中,在nginx入口控制器中生成的nginx规则无法检测到你的服务上的文件和资源。它只是将请求转发到你的服务。放置try_files指令的正确位置应该在你的服务的pod中。
我的工作案例如下:在我的服务pod里有一个nginx容器,我把“try_files $uri $uri//index. html;“文件夹在/etc/nginx/conf.d/默认配置文件中

7d7tgy0s

7d7tgy0s3#

对我来说,最好的方法是更新我的docker文件以加载自定义nginx.conf,设置如下:

worker_processes 1;

events {
  worker_connections 1024;
}

http {
  include mime.types;
  default_type application/octet-stream;
  sendfile on;

  gzip on;
  gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

  server {
    listen 80;
    server_name localhost;
    root /usr/share/nginx/html;

    #Vue router configuration
    location / {
        try_files $uri $uri/ @router;
        index index.html;
    }
    location @router {
        rewrite ^.*$ /index.html last;
    }
  }
}

之后,我只是修改了我的docker文件来加载这个nginx.conf,如上面的代码:

# production stage
FROM nginx:stable-alpine as production-stage

# Copy the custom nginx configuration file to the container
COPY infrastructure/nginx.conf /etc/nginx/nginx.conf

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

# Expose port 80 for the application
EXPOSE 80

# Start the application when the container starts
CMD ["nginx", "-g", "daemon off;"]

然后只需部署您的映像,它应该工作正常,而不会触及您的入口。

相关问题