Skip to content

Instantly share code, notes, and snippets.

@zeroiszero
Last active September 2, 2019 09:44
Show Gist options
  • Save zeroiszero/18373383d20a249e58302a29866e87ae to your computer and use it in GitHub Desktop.
Save zeroiszero/18373383d20a249e58302a29866e87ae to your computer and use it in GitHub Desktop.
Running Laravel in Kubernetes with path based routing using Ingress. Nginx and Apache configurations.

Kubernetes-Laravel

Background Info

To run different copies of Laravel with Kubernetes and each of these serve as a service for different purpose.

The routing of different services can be achieved by using path-based routing using Ingress.

Issue

When using same domain with path-based routing, all Laravel will treat domain root (e.g. example.com) as application base url.

But, We want each Laravel treat their respective path, (e.g. example.com/service1 and example.com/service2) as their base url.

Solution

We can use Alias in Apache server configurations:

Alias /service1 /var/www/html/public

Add this in .htaccess:

RewriteBase /service1/

OR

In Nginx:

# sub directory
location /sub_dir {  
    alias /usr/share/nginx/html/sub_dir/public;
    try_files $uri $uri/ @sub_dir;  

    location ~ \.(php|phar)(/.*)?$ {
        fastcgi_split_path_info ^(.+\.(?:php|phar))(/.*)$;

        fastcgi_intercept_errors on;
        fastcgi_index  index.php;
        include        fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME $request_filename;
        fastcgi_param  PATH_INFO $fastcgi_path_info;
        fastcgi_pass   php-fpm;
    }
}  

location @sub_dir {
    rewrite /sub_dir/(.*)$ /sub_dir/index.php?/$1 last;  
}
# end sub directory

Content in location block containing the PHP directives subject to change based on your overall nginx configuration

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment