按位置划分的 NGINX 访问日志

Ray*_*Ray 8 nginx log-files

您好,我有两个平台,其中一个作为子目录运行。我希望能够为每个应用程序提供访问和错误日​​志;但是它没有按我的意图工作:(

这是我所拥有的:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

只有对 app2 主页的访问才会记录在 app2 日志中,而进一步进入网站(如 /app2/help)将出现在 app1 日志中。

例子:

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

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

/app2/help == app1.access.log && app1.error.log *(想要在 app2 日志中)不行

kol*_*ack 5

发生这种情况是因为最终处理您的请求的位置是location ~ \.php$,它从服务器上下文继承其日志配置。假设 yoast seo 站点地图属于 app1,您需要这样的配置:

# 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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

将 fastcgi 参数移动到服务器并为 php 服务器使用上游意味着复制的内容并不多。