php Nginx 位置配置(子文件夹)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42443468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:36:01  来源:igfitidea点击:

Nginx location configuration (subfolders)

phphttpnginxserverslim

提问by Nubzor

lets say I've a path like:

假设我有一条类似的路径:

/var/www/myside/

that path contains two folders... let's say /staticand /manage

该路径包含两个文件夹......让我们说 /static/manage

I'd like to configure nginx to have an access to:

我想配置 nginx 以访问:

/staticfolder on /(eg. http://example.org/) this folder has some .html files.

/static文件夹/(例如http://example.org/)此文件夹有一些 .html 文件。

/managefolder on /manage(eg. http://example.org/manage) in this case this folder contains Slim's PHP framework code - that means the index.php file is in publicsubfolder (eg. /var/www/mysite/manage/public/index.php)

/manage文件夹/manage(例如http://example.org/manage)在这种情况下,此文件夹包含 Slim 的 PHP 框架代码 - 这意味着 index.php 文件位于public子文件夹中(例如 /var/www/mysite/manage/public/索引.php)

I've tried a lot of combinations such as

我尝试了很多组合,例如

server {
listen 80;
server_name  example.org;
error_log /usr/local/etc/nginx/logs/mysite/error.log;
access_log /usr/local/etc/nginx/logs/mysite/access.log;
root /var/www/mysite;

location /manage {
  root $uri/manage/public;

  try_files $uri /index.php$is_args$args;
}

location / {
  root $uri/static/;

  index index.html;
}

location ~ \.php {
  try_files $uri =404;
  fastcgi_split_path_info ^(.+\.php)(/.+)$;
  include fastcgi_params;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  fastcgi_param SCRIPT_NAME $fastcgi_script_name;
  fastcgi_index index.php;
  fastcgi_pass 127.0.0.1:9000;
}

}

}

The /works correctly anyway managedoesn't. Am I doing something wrong? Does anybody know what should I change?

/工作正常反正manage没有。难道我做错了什么?有人知道我应该改变什么吗?

Matthew.

马修。

采纳答案by Richard Smith

To access a path like /var/www/mysite/manage/publicwith a URI like /manage, you will need to use aliasrather than root. See this documentfor details.

要访问类似/var/www/mysite/manage/publicURI的路径/manage,您需要使用alias而不是root。有关详细信息,请参阅此文档

I am assuming that you need to run PHP from both roots, in which case you will need two location ~ \.phpblocks, see example below. If you have no PHP within /var/www/mysite/static, you can delete the unused locationblock.

我假设您需要从两个根运行 PHP,在这种情况下,您将需要两个location ~ \.php块,请参见下面的示例。如果里面没有 PHP /var/www/mysite/static,可以删除未使用的location块。

For example:

例如:

server {
    listen 80;
    server_name  example.org;
    error_log /usr/local/etc/nginx/logs/mysite/error.log;
    access_log /usr/local/etc/nginx/logs/mysite/access.log;

    root /var/www/mysite/static;
    index index.html;

    location / {
    }
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass 127.0.0.1:9000;

        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    }

    location ^~ /manage {
        alias /var/www/mysite/manage/public;
        index index.php;

        if (!-e $request_filename) { rewrite ^ /manage/index.php last; }

        location ~ \.php$ {
            if (!-f $request_filename) { return 404; }
            fastcgi_pass 127.0.0.1:9000;

            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        }
    }
}

The ^~modifier causes the prefix location to take precedence over regular expression locations at the same level. See this documentfor details.

^~修饰符使前缀的位置优先于在同级别的正则表达式的位置。有关详细信息,请参阅此文档

The aliasand try_filesdirectives are not together due to this long standing bug.

由于这个长期存在的错误aliastry_files指令没有在一起。

Be aware of this cautionin the use of the ifdirective.

在使用该指令时请注意这一警告if