How to connect Nginx Ingress with Laravel app

12/28/2018

I have laravel image defined with Dockerfile like so...

FROM php:7.2-fpm

# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/

# Set working directory
WORKDIR /var/www

# Install dependencies
RUN apt-get update -y && apt-get install -y openssl zip unzip git libpng-dev

# Install extensions
RUN docker-php-ext-install gd

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www

# Copy existing application directory contents
COPY . /var/www

# Copy existing application directory permissions
COPY --chown=www:www . /var/www

# Change current user to www
USER www

# Expose port 8181 and start php-fpm server
EXPOSE 8181
CMD ["php-fpm"]

I have nginx ingress installed with helm like so...

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-resource
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/proxy-body-size: 10m
    ingress.kubernetes.io/affinity: "cookie"
    nginx.ingress.kubernetes.io/load-balancer: "ip-hash"
spec:
  tls:
    - hosts:
      - api.myweb.com
      - www.myweb.com
      secretName: secret
  rules:
  - host: api.myweb.com
    http:
      paths:
      - backend:
          serviceName: backend-golang
          servicePort: 8080
  - host: www.myweb.com
    http:
      paths:
      - backend:
          serviceName: frontend-laravel
          servicePort: 8181

With this configuration, it return 502 Bad Gateway when I access www.myweb.com

Should I tell nginx that root folder was at /var/www/public..?? How do I do that?

Should I tell nginx that index file was at index.php..?? How do I do that?

My reference was this https://www.digitalocean.com/community/tutorials/how-to-set-up-laravel-nginx-and-mysql-with-docker-compose. But I don't want to use custom nginx,, instead I want to use nginx ingress from helm..

Thank you...

-- karina
kubernetes
laravel
nginx
nginx-ingress

1 Answer

12/28/2018

But I don't want to use custom nginx,, instead I want to use nginx ingress from helm..

I am pretty sure you are conflating two different nginx-es here: the ingress-controller's nginx is designed strictly for doing vhost upstream routing, and is only one of many ingress controller implementations. The other nginx in your story is responsible for dealing with the "HTTP" bits that sit upstream of your php cgi-bin (which is effectively what it is, "fpm" aside). In both cases you don't have to use nginx, you can use other servers, but the fact that in your situation they both are nginx is what I think is leading to the confusion.

You will want a "local" nginx inside your php container to deal with those HTTP bits since (AFAIK) fpm isn't bright enough to do that on its own.

I did wonder if you could use the nginx Ingress annotations to inject the necessary location {} blocks into the ingress controller's nginx's config file, but I am just not sure if nginx needs to be able to see any of the files on the disk of the Pod to work correctly, in which case the annotation trickery won't work.

You are penny pinching, since running a separate nginx in your cluster will likely be a rounding error compared to the overall memory and CPU budget, and for sure has already cost you (and me) more glucose to think through this edge-case than it would to just create the nginx.conf and be done with it.

-- mdaniel
Source: StackOverflow