【发布时间】:2016-04-22 23:13:44
【问题描述】:
我正在运行 nginx 的 Ubuntu 服务器上安装 Wordpress。安装过程非常顺利(遵循Installing LEMP 和Installing Wordpress 教程 - 带我完成了 mysql、php5-fpm 和 wordpress 设置),并且似乎大部分都可以工作。我可以查看 Wordpress 管理页面、创建博客文章,甚至查看这些文章。但是当我尝试访问我博客的主页(例如 index.php)时,nginx 将文件作为下载而不是执行它。 Nginx serves .php files as downloads, instead of executing them的配置我已经试过了,没有用。
这是我的虚拟服务器文件:
server {
listen 80;
server_name my.domain.com;
root /my/wordpress/home/dir;
index index.php index.html index.htm;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location / {
try_files $uri $uri/ /index.php?$args;
rewrite ^/([_0-9a-zA-Z-]+/)?wp-admin$ /$1wp-admin/ redirect;
if (-e $request_filename) {
rewrite ^/([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) /$2 break;
}
rewrite ^/([_0-9a-zA-Z-]+/)?(.*\.php)$ /$2 break;
rewrite ^(.*)$ /index.php break;
}
}
还有 nginx.conf:
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
为了清楚起见,我删除了注释行。 sudo nginx -t 没有报错。
我怎样才能让 index.php 执行而不是作为下载服务?感谢您的帮助。
编辑:看起来是浏览器缓存问题。我尝试删除过去 24 小时内的缓存数据,但没有任何改变。删除所有内容后,它现在可以正确加载而不是下载。它也可以在其他浏览器/计算机上加载。
【问题讨论】:
-
rewrite规则和if的目的是什么。这是非标准的,这会导致与您的try_files规则发生重大冲突。 -
这是配置Wordpress告诉我添加的nginx翻译。你介意解释一下冲突是什么吗? Nginx 匹配最长的
location可能,因此任何以.php结尾的 URL 都应该由第一个块处理。我想这意味着不会使用第二个块中的几行,但我不明白为什么会导致问题。我是 nginx 新手,所以请解释一下。 -
谢谢@RichardSmith!!您的第二个链接最终正是我需要的配置,并使我的多站点设置正常工作。我一直在阅读的所有其他博客/文档/论坛帖子都是多年前的……很高兴看到 nginx 人写了他们自己的 wordpress 操作方法。
标签: php wordpress ubuntu nginx