Nginx 为每个域提供单独的访问日志

问题描述 投票:0回答:2

我将 Nginx 与 Typo3 结合使用。我的 Typo3 安装有大约 8 个域。一切都像魅力一样。现在我遇到的问题是,我想使用适用于每个域的 AWStats,但我不知道如何分离每个域的访问日志。在下面你可以看到我的配置实际运行情况:

里面的配置文件

sites-available
:

server {
    listen        127.0.0.1:80;
    server_name    www.domain1.de
                       www.domain2.de
                       www.domain3.de
    root        "/var/www/oz/htdocs/";
    disable_symlinks if_not_owner;
    location ~ /\.ht {
        deny all;
    }

    location ~ ^/cgi-bin/ {
        deny all;
    }


    # PHP is enabled
    index index.php index.html index.htm;
    location ~ \.php(/|$) {
        try_files $fastcgi_script_name =404;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include /etc/nginx/fastcgi_params;
        set $path_info $fastcgi_path_info;
        fastcgi_param PATH_INFO $path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_index index.php;
        fastcgi_pass unix:/var/www/oz/conf/sockets/nginx-php-fcgi.sock;
        fastcgi_read_timeout 300;
               fastcgi_buffer_size 128k;
               fastcgi_buffers 256 4k;
               fastcgi_busy_buffers_size 256k;
    }

    location = / {
        error_page    403 /.errorFiles/coming-soon.html;
    }
    location /.errorFiles/ {
        alias /usr/share/liveconfig/html/;
    }


#### NGINX Typo3 Config - Start #####


      location = /favicon.ico {
               log_not_found off;
               access_log off;
      }

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

      # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
      location ~ /\. {
               deny all;
               access_log off;
               log_not_found off;
      }

       client_max_body_size 200M;

       location ~ /\.(js|css)$ {
               expires 604800s;
       }

       location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
                expires 365d;
               
       }
        if (!-e $request_filename){
               rewrite ^/(.+)\.(\d+)\.(php|js|css|png|jpg|gif|gzip)$ /$1.$3 last;
       }

       location ~* ^/fileadmin/(.*/)?_recycler_/ {
               deny all;
       }
       location ~* ^/fileadmin/templates/.*(\.txt|\.ts)$ {
               deny all;
       }
       location ~* ^/typo3conf/ext/[^/]+/Resources/Private/ {
               deny all;
       }
       location ~* ^/(typo3/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico) {
       }

       location / {
                       if ($query_string ~ ".+") {
                               return 405;
                       }
                       if ($http_cookie ~ 'nc_staticfilecache|be_typo_user|fe_typo_user' ) {
                               return 405;
                       } # pass POST requests to PHP
                       if ($request_method !~ ^(GET|HEAD)$ ) {
                               return 405;
                       }
                       if ($http_pragma = 'no-cache') {
                               return 405;
                       }
                       if ($http_cache_control = 'no-cache') {
                               return 405;
                       }
                       error_page 405 = @nocache;

                       try_files /typo3temp/tx_ncstaticfilecache/$host${request_uri}index.html @nocache;
       }

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

#### NGINX Typo3 Config - End #####
}

server {
    listen        127.0.0.1:80;
    server_name    domain1.de;
    rewrite        ^/(.*)$ "http://www.domain1.de/$1" permanent;
}

server {
    listen        127.0.0.1:80;
    server_name    domain2.de;
    rewrite        ^/(.*)$ "http://www.domain2.de/$1" permanent;
}

server {
    listen        127.0.0.1:80;
    server_name    domain3.de;
    rewrite        ^/(.*)$ "http://www.domain3.de/$1" permanent;
}

nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 5000;
    multi_accept on;
        use epoll;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

#   ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
#   ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

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

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_min_length 256;
        gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

我尝试将其在服务器块内分开。但我无法让它发挥作用。这里有人可以帮助我或给我一些提示吗?

typo3-6.2.x access-log awstats
2个回答
9
投票

每个

server
都可以覆盖其自己的访问日志位置:

server {
    listen        127.0.0.1:80;
    server_name    domain1.de;
    access_log /var/log/nginx/domain1-access.log;
    error_log  /var/log/nginx/domain1-error.log;
    rewrite        ^/(.*)$ "http://www.domain1.de/$1" permanent;
}

server {
    listen        127.0.0.1:80;
    server_name    domain2.de;
    access_log /var/log/nginx/domain2-access.log;
    error_log  /var/log/nginx/domain2-error.log;
    rewrite        ^/(.*)$ "http://www.domain2.de/$1" permanent;
}

server {
    listen        127.0.0.1:80;
    server_name    domain3.de;
    access_log /var/log/nginx/domain3-access.log;
    error_log  /var/log/nginx/domain3-error.log;
    rewrite        ^/(.*)$ "http://www.domain3.de/$1" permanent;
}

0
投票

我通常会制作一个文件

/etc/nginx/conf.d/log.conf

内容:

access_log "/var/log/nginx/access-log-$scheme-$server_name.log";

这将为您提供 http 和 https 服务器的日志文件。 $server_name 是安全的,因为它来自您的配置。

不要忘记确保您的 logrotate 会轮换这些文件。

还可以使用 - (减号),因为 nginx 似乎不喜欢在变量末尾有 _ (下划线)。我认为允许使用像 $http_ 这样的标头。

请注意,这对于 error_log 不起作用 - 您需要在每个服务器块中放入一个条目。

© www.soinside.com 2019 - 2024. All rights reserved.