【问题标题】:Nginx error -Starting nginx: nginx: emerg unknown "status" variableNginx 错误 - 启动 nginx:nginx:emerg 未知“状态”变量
【发布时间】:2016-06-28 08:43:49
【问题描述】:

我正在尝试使用 nginx 做的是调用后端进行身份验证,如果响应成功,我将重定向到网站 1(例如 -google.com),如果身份验证失败,我将重定向到网站 2(facebook例如)。

下面是我的 nginx.conf-

user              nginx;                                                        
 worker_processes  1;                                                            

 error_log  /var/log/nginx/error.log;                                            
 pid        /var/run/nginx.pid;                                                  

 events {                                                                        
    worker_connections  1024;                                                   
 }                                                                               

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

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '   
                  '[===>$status] $body_bytes_sent "$http_referer" '         
                  '"$http_user_agent" "$http_x_forwarded_for"';             

     access_log  /var/log/nginx/access.log  main;                                

     sendfile        on;                                                         
     keepalive_timeout  65;                                                      

    # Load config files from the /etc/nginx/conf.d directory                    
    # The default server is in conf.d/default.conf                              
   include /etc/nginx/conf.d/default.conf;                                      
}  

default.conf 文件如下 -

server {
    listen       80 default_server;
    server_name  _;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        # root   /usr/share/nginx/html;
        # index  index.html index.htm;

        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://backend_ip_Address;


        set $my_var 0;
        if ($status = "200"){
            set $my_var 1;
        }

        #if($status = 4xx) {
         #  set $my_var  2;
        #}

        if ($my_var = 1){
             proxy_pass http://www.google.com;
        }

        if ($my_var = 2) {
            proxy_pass http://www.facebook.com;
        }
    }

    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    } 
}

我面临的问题是当我尝试使用此配置执行 sudo service nginx restart 时出现以下错误- 启动 nginx: nginx: [emerg] 未知“状态”变量

相同的 $status 也存在于 nginx.conf 日志配置中,它正确地记录响应代码,如 301、200 等。但相同的状态变量在 default.conf 文件中不起作用。对我做错了什么有帮助吗?

我尝试用 body_bytes_sent 标头替换状态,它的工作原理。

通过谷歌搜索https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=nginx++unknown+%22status%22+variable只有相关信息是https://www.drupal.org/node/2738983,但对解决这个问题没有多大帮助。

【问题讨论】:

    标签: nginx


    【解决方案1】:

    状态变量是在很晚的阶段定义的,在请求被处理并准备好发送回之后。

    您不能将其用于条件路由。

    通常用于记录。

    您可以在这里阅读有关 nginx 指令执行顺序和阶段的信息: https://openresty.org/download/agentzh-nginx-tutorials-en.html

    【讨论】:

    • 感谢亚历山大的回复。我的用例如下 - 我将使用用户凭据从 nginx 调用身份验证模块(我上面的帖子中的 proxy_pass backend_ip_Address 事物)。如果身份验证成功(200),那么我将调用另一个服务来获取我的数据。如果它是未经授权的 (301) 我需要将此信息发送回给调用者。关于如何实现这一点的任何建议。
    • TLDR:您应该为 auth 配置单独的位置并为受保护的位置添加 auth_request 指令
    • 谢谢亚历山大。它就像一个冠军,正是我所需要的。谢谢你指出这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-24
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多