我正在尝试将Nginx路由到特定路径的别名中的index.PHP.使用子域(并添加具有不同名称的服务器块)会更容易,但我的项目需要这种结构有几个非主题原因.简单地说,我想要以下内容:
1. www.example.com --> /home/user/www/public/index.PHP
2. www.example.com/a/b --> /home/user/www/public/index.PHP
3. www.example.com/api --> /home/user/api/public/index.PHP
4. www.example.com/api/a/b --> /home/user/api/public/index.PHP
数字1和2非常标准:始终提供index.PHP文件(在某个根目录中)并让PHP处理路由. 3号模拟子域构造(并应指向不同目录中的index.PHP).
我已经设法让数字1-3工作,但不是数字4.每当我去www.example.com/api/a/b它给出404.错误日志告诉我Nginx试图为我服务/家庭/用户/ API /公共// A / b /.我觉得我非常接近解决方案,但我似乎没有做对.我也可以以相同的方式提供静态文件.
以下配置是相关的:
server {
listen 80;
server_name example.com www.example.com;
root /home/user/www/public;
index index.PHP index.html;
location / {
try_files $uri $uri/ /index.PHP;
}
location ~ \.PHP${
try_files $uri /index.PHP =404;
fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
fastcgi_index index.PHP;
fastcgi_pass unix:/var/run/PHP5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location /api {
alias /home/user/api/public/;
location ~ \.PHP${
try_files $uri /index.PHP =404;
fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
fastcgi_index index.PHP;
fastcgi_pass unix:/var/run/PHP5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
}
提前致谢.
解
server {
listen 80;
server_name example.com www.example.com;
root /home/user/www/public;
index index.PHP index.html;
location / {
try_files $uri $uri/ /index.PHP;
}
location ~ \.PHP${
try_files $uri /index.PHP =404;
fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
fastcgi_index index.PHP;
fastcgi_pass unix:/var/run/PHP5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location /api/ {
alias /home/user/api/public/;
try_files $uri /api/index.PHP =404;
location ~ \.PHP${
try_files $uri /index.PHP =404;
fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
fastcgi_index index.PHP;
fastcgi_pass unix:/var/run/PHP5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
}