AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 682216
Accepted
Abc Xyz
Abc Xyz
Asked: 2015-04-13 16:02:43 +0800 CST2015-04-13 16:02:43 +0800 CST 2015-04-13 16:02:43 +0800 CST

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

  • 772

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

树结构:

/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

我阅读了有关用户 dir、location、SF post 的信息,但仍然不知道该怎么做。

我应该添加或更改什么,我应该在哪里查看?(我几乎没有想法)

nginx
  • 1 1 个回答
  • 1292 Views

1 个回答

  • Voted
  1. Best Answer
    Abc Xyz
    2015-04-14T02:40:51+08:002015-04-14T02:40:51+08:00

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

    所以这

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

    应该

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

    及其现在的工作。

    • 2

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve