求助: nginx 无法获取内置变量的值

2020-03-07 20:29:16 +08:00
 yaocf

user root; worker_processes auto; pid /run/nginx.pid; include /etc/nginx/modules-enabled/*.conf; error_log /var/log/nginx/error.log;

events { worker_connections 1024; multi_accept on; } http { tcp_nodelay on; log_format main '$remote_addr - $remote_user [$time_local] $status ' '"$request" $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" "$host$request_uri"'; access_log /var/log/nginx/access.log main; server { listen 80; location ~/.well-known* { root /usr/lib/mylibs/acme_tiny; #alias /usr/lib/mylibs/acme_tiny/.well-known/; autoindex on; } location ~/* { rewrite ^(.*) https://$host$request_uri break; #return 301 https://$host$request_uri; } } }

环境:Ubuntu 18.04 (谷歌云主机,minimize 版的系统,没有图形界面) 直接用 apt install nginx 安装的。

以上是配置文件。 需求:.well-known 开头的路径依旧使用 http,其余的全部跳转到 https (如果请求包含路径的话,包括路径也得带到 https 链接中)。另外,不想用代理的方式进行转发。只想用重定向完成。 https 服务是另外的一个软件提供的(可以正常访问,并且证书有效)。

目前的问题$host、$request_uri 之类的,都获取不到包括日志,所有的变量都是空的(不加自定义日志格式的话,是正常有日志的)。

各种论坛之类的都搜过了,未找到答案。先谢谢各位了。

1988 次点击
所在节点    NGINX
4 条回复
yaocf
2020-03-07 20:29:58 +08:00
user root;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
error_log /var/log/nginx/error.log;

events {
worker_connections 1024;
multi_accept on;
}
http {
tcp_nodelay on;
log_format main '$remote_addr - $remote_user [$time_local] $status '
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$host$request_uri"';
access_log /var/log/nginx/access.log main;
server {
listen 80;
location ~/\.well-known* {
root /usr/lib/mylibs/acme_tiny;
#alias /usr/lib/mylibs/acme_tiny/.well-known/;
autoindex on;
}
location ~/* {
rewrite ^(.*) https://$host$request_uri break;
#return 301 https://$host$request_uri;
}
}
}
crystom
2020-03-08 22:02:11 +08:00
看下是否支持 debug 模式,开 debug 看下日志,另外默认的是 combined,可以在基础上修改
yaocf
2020-03-20 21:45:09 +08:00
@crystom 恩,谢谢,好不容易编译出了带调试模式的 nginx,看了下调试日志,问题找到了。配置文件中所有的 $ 换成 \$ 就行了。不知道为什么要转义一下才能用。按理说 nginx 的配置文件读取模块应该会考虑到这个吧。

新的配置文件如下,也希望可以帮到有同样问题的小伙伴。

user root;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
error_log /var/log/nginx/error.log;

events {
worker_connections 1024;
multi_accept on;
}
http {
tcp_nodelay on;
log_format main '\$remote_addr - \$remote_user [\$time_local] \$status '
'"\$request" \$body_bytes_sent "\$http_referer" '
'"\$http_user_agent" "\$http_x_forwarded_for" "\$host\$request_uri"';
access_log /var/log/nginx/access.log main;
server {
listen 80;
location ~/\.well-known* {
#root /usr/lib/mylibs/acme_tiny;
alias /usr/lib/mylibs/acme_tiny/.well-known/;
autoindex on;
}
location ~/* {
rewrite ^(.*) https://\$host\$request_uri break;
}
}
}
yaocf
2020-03-21 06:46:06 +08:00
@yaocf
可能是使用 cat <<EOF EOF 写入配置文件的原因,纠正一下,不需要加\。原先的配置文件应该是正确的。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/650769

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX