NGINX Access Log by Location

Hello so I have two platforms where one operates as a subdirectory. I would like to be able to have an access and error log for each application; however it is not working as I intended :(

Here is what I have:

server {
    listen 80 default;
    listen [::]:80;

    root /var/www/html/app1;
    index index.php;

    server_name localhost;

    access_log /var/log/nginx/app1.access.log;
    error_log /var/log/nginx/app1.error.log;    

    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt { log_not_found off; access_log off; allow all; }
    location ~ /\.(?!well-known).* {
            deny all;
            access_log off;
            log_not_found off;
    }
    location ~*  \.(woff|jpg|jpeg|png|gif|ico|css|js)$ {
        access_log off;
        log_not_found off;
        expires 365d;
    }

    location / {
        try_files $uri $uri/ /index.php?$is_args$args;
    }   


    location /app2 {

        try_files $uri $uri/ /app2/index.php$is_args$args;

        access_log /var/log/nginx/app2.access.log;
        error_log  /var/log/nginx/app2.error.log;
    }

    # SECURITY : Deny all attempts to access PHP Files in the uploads directory
    location ~* /(?:uploads|files)/.*\.php$ {
            deny all;
    }

    # PHP : pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;    
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    # Yoast SEO Sitemaps
    location ~ ([^/]*)sitemap-rewrite-disabled(.*).x(m|s)l$ {
            ## this redirects sitemap.xml to /sitemap_index.xml
        rewrite ^/sitemap.xml$ /sitemap_index.xml permanent;
            ## this makes the XML sitemaps work
            rewrite ^/([a-z]+)?-?sitemap.xsl$ /index.php?xsl=$1 last;
        rewrite ^/sitemap_index.xml$ /index.php?sitemap=1 last;
        rewrite ^/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
            ## The following lines are optional for the premium extensions
        ## News SEO
            rewrite ^/news-sitemap.xml$ /index.php?sitemap=wpseo_news last;
        ## Local SEO
        rewrite ^/locations.kml$ /index.php?sitemap=wpseo_local_kml last;
        rewrite ^/geo-sitemap.xml$ /index.php?sitemap=wpseo_local last;
        ## Video SEO
        rewrite ^/video-sitemap.xsl$ /index.php?xsl=video last;
    }
}

Only visits to the app2 homepage get logged in the app2 logs while further into the site like /app2/help will appear in the app1 logs.

Examples:

/help == app1.access.log && app1.error.log OK

/app2 == app2.access.log && app2.error.log OK

/app2/help == app1.access.log && app1.error.log *(want to be in app2 logs) NOT OK


Solution 1:

This is happening because the location that ultimately ends up handling your requests is location ~ \.php$, which inherits its log configuration from the server context. Assuming that the yoast seo sitemap belongs to app1, you'll want a config something like this:

# Use an upstream to future changes easier
upstream _php {
    server unix:/var/run/php/php7.0-fpm.sock;
}

server {
    listen 80 default;
    listen [::]:80;

    root /var/www/html/app1;
    index index.php;

    server_name localhost;

    access_log /var/log/nginx/app1.access.log;
    error_log /var/log/nginx/app1.error.log;    

    # Put php directives in the server context so they can be inherited by all locations
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt { log_not_found off; access_log off; allow all; }

    # Locations that aren't logged can be left outside and shared
    location ~ /\.(?!well-known) {
        deny all;
        access_log off;
        log_not_found off;
    }

    location ~* \.(woff|jpg|jpeg|png|gif|ico|css|js)$ {
        access_log off;
        log_not_found off;
        expires 365d;
    }

    # Everything that logs to app1 should go in here
    location / {
        try_files $uri $uri/ /index.php?$is_args$args;

        # SECURITY : Deny all attempts to access PHP Files in the uploads directory
        location ~* /(?:uploads|files)/.*\.php$ {
            deny all;
        }

        # PHP : pass the PHP scripts to FastCGI server defined in upstream _php
        location ~ \.php$ {
            fastcgi_pass _php;
        }

        # Yoast SEO Sitemaps
        location ~ ([^/]*)sitemap-rewrite-disabled(.*).x(m|s)l$ {
                ## this redirects sitemap.xml to /sitemap_index.xml
            rewrite ^/sitemap.xml$ /sitemap_index.xml permanent;
                ## this makes the XML sitemaps work
                rewrite ^/([a-z]+)?-?sitemap.xsl$ /index.php?xsl=$1 last;
            rewrite ^/sitemap_index.xml$ /index.php?sitemap=1 last;
            rewrite ^/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
                ## The following lines are optional for the premium extensions
            ## News SEO
                rewrite ^/news-sitemap.xml$ /index.php?sitemap=wpseo_news last;
            ## Local SEO
            rewrite ^/locations.kml$ /index.php?sitemap=wpseo_local_kml last;
            rewrite ^/geo-sitemap.xml$ /index.php?sitemap=wpseo_local last;
            ## Video SEO
            rewrite ^/video-sitemap.xsl$ /index.php?xsl=video last;
        }
    }   

    # Everything that logs to app2 should go in here
    location /app2 {
        try_files $uri $uri/ /app2/index.php$is_args$args;

        access_log /var/log/nginx/app2.access.log;
        error_log  /var/log/nginx/app2.error.log;

        # SECURITY : Deny all attempts to access PHP Files in the uploads directory
        location ~* /(?:uploads|files)/.*\.php$ {
            deny all;
        }

        # PHP : pass the PHP scripts to FastCGI server defined in upstream _php
        location ~ \.php$ {
            fastcgi_pass _php;
        }
    }
}

Moving the fastcgi params into the server and using an upstream for the php server means it's not a lot to duplicate.

Solution 2:

You can try the conditional logging with "if". Setup a map for each location and add "if" in the log statement.

map $uri $app1 {
    ~^[app1] 1;
    default 0;
}
map $uri $app2 {
    ~^[app2]  1;
    default 0;
}

access_log /path/to/access-app1.log combined if=$app1;
access_log /path/to/access-app2.log combined if=$app2;

Please note - above statement is written for reference purpose not tested, there might be some syntax changes required.