Nginx root 没有按我预期的那样工作

Nginx root 没有按我预期的那样工作

我正在尝试实现以下目标:

我的域名的根目录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

根据您的需要,使用location /malibu {location ^~ /malibu {代替location ~ /malibu {

这里您可以阅读一个易于理解的概述,了解 Nginx 如何处理位置块。

编辑:抱歉,我写答案时没有看到你上次的编辑。我认为这就是alias你正在寻找的。只需将root位置 2 替换为alias,输出就没问题了。(来源

答案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;
}

}

相关内容