V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
yaocf
V2EX  ›  NGINX

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

  •  
  •   yaocf · 2020-03-07 20:29:16 +08:00 · 1977 次点击
    这是一个创建于 1504 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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 之类的,都获取不到包括日志,所有的变量都是空的(不加自定义日志格式的话,是正常有日志的)。

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

    4 条回复    2020-03-21 06:46:06 +08:00
    yaocf
        1
    yaocf  
    OP
       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
        2
    crystom  
       2020-03-08 22:02:11 +08:00
    看下是否支持 debug 模式,开 debug 看下日志,另外默认的是 combined,可以在基础上修改
    yaocf
        3
    yaocf  
    OP
       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
        4
    yaocf  
    OP
       2020-03-21 06:46:06 +08:00
    @yaocf
    可能是使用 cat <<EOF EOF 写入配置文件的原因,纠正一下,不需要加\。原先的配置文件应该是正确的。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   860 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 21:45 · PVG 05:45 · LAX 14:45 · JFK 17:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.