#StackBounty: #nginx #log-files NGINX Access Log by Location

Bounty: 50

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


Get this bounty!!!

Leave a Reply