Nginx删除uri中的路径

问题描述 投票:0回答:1

我需要删除请求uri中的部分路径。我正在使用Goocle Cloud上的入口来访问服务器,如果我转到localhost / symfony /我想要在uri请求中忽略symfony路径。

目前它是有效的,如果我去localhost / symfony但如果我去localhost / symfony /东西我收到404默认后端。我也尝试添加$ 1来设置request_url,但它不起作用。

这是我的配置:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: symfony-nginx
  namespace: default
  labels:
    app: symfony-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: symfony-nginx
  template:
    metadata:
      labels:
        app: symfony-nginx
    spec:
      volumes:
        - name: symfony-nginx
          configMap:
            name: symfony-nginx-configmap
      containers:
      - name: symfony-nginx
        image: nginx:stable
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: /etc/nginx/conf.d
          name: symfony-nginx
        resources:
          requests:
            memory: 32Mi
            cpu: 10m
          limits:
            memory: 64Mi
            cpu: 30m
        readinessProbe:
          httpGet:
            path: /nginx-health
            port: 80
          initialDelaySeconds: 15
          timeoutSeconds: 1
          periodSeconds: 10
        livenessProbe:
          httpGet:
            path: /nginx-health
            port: 80
          initialDelaySeconds: 15
          timeoutSeconds: 1
          periodSeconds: 10
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: symfony-nginx-configmap
  namespace: default
data:
  default.conf: |
    server {
            server_name php-docker.local;
            error_log  /var/log/nginx/error.log;
            access_log /var/log/nginx/access.log;
            root /var/www/html/symfony/public;

            proxy_buffering off;

            location = /nginx-health {
                access_log off;
                return 200 "healthy\n";
            }

            location / {
                try_files $uri /index.php$is_args$args;
            }

            location ~ ^/index\.php(/|$) {

                set $request_url $request_uri;
                if ($request_uri ~ ^/symfony(.*)$ ) {
                    set $request_url /;
                }
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass symfony:9000;
                include fastcgi_params;
                fastcgi_param  REQUEST_URI $request_url;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param DOCUMENT_ROOT $document_root;
                internal;
            }
            location ~ \.php$ {
                    return 404;
            }
        }
---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: symfony-nginx-hpa
  namespace: default
  labels:
    app: symfony-nginx
spec:
  scaleTargetRef:
    kind: Deployment
    name: symfony-nginx
    apiVersion: apps/v1
  minReplicas: 1
  maxReplicas: 10
  targetCPUUtilizationPercentage: 70
---
apiVersion: v1
kind: Service
metadata:
  name: symfony-nginx-np
  namespace: default
  labels:
    app: symfony-nginx
spec:
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
  selector:
    app: symfony-nginx
  type: NodePort
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: symfony-nginx-ingress
spec:
  rules:
  - http:
      paths:
      - path: /symfony
        backend:
          serviceName: symfony-nginx-np
          servicePort: 80

有什么建议吗?

非常感谢

帕斯夸莱

nginx kubernetes google-cloud-platform kubernetes-ingress
1个回答
1
投票

您需要在Ingress对象中使用注释来重写URI。 nginx.ingress.kubernetes.io/rewrite-target注释的目标是必须重定向流量的URI。例:

    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      annotations:
        nginx.ingress.kubernetes.io/rewrite-target: /
      name: symfony-nginx-ingress
    spec:
      rules:
      - http:
          paths:
          - path: /symfony
            backend:
              serviceName: symfony-nginx-np
              servicePort: 80

在这种情况下,它会将localhost/symfony/中的URI重写为localhost/

请注意,它适用于Ingress对象中描述的所有端点。

© www.soinside.com 2019 - 2024. All rights reserved.