Nginx 位置配置(子文件夹)
Nginx location configuration (subfolders)
假设我有这样的路径:
/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
却不能。难道我做错了什么?有人知道我应该更改什么吗?
马修
要使用 /manage
等 URI 访问 /var/www/mysite/manage/public
等路径,您需要使用 alias
而不是 root
。有关详细信息,请参阅 this document。
我假设您需要从两个根开始 运行 PHP,在这种情况下您将需要两个 location ~ \.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。
由于 this long standing bug,alias
和 try_files
指令不在一起。
在使用 if
指令时注意 this caution。
假设我有这样的路径:
/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
却不能。难道我做错了什么?有人知道我应该更改什么吗?
马修
要使用 /manage
等 URI 访问 /var/www/mysite/manage/public
等路径,您需要使用 alias
而不是 root
。有关详细信息,请参阅 this document。
我假设您需要从两个根开始 运行 PHP,在这种情况下您将需要两个 location ~ \.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。
由于 this long standing bug,alias
和 try_files
指令不在一起。
在使用 if
指令时注意 this caution。