1

I am a newbie in the world of Nginx. While learning it, I am trying to accomplish this: I want to return a string whenever someone hits this URL: /greet/user and the string will be 'Hello user.' So, if someone hits localhost/greet/john, the page will show this string: 'Hello john'; if the URL is localhost/greet/merry, the string will be 'Hello merry.'

I tried doing that with the following code fragment. To note, I did use a regular expression to catch any argument that came second.

location ~/greet/(user+) {
    set $user user;
    return 200 'Hello ${user}';
}

What am I missing here? And how should it be done? My OS is Ubuntu 22.04. This is my first post on server fault, and I am new to the world of web servers. So, if I miss out on anything while writing this post, I hope I will not receive any penalty.

lone wolf
  • 11
  • 3

2 Answers2

0

I have figured it out. What I did wrong was in the regular expression. It was capturing any number of 'r's followed by use.

So the correct code was the following:

location ~ /greet/(\w+) {
    set $user $1;
    return 200 'Hello ${user}';
}

I set the first word to a variable named user and returned it with a string.

lone wolf
  • 11
  • 3
0

You could also use sub_filter to replace HTML or send your response in a header.

Steven
  • 135
  • 1
  • 8