【问题标题】:Nginx location configuration (subfolders)Nginx 位置配置(子文件夹)
【发布时间】:2017-07-15 13:41:35
【问题描述】:

假设我的路径如下:

/var/www/myside/

该路径包含两个文件夹...假设 /static/manage

我想配置 nginx 以访问:

/static/ 上的文件夹(例如,http://example.org/) 这个文件夹有一些 .html 文件。

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

我试过很多组合比如

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

/ 无论如何都可以正常工作,manage 不能。难道我做错了什么?有人知道我应该改变什么吗?

马修。

【问题讨论】:

  • 我认为root $uri/(directory); 行不通,不是吗?这肯定会有些奇怪,比如root /manage/manage/public,而不是你想要的。还是我误解了$uri 的作用?
  • @Bytewave 是的,你说得对,它不会起作用。这是我已经尝试过的百万种组合之一:) 让我们假设没有 $uri 但 /var/www/mysite/manage/public/var/www/mysite/static - 无论如何它不起作用。

标签: php http nginx server slim


【解决方案1】:

要使用类似/manage 的URI 访问类似/var/www/mysite/manage/public 的路径,您需要使用alias 而不是root。详情请见this document

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

例如:

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

^~ 修饰符使前缀位置优先于同一级别的正则表达式位置。详情请见this document

由于this long standing bugaliastry_files 指令没有放在一起。

在使用if 指令时注意this caution

【讨论】:

猜你喜欢
  • 2020-07-27
  • 2017-05-30
  • 1970-01-01
  • 1970-01-01
  • 2015-03-03
  • 2014-04-12
  • 1970-01-01
相关资源
最近更新 更多