【问题标题】:Multiple WordPress Installations with Nginx使用 Nginx 安装多个 WordPress
【发布时间】:2013-05-25 19:09:49
【问题描述】:

我正在尝试使用带有 nginx 的单个站点配置文件来为子文件夹中的任意数量的 WordPress 安装提供服务器,但根本无法获得漂亮的永久链接。

+ root
    + WordPressOne
    + WordPressTwo
    + WordPressThree

禁用永久链接后,/WordPressOne/?p=12 可以正常工作。启用永久链接后,/WordPressOne/MyPage/ 会从根文件夹发送 404。

由于子文件夹中的 WP 安装数量不断变化(它用于开发),我不想经常修改/创建/删除站点配置,我希望它能够正常工作,这样我就可以将新的 WordPress 安装复制到新的子文件夹,设置 WP,并为该安装设置永久链接,而无需重新启动 nginx 或 PHP-FPM。

这是/etc/nginx/sites-available/ 文件夹中使用的基本site.conf(它也反映了生产中使用的许多规则):

server {
    listen 5000 default;
    server_name dev wpdev;
    root /vagrant/sites;
    client_max_body_size 2m;
    expires -1;
    charset utf-8;
    index index.html index.php;

    location ~* ^.+\.(manifest|appcache)$ {
        expires -1;
        index index.html index.htm;
        uwsgi_cache off;
    }

    location ~* \.(?:rss|atom)$ {
        expires 1h;
        add_header Cache-Control "public";
    }

    location ~* ^.+\.(css    |js|jpg|jpeg|gif|png|ico|gz|svg|svgz|ttf|otf|woff|eot|mp4|ogg|ogv|webm|txt)$ {
        expires max;
        access_log off;
        index index.html index.htm;
        add_header Cache-Control "public";
        uwsgi_cache off;
    }

    location / {
        try_files $uri $uri/ /index.php?$args;
        uwsgi_cache off;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        uwsgi_cache off;
        include /etc/nginx/fastcgi_params;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
    }

    location ~ /\.ht {
        deny all;
    }

    add_header "X-UA-Compatible" "IE=Edge,chrome=1";
    add_header "Vary" "Accept-Language";
    add_header "Access-Control-Allow-Origin" "*";
    add_header "Cache-Control" "no-transform";
}

这也用作虚拟机配置的一部分(使用 Vagrant,如您所见),我希望设计师和 WP 开发人员不必担心在 nginx 中更新他们的站点文件。

谢谢!

【问题讨论】:

    标签: php wordpress nginx


    【解决方案1】:

    您似乎复制了一个基本的 WordPress 设置,但我没有看到任何尝试实现您想要的逻辑。每个 WordPress 安装都有一个名为 index.php 的路由器,它应该是漂亮 URL 工作的全部。他们需要携带站点根目录下的第一个路径,这可能是 WordPress 配置设置。 然后,您需要根据 request_uri 中的第一个路径组件设置根目录,并将不存在的文件和目录路由到相应的 index.php。 我不完全确定这会起作用,并且无法测试它的时间限制,所以试试这个并报告:

    set $wpinstall = '';
    location ~ /([^/]+)/(.*)$ {
        set $wpinstall = $1;
        try_files $uri $uri/ @wp;
    }
    
    location @wp {
        rewrite /$wpinstall/ /$wpinstall/index.php;
    }
    
    location ~ ^(.+\.php)(.*)$ {
        root /vagrant/sites;
        fastcgi_split_path_info ^(.*\.php)(.*)$;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root/$wpinstall$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        #...rest of fastcgi parameters
    }
    

    如果您报告回来,请务必通过设置适当的error_log 调试级别来包含重写调试信息。老实说,如果您使用基于this configuration 的虚拟主机,这可能会容易得多。您的开发人员只需要编辑他们的主机文件。

    【讨论】:

    • 这种方法确实有效,但所有资产(样式表、图像等)最终也都被重写了。
    • 我已经做出了一个“商业决策”,并决定我们将使用 Apache 代替这些开发人员 VM,并让 htaccess 文件处理重写。不过还是谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 2012-01-16
    • 1970-01-01
    • 1970-01-01
    • 2015-10-10
    • 2016-12-14
    • 2019-03-23
    相关资源
    最近更新 更多