I want to inject "project name" to every link on a web page.

Example: link and site http://localhost/osqa/ is working, but when I'm on the osqa page, all links are directing to localhost directly http://localhost/account/signin/ - are broken ...

so i want to rewrite/redirect all links to point to project name location, like http://localhost/osqa/account/signin/

Is it possible, if yes then how to do it? Right now I'm learning about rewrite and try_files.

This is my project tree structure, project names are (osqa, forum, other_project, another_project):

├── osqa
│   ├── osqa
│   ├── media
│   └── static
├── forum
│   ├── forum
│   ├── media
│   └── static
├── other_project
│   ├── other_project
│   ├── media
│   └── static
└── another_project
    ├── another_project
    ├── media
    └── static

This is my nginx config:

server {
    listen 8080;
    server_name localhost;
    root /home/user/webapps/;

location ~ ^\/(?P<app>[\w-_]+)(/.*)?$ {
    alias /home/user/webapps/$app/;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unix:/home/user/webapps/$app/run/gunicorn.sock:/;

I was changing root path and rewriting uri but without success, yet.


The rewrite and try_files apply only to incoming requests and the way the incoming URLs are mapped to paths on the server.

There isn't a reliable way to rewrite the URLs that are in the actual HTML code that is sent to the clients.

You should study other methods to make your apps prepend the required path into the URLs. Most often applications have a setting for root URL, for example you would set that to http://localhost/forum for your forum app. After setting that, the forum automatically adds the required prefixes to the URLs.

  • I was doing to many things at the same time, thank you for pointing things out ... so it is not possible with nginx. – Abc Xyz Apr 14 '15 at 19:17
  • Well, it is possible with nginx, but it is very hard to make to work reliably and efficiently. – Tero Kilkanen Apr 15 '15 at 8:38

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.