首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按地点分列的NGINX访问日志

按地点分列的NGINX访问日志
EN

Server Fault用户
提问于 2018-05-08 13:39:34
回答 4查看 22.4K关注 0票数 10

你好,我有两个平台,其中一个作为子目录运行。我希望能够为每个应用程序提供一个访问和错误日志;但是,它并不像我预期的那样工作:(

以下是我所拥有的:

代码语言:javascript
复制
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;
    }
}

只有对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日志中)

EN

回答 4

Server Fault用户

回答已采纳

发布于 2018-05-18 18:43:55

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

代码语言:javascript
复制
# 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;
        }
    }
}

将快速not参数移动到服务器中,并为php服务器使用上游,这意味着复制的事情不多。

票数 7
EN

Server Fault用户

发布于 2018-05-17 10:11:43

您可以使用"if“尝试条件日志记录。为每个位置设置一个映射,并在log语句中添加"if“。

代码语言:javascript
复制
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;

请注意-上面的语句是为参考目的编写的,没有经过测试,可能需要进行一些语法更改。

票数 1
EN

Server Fault用户

发布于 2018-05-08 17:54:01

配置看起来是正确的。如果不使用=~,则nginx在位置上的匹配时间最长,因此以/app2/开头的任何内容(包括/app2/helper )都将匹配第二个位置,并优先于location /

我不能使用您发布的完全相同的配置来重现您的问题。我猜你没有重启nginx。重新装载可能还不够。

票数 0
EN
页面原文内容由Server Fault提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://serverfault.com/questions/911279

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档