我正在尝试实现以下目标:
我的域名的根目录http://www.paulix.local/
应该为位于的文件提供服务/var/www/html/main/webroot/index.php
该 URLhttp://www.paulix.local/malibu
应提供位于以下位置的文件/var/www/domain/malibu/webroot/index.php
我尝试实现的重写 (url => script):
- / => /var/www/html/main/webroot/index.php
- /path/whatever => /var/www/html/main/webroot/index.php?/path/whatever
- /malibu => /var/www/domain/malibu/webroot/index.php
- /malibu/path/whatever => /var/www/domain/malibu/webroot/index.php?/path/whatever
这是我当前的配置
server {
listen 80;
listen [::]:80;
rewrite_log on;
access_log /var/log/nginx/paulix-access.log;
error_log /var/log/nginx/paulix-errors.log notice;
server_name www.paulix.local;
root /var/www/html/main/webroot;
index index.php;
# location 1
location / {
try_files $uri $uri/ /index.php?$args;
}
# location 2
location ~ /malibu {
root /var/www/domain/malibu/webroot;
try_files $uri $uri/ /index.php?$args;
# location 3
location ~ \.php$ {
return 200 "Malibu is $document_root$fastcgi_script_name";
}
}
# location 4
location ~ \.php$ {
return 200 "Global $document_root$fastcgi_script_name";
}
}
如果我点击该 url http://www.paulix.local
,它会到达位置 4 并且我会得到以下输出:Global /var/www/html/main/webroot/index.php
这正是我所期望的。
问题
如果我点击该 URL,http://www.paulix.local/malibu
它也会点击位置 4,输出与上面相同,但我预计它会点击位置 3,预期输出为:Malibu is /var/www/domain/malibu/webroot/index.php
最后,点击http://www.paulix.local/malibu/index.php
命中位置 3,但没有得到预期的输出。它输出了Malibu is /var/www/domain/malibu/webroot/malibu/index.php
我想要的输出/var/www/domain/malibu/webroot/index.php
答案1
答案2
编辑:问题是由于Nginx 中的错误
一个解决方案是下面的一个,甚至更好这个
server {
listen 80 default_server;
listen [::]:80 default_server;
rewrite_log on;
access_log /var/log/nginx/paulix-access.log;
error_log /var/log/nginx/paulix-errors.log notice;
server_name www.paulix.test;
root /var/www/html;
location / {
root /var/www/html/main;
if (-f $request_filename) {
break;
}
# prevent recursion
if ($request_uri ~ /webroot/index.php) {
break;
}
#rewrite ^/$ / permanent; # do not uncomment, or face infinite rewrite
rewrite ^/webroot/(.*) /webroot/index.php last;
rewrite ^/(.*)$ /webroot/$1 last;
location ~ index\.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
location /malibu {
root /var/www/domain;
if (-f $request_filename) {
break;
}
# prevent recursion
if ($request_uri ~ /webroot/index.php) {
break;
}
rewrite ^/malibu$ /malibu/ permanent;
rewrite ^/malibu/webroot/(.*) /malibu/webroot/index.php last;
rewrite ^/malibu/(.*)$ /malibu/webroot/$1 last;
location ~ index\.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}