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

马修。

回答:

要访问类似/var/www/mysite/manage/publicURI之类的路径/manage,您将需要使用alias而不是root。有关详细信息,请参见此文档。

我假设您需要从两个根目录运行PHP,在这种情况下,您将需要两个location ~ \.php块,请参见下面的示例。如果其中没有PHP

/var/www/mysite/static,则可以删除未使用的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;

}

}

}

^~修饰符使前缀的位置优先于在同级别的正则表达式的位置。有关详细信息,请参见此文档。

由于这个长期存在的错误,aliasand

try_files指令不能一起使用。

在使用指令时要注意这一点if

以上是 Nginx位置配置(子文件夹) 的全部内容, 来源链接: utcz.com/qa/432103.html

回到顶部