Nginx remove a path in uri

8/30/2018

I need to remove a part path in a request uri. I'm using an ingress on Goocle Cloud to access to the server and I want that if I go to localhost/symfony/ the symfony path is ignored in the uri request.

At moment It works if I go to localhost/symfony but If I go to localhost/symfony/something I receive 404 default backend. I have tried also to add $1 to set request_url but it not works.

This is my configuration:

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

Any suggestion?

Thansk a lot

Pasquale

-- Pasquale Martucci
google-cloud-platform
kubernetes
kubernetes-ingress
nginx

1 Answer

8/30/2018

You need to use annotations in the Ingress object to rewrite URI. The nginx.ingress.kubernetes.io/rewrite-target annotation targets URI where the traffic must be redirected. Example:

    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

In this case, it will rewrite URI from localhost/symfony/ to localhost/.

Please note that it works for all endpoints described in the Ingress object.

-- Artem Golenyaev
Source: StackOverflow