社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  NGINX

nginx子目录中的独立应用程序

Džuris • 7 年前 • 1683 次点击  

我正在尝试将一个应用程序从apache服务器移动到nginx。问题是子目录中有多个应用程序,我找不到配置服务器的正确方法。

我需要什么:

  • www.example.com 从服务 /srv/app
  • www.example.com/sub1 从服务 /srv/app/sub1
  • www.example.com/sub2 从服务 /srv/app/sub2

每个应用程序都需要相同的配置,因此我在一个片段中提取了该配置:

# snippets/app.conf

index index.php index.html index.htm index.nginx-debian.html;

location /system {
    return 403;
}

# [a couple of other 403s excluded]

# Pass non-file URI to index.php
location / {
    try_files $uri $uri/ /index.php?$query_string;
}

# Use PHP
location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}

# Hide .htaccess
location ~ /\.ht {
    deny all;
}

在主服务器文件中:

# [non-www and http redirects]

server {
    # [listen directives]

    server_name www.example.com;

    root /srv/app;

    include snippets/app.conf;

    location /sub1 {
        root /srv/app/sub1;
        include snippets/app.conf;
    }

    # [other sub-apps included in the same way]

    # [ssl stuff]
}

但是,这给了我一个错误:

nginx:[emerg]location“/system”在外部location“/sub1”in/etc/nginx/snippets/app.conf:5

从错误中可以明显看出 /system 被解释为“绝对的” www.example.com/system 而不是嵌套的 www.example.com/sub1/system . 我是否可以指定要将嵌套位置视为相对位置?或者我只需要为每个改变前缀的子应用重复几乎相同的配置?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/46010
文章 [ 1 ]  |  最新文章 7 年前
Džuris
Reply   •   1 楼
Džuris    7 年前

结果发现,在nginx中,大部分重复都是不必要的。

使用fastcgi的指令 .php 藏起来 /.ht 文件已经是regex了,所以它们会影响到所有内容。足以说明 index 有一次,如果我只想用的话,默认的东西是多余的 index.php .

由于所有应用程序都以与web相同的方式嵌套在文件系统中,因此也不需要指定根。

令我惊讶的是 location ^~ /(system|data)/ { ... } 不仅匹配 www.example.com/system/ ,而且 www.example.com/sub1/system/ . 我以为 ^~ 只有当开始位置与正则表达式匹配时才应该匹配…

# [non-www and http redirects]

server {
    # [listen directives]

    server_name www.example.com;

    root /srv/app;

    index index.php;

    location ^~ /(system|data)/ {
        return 403;
    }

    # Use PHP
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    }

    # Pass non-file URI to index.php for all locations
    location /sub1/ {
        try_files $uri $uri/ /sub1/index.php?$query_string;
    }

    location /sub2/ {
        try_files $uri $uri/ /sub2/index.php?$query_string;
    }

    # [other sub-apps included in the same way]

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ /\.ht {
        deny all;
    }

    # [ssl stuff]
}

我也试着用

location ^~ /(sub1|sub2)/ {
    try_files $uri $uri/ /$1/index.php?$query_string;
}`

但没有成功-这个位置不知怎么不匹配,所有的东西都传给了 /index.php 而是在基地。