0

Creating regex captures for multiple locations.

Tree structure:

/home/user/webapps/
├── index.html           <= content: root.
├── osqa
│   ├── index.html       <= content: osqa test A.
│   ├── osqa             <= django project
│   │   ├── index.html   <= content: osqa test B.
│   │   └── osqa
│   │       ├── settings.py
│   │       └── wsgi.py
│   ├── run
│   └── static
└── forum
    ├── index.html       <= content: forum test A.
    ├── forum            <= django project
    │   ├── index.html   <= content: forum test B.
    │   └── forum
    │       ├── settings.py
    │       └── wsgi.py
    ├── run
    └── static

This nginx config is working:

server {
    listen 8080;
    server_name localhost;
    root /home/user/webapps/;
    location /osqa/ {
        alias /home/user/webapps/osqa/;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass h ttp://unix:/home/user/webapps/osqa/run/gunicorn.sock:/;
    }
    location /forum/ {
        alias /home/user/webapps/forum/;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass h ttp://unix:/home/user/webapps/forum/run/gunicorn.sock:/;
    }

trying to join those locations into one PCRE regex

    location ~ webapps\/(?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 h ttp://unix:/home/user/webapps/$app/run/gunicorn.sock:/;
    }

what i get:

localhost:8080/osqa/osqa/
returns: osqa test B
localhost:8080/osqa/
returns: 403 Forbidden
localhost:8080
returns: osqa test A

what i expected:

localhost:8080/osqa/osqa/
returns: osqa test B
localhost:8080/osqa/
returns: django site
localhost:8080
returns: root

I read about user dir, location, SF post and still can't figure out how to do it.

What should i add or change, where should i look ? (I'm almost out of ideas)

Abc Xyz
  • 608
  • 1
  • 8
  • 17
  • Where does the `~ webapps\/` part come into your combined location? There is no URI in the source `location` blocks that contains the word `webapps`. – Tero Kilkanen Apr 13 '15 at 10:26

1 Answers1

2

The matching is performed against a normalized URI not a root path.

So this

location ~ webapps\/(?P<app>[\w-_]+) {

Should be

location ~ ^\/(?P<app>[\w-_]+) {

and its working now.

Abc Xyz
  • 608
  • 1
  • 8
  • 17