nginx - 正则表达式捕获多个位置

nginx - 正则表达式捕获多个位置

为多个位置创建正则表达式捕获。

树结构:

/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

该 nginx 配置有效:

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:/;
    }

尝试将这些位置合并到一个 PCRE 正则表达式中

    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:/;
    }

我得到的是:

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

我的期望是:

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

我读到用户目录地点顺丰快递但仍然不知道该怎么做。

我应该添加或更改什么,应该查看哪里?(我几乎已经没有主意了)

答案1

匹配是针对规范化的 URI 而不是根路径进行的。

所以这

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

应该

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

现在它正在工作。

相关内容