Need Jekyll Help in running multiple jekyll instances on different port and using nginx server

Dear Jekyll experts,
Need your help here. Can you please suggest or guide something.

On an ubuntu server, I am running multiple Jekyll instances.
Different versions of documents are checked out in different directory and each has an jekyll running.
top/dir1 — jekyll port 4000
top/dir2 – jekyll port 4001
top/dir3 — jekyll port 4003

These different jekyll instances are mapped on nginx
location /stg/ {
proxy_pass http:/localhost:4001/;
}

location / {
proxy_pass http://localhost:4000/;
}

Problem I am facing is, when I use stg environment by putting www.example.com/stg
top pages looks fine. But links under those pages goes to
www.example.com/ instead of www.example.com/stg.

e.g. link on page1 on stg
www.example.com/stg/page1

becomes
www.example.com/link1 instead of www.example.com/stg/link1

Any idea what can I configure in jekyll to get it fixed?
That way Jekyll generate full path for the URL (including directory under example.com).

Any help or input will be highly appreciated?

Thanks
-Gaurav

If you are doing all that in ubuntu and nginx then you may already know all of the following but I will post it just in case:

kind of sounds the same as GH pages on a project - maybe you should use baseurl and prepend that to your urls?

If the website is in a sub folder rather than the root, and you have a url like /contact/ the first / I believe is saying go all the way to the root and then look for /contact. but it sounds like it is really in a sub folder called stg and then it is /contact, This is where people have trouble with GH - their project name is really a sub directory to their username.

You may have some trouble with this though if you are using jekyll serve on the server - as it may be disregarding baseurl since it thinks you are doing dev work locally - I think you can over ride this if you set the enfironment to something other than development.

This links to info about jekyll serve and what it is doing with baseurl:

It might be the case your Jekyll sites configuration is missing baseurl setting.

Or it might be your Jekyll sites markup just doesn’t account for that case…

Seeing source code might be very helpful to answer your question.