在Nginx中使用别名作为相对URL时的禁止位置 [英] Forbidden location when using alias in nginx for relative urls
问题描述
我正在尝试在相对URL上使用nginx设置roundcube/phpldapadmin/...,例如:
I am trying to set up roundcube / phpldapadmin / ... with nginx on relative urls, e.g.:
example.com/roundcube
example.com/phpldapadmin
首先,Apache 2.4一切正常.我有以下文件夹:
First of all, everything was working fine with Apache 2.4. I have the following folders:
# roundcube
/var/www/roundcube
# phpldapadmin
/usr/share/phpldapadmin
对于roundcube
,我具有以下location
:
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
的以下操作不起作用:
Which works fine, but the following for phpldapadmin
does not work:
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
,其中包含以下日志:
I get a 403 forbidden
, with the following logs:
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尝试以下操作时,它不起作用:
I tried changing the owner to :www-data
but it did not work. When I tried the following for roundcube it did not work:
location /roundcube/ {
alias /var/www/roundcube;
...
}
我认为这可能是尾随/
或类似问题的问题,但是我真的是nginx的新手,所以我找不到它...
I am thinking that this is probably a problem with a trailing /
, or something similar, but I am really new to nginx so I can't find it...
基本上,我有这个问题的反问题: https://stackoverflow.com/questions/31820362/nginx-403-directory-is-forbidden-when-using-root-location
Basically, I have the inverse problem of this question : https://stackoverflow.com/questions/31820362/nginx-403-directory-is-forbidden-when-using-root-location
推荐答案
location
和alias
应该都带有尾随/
,或者都不应该带有尾随/
.但是对于您而言,两个定位块都应使用root
而不是alias
.
The location
and alias
should both have a trailing /
or neither have a trailing /
. But in your case, you should be using root
instead of alias
for both location blocks.
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
匹配的位置不做任何事情(请参见
The fastcgi_index
will not do anything in a location that only matches .php
(see this document).
两个块中都需要SCRIPT_FILENAME
参数(如果/etc/nginx/fastcgi_params
中已经没有,则两个参数都不需要).
The SCRIPT_FILENAME
parameter is needed in both blocks (or neither if it is already in /etc/nginx/fastcgi_params
).
这篇关于在Nginx中使用别名作为相对URL时的禁止位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!