【问题标题】:Don't see difference between two nginx config看不到两个 nginx 配置之间的区别
【发布时间】:2020-01-12 20:12:42
【问题描述】:

伙计们。我需要你的帮助。 第一个配置文件运行成功:

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 订阅者永远聆听。服务器没有响应:( 两个配置之间的根本区别是什么。我不明白。 谢谢建议。

【问题讨论】:

    标签: nginx


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2013-10-30
      • 2011-11-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-18
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      相关资源
      最近更新 更多