在nginx中为相对URL使用别名时的禁止位置

Hol*_*olt 9 nginx relative-path phpldapadmin

我试图在相对网址上设置带有nginx的roundcube/phpldapadmin/...,例如:

example.com/roundcube
example.com/phpldapadmin
Run Code Online (Sandbox Code Playgroud)

首先,Apache 2.4一切正常.我有以下文件夹:

# roundcube
/var/www/roundcube
# phpldapadmin
/usr/share/phpldapadmin
Run Code Online (Sandbox Code Playgroud)

我有以下location为roundcube:

location /roundcube/ {
    root /var/www;
    index index.php;

    location ~ \.php$ {
        try_files $uri =404;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}
Run Code Online (Sandbox Code Playgroud)

哪个工作正常,但以下内容phpldapadmin不起作用:

location /phpldapadmin/ {
    alias  /usr/share/phpldapadmin/htdocs;
    index  index.php index.html index.htm;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include /etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}
Run Code Online (Sandbox Code Playgroud)

我得到一个403 forbidden,有以下日志:

2016/02/07 21:43:33 [error] 23047#0: *1 directory index of "/usr/share/phpldapadmin/htdocs" is 
forbidden, client: xxx.xxx.xxx.xxx, server: ****, request: "GET /phpldapadmin/ HTTP/1.1", 
host: "****"
Run Code Online (Sandbox Code Playgroud)

我检查了许可:

$ namei -om /usr/share/phpldapadmin/htdocs
f: /usr/share/phpldapadmin/htdocs
 drwxr-xr-x root root     /
 drwxr-xr-x root root     usr
 drwxr-xr-x root root     share
 drwxr-xr-x root root     phpldapadmin
 drwxr-xr-x root www-data htdocs
$ ls -l /usr/share/phpldapadmin/htdocs/index.php
-rw-r--r-- 1 root root 20036 Oct 28 17:32 /usr/share/phpldapadmin/htdocs/index.php
Run Code Online (Sandbox Code Playgroud)

我尝试将所有者更改为:www-data但不起作用.当我为圆形立方体尝试以下操作时,它不起作用:

location /roundcube/ {
    alias /var/www/roundcube;
    ...
}
Run Code Online (Sandbox Code Playgroud)

我认为这可能是尾随/或类似的问题,但我对nginx很新,所以我找不到它......

基本上,我有这个问题的反问题:https://stackoverflow.com/questions/31820362/nginx-403-directory-is-forbidden-when-using-root-location

Ric*_*ith 8

在location和alias都应该有一个结尾/,或者都没有尾随/.但在您的情况下,您应该使用root而不是alias两个位置块.

location /roundcube {
    root /var/www;
    index index.php;

    location ~ \.php$ {
        try_files $uri =404;

        fastcgi_pass unix:/var/run/php5-fpm.sock;

        include /etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

location /phpmyadmin {
    root  /usr/share;
    index  index.php index.html index.htm;

    location ~ \.php$ {
        try_files $uri =404;

        fastcgi_pass unix:/var/run/php5-fpm.sock;

        include /etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}
Run Code Online (Sandbox Code Playgroud)

在fastcgi_index中,只有相匹配的位置将不会做任何事情.php(见本文件).

SCRIPT_FILENAME两个块中都需要该参数(如果已经存在,则不需要该参数/etc/nginx/fastcgi_params).