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

查看:64
本文介绍了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 文件夹位于 /manage(例如 http://example.org/manage) 在这种情况下,此文件夹包含 Slim 的 PHP 框架代码 - 这意味着 index.php 文件位于 public 子文件夹(例如/var/www/mysite/管理/公共/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)

我尝试了很多组合比如

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 没有.难道我做错了什么?有人知道我应该改变什么吗?

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

马修.

推荐答案

使用像 /manage/var/www/mysite/manage/public 这样的路径code>,您将需要使用 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.

aliastry_files 指令不在一起,因为 这个长期存在的错误.

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

请注意此注意事项在使用if 指令.

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

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