在nginx中为相对网址

时间:2016-02-07 20:54:47

标签: nginx relative-path phpldapadmin

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

example.com/roundcube
example.com/phpldapadmin

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

# roundcube
/var/www/roundcube
# phpldapadmin
/usr/share/phpldapadmin

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

哪种方法正常,但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,但它无效。当我在圆形立方体上尝试以下操作时,它不起作用:

location /roundcube/ {
    alias /var/www/roundcube;
    ...
}

我认为这可能是一个跟踪/或类似问题的问题,但我对nginx很新,所以我无法找到它......

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

2 个答案:

答案 0 :(得分:5)

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中,则两者都不需要。)

答案 1 :(得分:0)

或者,您可以尝试在nginx.conf>>顶部写字。用户username

由于我使用的是AWS Linux EC2实例,我写了

user ec2-user;

而不是

user nginx;

这可以通过提供所有必需的权限来解决问题