How do you mix variables and strings together in a Nginx configuration file?

I have a configuration file that looks like this:

server {
        listen 80;
        server_name example.org;
        root /var/www/comet;
        index index.htm;
        default_type text/plain;
        location /publish {
                push_stream_publisher admin;
                set $push_stream_channel_id             $arg_id;
        location /sub {
                push_stream_subscriber long-polling;
                set $callback "${arg_callback}({\"id\":~id~,\"data\":~text~});";
                push_stream_message_template   $callback;
                set $push_stream_channels_path "/channel1";

It's a push server and it's supposed to push a message to the client depending on what they pass to on the callback parameter.

I.e. If the user requests http://example.org/sub?callback=call&id=blah and a message comes to them, it should read call({"id":0, "data":"blah"});

The above code outputs the literal variable name

tangrs@~ $ curl "http://example.org/sub?id=woo&callback=call" -D - && echo
HTTP/1.1 200 OK
Server: nginx/1.0.11
Date: Thu, 12 Jan 2012 04:55:38 GMT
Content-Type: text/plain
Last-Modified: Thu, 12 Jan 2012 04:55:38 GMT
Connection: close
Transfer-Encoding: chunked
Etag: 0


Does anyone know how to concat strings in Nginx config files?

  • 1
    Sounds like a good recipe for cross-site scripting. Anyway, treating a web server configuration file as if it were a programming language doesn't tend to end in a happy result; I'd recommend using a real programming language generating dynamic content instead. – Shane Madden Jan 12 '12 at 5:09
  • It's a locally hosted project until I can get it working. I'm focused on getting it working before worrying about security. Thanks anyway though. – tangrs Jan 12 '12 at 5:17
  • 1
    Ok, fair enough - but again, with very few exceptions, configuration files are not programming languages. – Shane Madden Jan 12 '12 at 5:26
  • 2
    A word of advice from a recovering programmer: Worry about security NOW. Fixing a security disaster often requires a total rewrite. Doing it right from the beginning is always less effort. (Also, what @ShaneMadden said: Config files aren't programming languages. They should by and large be static entities, perhaps auto-generated, and dynamic aspects should be handled with Real Code™.) – voretaq7 Jan 12 '12 at 5:29

Store the string in another variable (say, $string) and do this:

set $callback $arg_callback$string;

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.