【发布时间】:2016-03-26 19:49:58
【问题描述】:
我正在尝试在 AWS ubuntu 实例上使用 nginx 设置 wordpress。但到目前为止,我无法解决 nginx 抛出的这个 403 禁止错误。这是我的 nginx 配置文件:
server {
listen 80;
server_name test.com;
return 301 https://www.test.com$request_uri;
}
server {
listen 80;
server_name www.test.com;
return 301 https://www.test.com$request_uri;
}
server {
listen 443;
server_name test.com;
return 301 https://www.test.com$request_uri;
}
server {
listen 443 ssl;
server_name www.test.com test.com;
root /home/ubuntu/wordpress;
index index.php index.html index.htm;
location / {
alias /home/ubuntu/wordpress;
try_files $uri $uri/ /index.php?$args;
location ~ \.php$ {
include fastcgi_params;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+?\.php)(.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
}
我的nginx 与用户www-data 一起运行,而/home/ubuntu/wordpress 目录由ubuntu 用户拥有。但是,我已授予 wordpress 目录 775 权限,因此这不应该成为问题。
但是当我访问www.test.com 时,它会显示 403 禁止错误。
nginx 错误日志显示如下消息:
2015/12/20 22:49:26 [error] 27984#0: *39 directory index of "/home/ubuntu/wordpress" is forbidden, client: xx.xxx.xxx.xxx, server: www.test.com, request: "GET / HTTP/1.1", host: "www.test.com"
我已经 php-fpm 与用户 www-data 竞争。我的 wordpress 网站的目录结构是这样的:
/home/ubuntu/wordpress
|
+-- index.php
+-- wp-content
这是我的.htaccess 文件,以备不时之需:
# directory browsing
Options All -Indexes
# Disable access to all file types except the following
Order deny,allow
Deny from all
<Files ~ ".(xml|css|js|jpe?g|png|gif|pdf|docx|rtf|odf|zip|rar)$">
Allow from all
</Files>
# Deny access to wp-config.php file
<files wp-config.php>
order allow,deny
deny from all
</files>
# Deny access to all .htaccess files
<files ~ "^.*\.([Hh][Tt][Aa])">
order allow,deny
deny from all
satisfy all
</files>
# Block wp-includes folder and files
RewriteEngine On
RewriteBase /
RewriteRule ^wp-admin/includes/ - [F,L]
RewriteRule !^wp-includes/ - [S=3]
RewriteRule ^wp-includes/[^/]+\.php$ - [F,L]
RewriteRule ^wp-includes/js/tinymce/langs/.+\.php - [F,L]
RewriteRule ^wp-includes/theme-compat/ - [F,L]
我在 Stack Exchange 网络上的不同站点上遇到了这个问题的多个重复项。已经尝试了其中的 15-20 种方法,但似乎没有一个在这里工作。我不确定缺少什么。有人可以看看这个吗?
我发现的另一个问题是:当我将location / {} 块中的alias 更改为root 时,nginx 开始将我重定向到www.test.com:8089。我不确定为什么它会一直重定向到 8089,即使我目前没有任何此类重定向规则。我之前确实有那个重定向规则,但现在已经不存在了。 nginx 是否在某处缓存重定向规则?对此我不确定。当我尝试访问 www.test.com/index.php 时也会发生重定向,其中有 alias 的东西。
请询问您是否需要其他文件信息。
【问题讨论】:
-
您根本不应该有
alias指令。您不需要将root指令放在位置块中,因为该值已经在server块中定义并被继承。您的浏览器可能正在缓存以前的重定向 - 确保每次重建时都清除缓存。 -
@RichardSmith 我一直在隐身浏览器窗口中尝试这个。如果我删除
alias指令,则会发生相同的重定向。 -
我唯一的解释是删除别名使其工作并且Wordpress导致重定向。
-
@RichardSmith 那么,
.htaccess文件有问题吗? -
Nginx 看不到 .htaccess 文件。这是一个 Apache 结构。
标签: php wordpress .htaccess nginx