我尝试使用nginx做的是-调用后端进行身份验证,如果响应成功,我将重定向到网站1(例如-google.com),如果身份验证失败,我将重定向到website2(例如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日志配置中也存在相同的$status,并且它正确地记录了响应代码,如301、200等。但是相同的状态变量在default.conf文件中不起作用。对我做错了什么有什么帮助吗?
我试着用body_bytes_sent头替换status,它起作用了。
通过谷歌搜索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,但没有太多的帮助来解决这个问题。
发布于 2016-06-30 20:11:11
状态变量定义在非常晚的阶段,在请求被处理并且响应准备发送回来之后。
您不能将其用于条件路由。
通常,它用于日志记录。
你可以在这里读到nginx指令的执行顺序和阶段:https://openresty.org/download/agentzh-nginx-tutorials-en.html
https://stackoverflow.com/questions/38071255
复制相似问题