Welcome Guest, Not a member yet? Register   Sign In
NGINX: Multiple CI Apps on a Single Domain Not Working
#4

(This post was last modified: 07-14-2024, 04:02 PM by sillyquota.)

Hey thanks for the replies! Apologies for the late response, I've been extremely busy of late.

I did however set aside some time to really think this through and I was able to find the solution to my issue. Unfortunately I don't have the complete config file as my server is having issues with the hard drive but this is the resolution I implemented:
Code:
server {
    listen serverip:443 ssl;
    http2 on;
    server_name api.domain.com www.api.domain.com;
    root /home/domaincom/api.domain.com/;
    index index.php index.html index.htm;

    ssl_certificate /etc/pki/tls/certs/api.domain.com.bundle;
    ssl_certificate_key  /etc/pki/tls/private/api.domain.com.key;
    include /etc/nginx/ssl.conf;
    include /etc/nginx/basicheaders.conf;
    include /etc/nginx/nocache.conf;

    # prevent access to sensitive folders
    # alternatively you can replace 'return 404;' with 'deny all;'
    location ~* ^/(app|public|tests|vendor|writable)(/|$) { return 404; }
    location ~* ^/v1/(app|public|tests|vendor|writable)(/|$) { return 404; }
    location ~* ^/v2/beta/(app|public|tests|vendor|writable)(/|$) { return 404; }
    # EDIT: you could probably do something like `location ~* /(app|public|tests|vendor|writable)(/|$) { return 404; }`, get rid of the others, and not need to add additional rules as more apps are added

    # serve app1 via https://api.domain.com/v1
    # app1 approot located at /home/domaincom/api.domain.com/v1/
    #  eg: app1 files would be located throughout
    #  /home/domaincom/api.domain.com/v1/{app,public,writable,tests, ... }
    location ~ /v1 {
        alias /home/domaincom/api.domain.com/v1/public/;

        location ~ [^/]\.php(/|$) {
            rewrite ^/v1/(.*)$ /$1 break;
            alias /home/domaincom/api.domain.com/v1/public/;
            include /etc/nginx/fastcgi.conf;
            if (!-f $document_root$fastcgi_script_name) { return  404; }
            fastcgi_pass    unix:/usr/var/php-fpm/domaincom.sock;
            fastcgi_index  index.php;
        }
    }

    # serve app2 via https://api.domain.com/v2/beta/
    # app2 approot located at /home/domaincom/api.domain.com/v2/
    location ~ /v2/beta/ {
        alias /home/domaincom/api.domain.com/v2/public/;

        location ~ [^/]\.php(/|$) {
            rewrite ^/v2/beta/(.*)$ /$1 break;
            alias /home/domaincom/api.domain.com/v2/public/;
            include /etc/nginx/fastcgi.conf;
            if (!-f $document_root$fastcgi_script_name) { return  404; }
            fastcgi_pass    unix:/usr/var/php-fpm/domaincom.sock;
            fastcgi_index  index.php;
        }
    }

    # serve PHP files (any, not just CodeIgniter) located anywhere within
    # docroot (root /home/domaincom/api.domain.com/) unless otherwise blocked
    location ~ [^/]\.php(/|$) {
        include /etc/nginx/fastcgi.conf;
        if (!-f $document_root$fastcgi_script_name) { return  404; }
        fastcgi_pass    unix:/usr/var/php-fpm/domaincom.sock;
        fastcgi_index  index.php;
    }
}
This allows you to host multiple CI apps on https://api.domain.com under various directories while also being able to serve regular (non-CI) PHP content anywhere else on api.domain.com (as long as the location isn't disallowed)

NOTE: Some of this was reconstructed from memory because my server is offline for repair so YMMV, but I'm pretty sure this is how I got it working. I imagine this might be a bit of an edge-case since this places the approot within the webserver docroot (against most recommendations) and requires explicit location-blocking of sensitive directories. This was as much a learning exercise as it was a necessity for my situation Smile
Reply


Messages In This Thread
RE: NGINX: Multiple CI Apps on a Single Domain Not Working - by sillyquota - 07-14-2024, 04:00 PM



Theme © iAndrew 2016 - Forum software by © MyBB