设置:nginx/1.4.6、Ubuntu/14.04.3 LTS、Wordpress/4.5.4
我从子目录运行 Wordpress(遵循这篇文章:使用预先存在的子目录安装),我发现让网站正常运行的唯一方法是将其作为我的 nginx 配置的一部分:
location / {
try_files $uri /wordpress$uri/ /wordpress/index.php?$args;
}
...但我无法访问管理面板。使用任何其他 nginx 配置,我都可以看到 HTML 内容,但对于每个外部资产(css、图像、js 等),我都会得到 404 错误。
我按照以下帖子(以及许多其他帖子)尝试了许多不同的配置,但没有结果:
当前 nginx 配置:
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.php;
server_name mysite.com;
location / {
#try_files $uri $uri/ /index.php?$args;
#try_files $uri /wordpress$uri/ /wordpress/index.php?$args;
}
location /wordpress {
try_files $uri $uri/ /wordpress/index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
}
答案1
这是我在“博客”子目录中运行 wordpress 时使用的位置设置。more_clear_headers 需要编译到 Nginx 中的正确模块。
location / {
# If the file can't be found try adding a slash on the end - it might be
# a directory the client is looking for. Then try the Wordpress blog URL
# this might send a few requests to PHP that don't need to go that way
try_files $uri $uri/ /blog/index.php?$args;
more_clear_headers Server; more_clear_headers "Pragma"; more_clear_headers "Expires";
}
这部分很有帮助
# Add trailing slash to */wp-admin requests.
rewrite /blog/wp-admin$ $scheme://$host$uri/ permanent;
对于大多数人来说这不是必需的,但我为非管理页面更改了一些标题,以确保默认标题能够通过。
# Wordpress admin caching headers are set correctly, for pages and resources. The only reason we define
# this block separately is to avoid messing with the headers in the main php block.
# This is probably unnecessary because of the skip_cache variable and may be removed
location ~* wp-admin {
fastcgi_keep_conn on;
fastcgi_pass php56-fpm;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
}
我有一个Wordpress/Nginx 教程你可能会觉得它很有用。它包含你可以下载的配置文件,尽管我不认为我实际上将这个 Wordpress 模式包含在子目录中 - 不是 100% 确定。