lundi 14 décembre 2020

Serve a PHP/Lavel application in a domain subfolder and a reverse proxy

So I have a complex deployment structure, where a public SSL capable domain should host many applications in subfolder URLs. For instance:

And so on... App1 is already running an Laravel API and it's fine.

Now I need to deploy a running large Laravel application to this model and would like to do that doing minimal changes to app code - would be great if I need to change only nginx config files.

Currently App2 is in its own domain (olddomain.com) but we need to bring it to a subfolder to standardize the deploy environment.

First of all, the reverse proxy (running as a docker container) defines a app2.subfolder.conf to redirect subfolder requests:

location /app2 {     
  return 301 $scheme://$host/app2/;
}  

location ^~ /app2/ {
     include /config/nginx/proxy.conf;
     resolver 127.0.0.11 valid=30s; # docker dns
     set $upstream_app app2-webserver; # docker app2' webserver name
     set $upstream_port 80;     
     set $upstream_proto http;
     proxy_pass $upstream_proto://$upstream_app:$upstream_port;
}

Here, app2-webserver is another nginx (docker) instance that exclusively handles requests to app2 (also a container). Our architecture isolates every app webservers.

Then, the app2-webserver has its own nginx configuration:

server {
    listen 80 default;
    access_log /var/log/nginx/application.access.log;

    root /application/public;

    location / {
        index  index.html index.htm index.php;
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        # zero-day exploit defense.
        try_files                       $uri =404;

        # performance boosts for PHP
        sendfile                        on;
        tcp_nopush                      off;
        keepalive_requests              0;

        # proxy buffers - no 502 errors!
        proxy_buffer_size               128k;
        proxy_buffers                   4 256k;
        proxy_busy_buffers_size         256k;

        # fastcgi buffers - no 502 errors!
        fastcgi_buffering               on;
        fastcgi_buffer_size             32k;
        fastcgi_buffers                 16 16k;

        # max timeouts (should match php.ini)
        fastcgi_connect_timeout         600;
        fastcgi_send_timeout            600;
        fastcgi_read_timeout            36000;

        fastcgi_pass php-fpm:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PHP_VALUE "error_log=/var/log/nginx/application_php_errors.log";
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny  all;
    }

}

The Laravel app itself is on another container (named php-fpm) on port 9000.

How can I change app2-webserver's config to serve the app2?

PS: Both the reverse proxy and the app2-webserver are in the same docker network and they can communicate between each other.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire