1

我想将变量 $host 放在 Nginx access_log 的文件路径中:

http {
   log_format compression '$remote_addr - $remote_user [$time_local] '
                       '"$request" $status $body_bytes_sent '
                       '"$http_referer" "$http_user_agent" 
   "$gzip_ratio"';

 server {
     gzip on;
     access_log /var/logs/$host.access.log compression;
 }
}

基于 Nginx 文档:http://nginx.org/en/docs/http/ngx_http_log_module.html, $host 是嵌入式变量,应该在日志路径中工作。但是,在我的情况下它不起作用。谁能在这里提供一些提示?我的 Nginx 版本是 1.10。谢谢

4

3 回答 3

1

可能不支持,就是测试结果

[root@l nginx.d]# nginx -s quit
[root@l nginx.d]# nginx 
[root@l nginx.d]# ls /var/log/nginx/
access.log  error.log  ${host}.log  $host.log  $server_name.log
[root@l nginx.d]# cat cms.0.dev.q.com.on 
server{

    server_name
        cms.0.dev.q.com 
        cms-0.dev.q.com 
        ;

    error_log /var/log/nginx/${host}.log warn;
    access_log /var/log/nginx/$server_name.log;
于 2021-01-12T06:48:12.960 回答
0

根据 nginx 文档,当在名称中使用变量时,缓冲写入不起作用,并且 gzip 压缩是缓冲写入。所以你可以有一个变量,或者gzip,但不幸的是不能同时使用。

于 2021-04-26T21:20:39.623 回答
-1

尝试先设置 server_name 参数。另请查看此工作示例:

server {
            if ($host ~* www\.(.*)) {
                    set $host_without_www $1;
                    rewrite ^(.*)$ http://$host_without_www$1/ permanent;
            }
            server_name_in_redirect off; #or folders like /awstats will redirect to _
            listen 80;
            server_name _;
            access_log      /var/log/nginx/$host.access_log main;
            error_log      /var/log/nginx/$host.access_log info;
            root /var/www/$host/htdocs;
            location ~ \.php$ {
                    include /etc/nginx/fastcgi_params;
                    fastcgi_pass  127.0.0.1:1026;
                    fastcgi_index index.php;
            }
    #For WP
            if (!-e $request_filename) {
                    rewrite ^(.+)$ /index.php?q=$1 last;
            }
    }
于 2018-11-15T06:05:20.137 回答