dougou1127 2017-02-24 16:21
浏览 72
已采纳

Nginx位置配置(子文件夹)

lets say I've a path like:

/var/www/myside/

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

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

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

/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?

Matthew.

  • 写回答

1条回答 默认 最新

  • douqihua6212 2017-02-26 10:52
    关注

    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.

    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.

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

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?