Nginx位置配置(子文件夹) [英] Nginx location configuration (subfolders)

查看:748
本文介绍了Nginx位置配置(子文件夹)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我说一条像这样的路径:

lets say I've a path like:

/var/www/myside/

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

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

我想将nginx配置为可以访问:

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

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

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

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

/manage folder 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 public subfolder (eg. /var/www/mysite/manage/public/index.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 manage doesn't. Am I doing something wrong? Does anybody know what should I change?

马修.

推荐答案

要使用像/manage这样的URI访问像/var/www/mysite/manage/public这样的路径,您将需要使用alias而不是root.有关详细信息,请参见本文档.

To access a path like /var/www/mysite/manage/public with a URI like /manage, you will need to use alias rather than root. See this document for details.

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

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

例如:

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 document for details.

由于这个长期存在的错误和try_files指令未一起使用.

The alias and try_files directives are not together due to this long standing bug.

在使用以下内容时请注意此警告 if指令.

Be aware of this caution in the use of the if directive.

这篇关于Nginx位置配置(子文件夹)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆