我在 nginx 中有以下默认服务器设置:
# Default HTTP Server
server {
listen 80 default;
server_name _;
access_log /var/log/nginx/$server_name.access.log;
error_log /var/log/nginx/$server_name.error.log;
server_name_in_redirect off;
location / {
root domain.com/public;
index index.php;
try_files $uri index.php;
}
location ~ \.(html|jpg|jpeg|gif|png|ico|css2|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ {
root /path/to/domain.com/public;
expires 30d;
break;
}
charset utf-8;
location ~ \.php$ {
include /opt/nginx/conf/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /path/to/domain.com/public/index.php;
}
location ~ \.(js|ico|gif|jpg|png|css)$ {
root /path/to/domain.com/public;
}
}
我有几个域指向该服务器。我在这里试图实现的是让日志采用以下格式之一/var/log/nginx/mydomain.com/access.log
或/var/log/nginx/mydomain.com.access.log
相反,我得到的是/var/log/nginx/$server_name.access.log
。
如果我尝试目录方法,检查配置时会出错nginx: [emerg] open() "/var/log/nginx/$server_name/access.log" failed (2: No such file or directory)
为什么 nginx 没有将变量传递给文件名?
使用 nginx/1.0.0
答案1
您需要使用$host
变量 – 仅允许用于access_log
指令。