Score:0

Nginx : Get last value of URL

vc flag

I am posting here because I have not found an answer to the following problem.

I have the following url: https://www.example.com/archives/5852555.pdf

and I need to keep only 5852555.pdf to pass this value to http://localhost:3001/download.php?file=5852555 .

For now, I have the following configuration:

location ~* ^/archives/([0-9]+)\.(txt|rtf|doc|docx|pdf)$ {
      proxy_pass http://localhost:3001/download.php?file=$1 ; 
}

But $1 is still empty, and I don't understand why.

Can you help me?

P.S. I'm relatively new to nginx, thanks for your understanding.

I'm using nginx 1.23.2 .

Richard Smith avatar
jp flag
Other than the missing `;` after the `proxy_pass` statement, the configuration fragment in your question works for me. Use `nginx -T` (uppercase `T`) to test and view the entire configuration across all included files.
outstore avatar
vc flag
Thank you for your reply. Indeed, it was a typo for the semicolon in my post but not in my server configuration file. I corrected it on the post and also added the same regular expression from my server. Now, after restarting my nginx, it does not restart. I have the following error: `nginx:[emerg] "proxy_pass" cannot have a URI part in the location given by the regular expression, or inside the named location, or inside the "if" statement, or inside the "limit_except" block` Can you help me?
Richard Smith avatar
jp flag
I do not get that error with the configuration you posted. It is behaving as if the `$1` is missing.
jabbson avatar
sb flag
Same here, works fine, no error. Probably you have another proxy_pass block?
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.