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

标签 nginx relative-path phpldapadmin

我正在尝试在相对网址上使用nginx设置roundcube/phpldapadmin/...

example.com/roundcube
example.com/phpldapadmin

首先,Apache 2.4一切正常。我有以下文件夹:
# roundcube
/var/www/roundcube
# phpldapadmin
/usr/share/phpldapadmin

我有以下locationroundcube:
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;
    }
}

哪个工作正常,但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;
    }
}

我得到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: "****"

我检查了许可:
$ 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

我尝试将所有者更改为:www-data,但没有成功。当我对roundcube尝试以下操作时,它不起作用:
location /roundcube/ {
    alias /var/www/roundcube;
    ...
}

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

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

最佳答案

locationalias都应带有尾随/,或者都不应该带有尾随/。但是对于您的情况,两个位置块都应使用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;
    }
}
fastcgi_index在仅与.php匹配的位置中不会做任何事情(请参阅this document)。

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

关于nginx - 在Nginx中使用别名作为相对URL时的禁止位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35259014/

相关文章:

javascript - Javascript 中带有 fetch 的相对路径

c++ - C/C++ 是否有使用相对路径的跨平台方式?

ssl - 无法使用 Let's Encrypt 证书通过 ldaps 连接到服务器

php - 安装 nginx 后找不到 Laravel 路由

docker - Docker-compose + Nginx + Certbot +简单的Django Rest Framework应用

bash - 如何将 chdir 分配给 .bat 中的变量?

Docker nginx proxy, open ldap and phpldapadmin - 无法通过nginx访问ldap admin

ldap - 无法创建新条目。 PHPLDAP管理员

Nginx 默认错误页面仍然显示

python - uwsgi内部可调用文件模块导入错误