2017-02-24 159 views
4

可以说,我已经像一个路径:Nginx的位置配置(子文件夹)

/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的/管理/公共/指数.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不不是。难道我做错了什么?有谁知道我应该改变什么?

马修。

+0

我不认为'根$ URI /(目录);'会工作,会吗?当然会出现一些奇怪的,比如'root/manage/manage/public',而不是你想要的。还是我误解了'$ uri'呢? – Bytewave

+0

@Bytewave好耶,你是对的,它不会工作。这是我已经尝试过:)让我们假设没有$的畅想组合一个URI但是'在/ var/WWW/mysite的/管理/ public'和'在/ var/WWW/mysite的/ static' - 反正它不没有工作。 – Nubzor

回答

1

要使用像/manage这样的URI访问像/var/www/mysite/manage/public这样的路径,您需要使用alias而不是root。详情请参阅this document

我假设你需要从两个根,在这种情况下,你将需要两个location ~ \.php块,见下面的例子运行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

aliastry_files指令由于this long standing bug而不在一起。

在使用if指令意识到this caution

+0

您的例子 – vladkras

+0

@vladkras数倍常见的错误和缺陷(https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/)如? –