看不出两个nginx配置之间的区别

时间:2015-04-27 13:35:07

标签: nginx cors

人。我需要你的帮助。 第一个配置文件正常运行:

set $cors "";
if ($http_origin ~* "^https?://auto.*") {
    set $cors "true";
}
if ($http_origin ~* "^https?://dom.*") {
    set $cors "true";
}
location /push/pub/ {
    push_stream_publisher                   admin;
    push_stream_channels_path               $arg_id;
    include allow/servers.*.conf;
    deny all;
}
location ~ /push/sub/(.*) {
    push_stream_subscriber                  long-polling;
    push_stream_channels_path               $1;
    push_stream_last_received_message_tag   $arg_tag;
    push_stream_last_received_message_time  $arg_time;
    push_stream_longpolling_connection_ttl  30s;
}
#cors configuration
    if ($cors = "true") {
      add_header  'Access-Control-Allow-Origin' "$http_origin";
      add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
      add_header 'Access-Control-Allow-Headers' 'If-Modified-Since';
    }
}

我试着重构这段代码,就像那样

location /push/pub/ {
    push_stream_publisher                   admin;
    push_stream_channels_path               $arg_id;
    include allow/servers.*.conf;
    deny all;
}

location ~ /push/sub/(.*) {
    push_stream_subscriber                  long-polling;
    push_stream_channels_path               $1;
    push_stream_last_received_message_tag   $arg_tag;
    push_stream_last_received_message_time  $arg_time;
    push_stream_longpolling_connection_ttl  30s;

    if ($http_origin ~* "^https?://(auto|dom).*") {
        add_header 'Access-Control-Allow-Origin' "$http_origin";
        add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'If-Modified-Since';
    }
}

现在js订阅者永远倾听。服务器无响应:( 两个配置之间的根本区别是什么。我没有责备。 感谢您的建议。

1 个答案:

答案 0 :(得分:0)

不同之处在于,在第二种情况下,if指令创建了另一个配置上下文,其中add_headers但未配置push_stream

您应该了解重写模块的工作原理(这很棘手):http://nginx.org/en/docs/http/ngx_http_rewrite_module.html 并尽可能避免使用ifhttp://wiki.nginx.org/IfIsEvil