Score:0

Configure nginx to serve query string parameter

jp flag

We have one NextJs site, with next export we get one out folder which we want to serve from Nginx. In out/login there is one '[[...parameter]].html'. When we deploy site using pm2 it is working if we hit url "serverIP:port/login/programID/applicant"

But when serving through nginx it is not working.

Below is the nginx configuration file content:

server{

    listen 7001;
    server_name server_ip;
    root /var/www/html/out/;
   location / {

            try_files $uri $uri.html  $uri.html/ =404;
   }

}

Please help. I am new to nginx so having some difficulties.

ws flag
The config you have shown us is for a very simple origin server. If you want to apply some mapping between the path in the URL and the path on the filesystem, you're going to need some rules in nginx to define what that mapping should be. If you can describe the problem accurately, providing explicit examples of the request URLs and the corresponding filesystem locations you might get some sensible answers here.
Score:0
ru flag

Query string refers to GET parameters which you provide after URL e.g ?name=foo. My guess is that you want to serve files in a directory instead, since you mention "out folder which we want to serve from Nginx".


In this case your example should work fine, but your try_files rule expects that you also provide the file name inside the URL.

So when you are requesting GET www.mysite.com then Nginx tries to find a path inside the root directory with either name /, .html, .html/ or fail with 404 error code. Path .html/ refers to a directory named .html inside your root and is probably a mistake.

In case you have a file named index.html in your specified root directory then following requests would match for it:

  • GET www.mysite.com/index.html (because of $uri)
  • GET www.mysite.com/index (because of $uri.html)

What you probably are looking for is this:

location / {
   root /var/www/html/out;
   index index.html index.htm;
   try_files $uri $uri/ =404;
}

This does following:

  • Is there an actual file, specified by $uri, in your root directory - then serve this.
  • Is there a directory path, specified by $uri, in your root directory?
    • If yes, check if it contains a file named index.html or index.htm - then serve this.
  • None of the above, return 404.

So GET www.mysite.com would try to find a file index.html or index.htm inside your root.


In case you still mean query strings you could write something like this:

location / {
    root /var/www/html/out;
    try_files /$arg_file =404;
}

Then request like GET www.mysite.com?file=index.html would search for a file index.html inside your root. Nginx is awesome, isn't it?


Hope this helps :)

Also read more about try_files and Nginx Pitfalls and Common Mistakes.

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.