Nginx+WordPress子域多个,核心在子目录中

时间:2015-07-14 作者:guillaume.molter

我安装了WordPressthat works perfectly on a Apache server. 我目前正在改变我的托管公司(新的一家提供NGinx)和我的开发堆栈(转移到vvv)。

Here is how the install is set-up:

它是一个Wordpress子域多站点安装,核心在一个子文件夹(/wp-app/)中,wp内容在另一个子文件夹(/wp-app-content/)中,当然还有我的索引。php,wp配置。php和。根文件夹中的htaccess以及这两个文件夹。

Here is my problem :

在生产版本(apache)上,管理员工作正常,/wp app/文件夹完全隐藏。在开发版本(NGinx)上,管理员部分工作,但许多页面返回404,有时WP会使用WP app重写url。

What I think is the source of the problem :

我几乎可以肯定我的NGinx重写不好。我发现有几个feed提到了这个问题,但他们要么没有回答,要么回答不完整,对我不起作用。

My .htaccess file that works :

# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index\\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) wp-app/$1 [L]
RewriteRule ^(.*\\.php)$ wp-app/$1 [L]
RewriteRule . index.php [L]

# END WordPress
My nginx.conf that partially works :

server {

    listen       80;
    listen       443 ssl;
    server_name  guillaumemolter.dv *.guillaumemolter.dv 

    root        /srv/www/guillaumemolter/htdocs;
    index index.php;

    if (!-e $request_filename) {
        rewrite /wp-admin$ $scheme://$host$uri/ last;
        rewrite ^/(wp-.*.php)$ /wp-app/$1 last;
        rewrite ^/(wp-(content|admin|includes).*) /wp-app/$1 last;
    }

    location / {
        #try_files $uri $uri/ /wp-app/index.php?$args ;
        try_files $uri $uri/ /index.php?$args ;
    }

    location ~ \\.php$ {
        #try_files /wp-app/$uri =404;
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_pass 127.0.0.1:9000; 
    }

    location ~* ^.+\\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
        access_log off; log_not_found off; expires max;
    }

    location = /robots.txt { access_log off; log_not_found off; }
    location ~ /\\. { deny  all; access_log off; log_not_found off; }
}
这两行注释是我出于绝望而做的测试。。。。

Resources I read and used so far:

https://codex.wordpress.org/Nginxhttps://rtcamp.com/wordpress-nginx/tutorials/multisite/还有其他一些,包括来自这个网站的2个feed,我不能在这里发布,因为我没有足够的声誉。

非常感谢你的帮助。

1 个回复
SO网友:Adriano Monecchi

我使用非常相似的Nginx服务器设置,下面的内容对我来说非常有用,但是,我更喜欢采用不同的路由,将服务器块配置分别拆分为“非安全-http”和“安全-https”。此外,根据您的子目录结构,您还需要查看以下内容rtcamp article 为了调整location / {} 指令如下:

# HTTPS Secure Server 
#
server {
    listen 443 default_server ssl;

    ssl on;
    ssl_certificate /path/to/ssl/certificate.crt;
    ssl_certificate_key /path/to/ssl/certificate.key;

    ssl_session_cache    shared:SSL:10m;
    ssl_session_timeout  10m;

   # Nginx 1.6 PCI compliance ssl directives, the first one is stronger but slower
   #It should be preferred when saving credit card and/or sensible information is needed.
   #e.g. If redirecting to payment gateways providers such as PayPal is all you need, them it can be safely disabled here.

  # ssl_ciphers  HIGH:!aNULL:!MD5;

    ssl_ciphers HIGH:!aNULL:!MD5:!kEDH;
    ssl_prefer_server_ciphers  on;

    root /srv/www/guillaumemolter/htdocs;
    index index.php;

    server_name guillaumemolter.dv *.guillaumemolter.dv;

    location / {
     try_files $uri $uri/ /wp-app/index.php?q=$uri&$args;
    }
}

    location ~ \\.php$ {
            try_files      $uri = 404;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
    }

# Alternatively Pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

    #location ~ \\.php$ {
            #try_files $uri =404;
            #fastcgi_split_path_info ^(.+\\.php)(/.+)$;
            #fastcgi_pass unix:/var/run/php5-fpm.sock;
            #fastcgi_index index.php;
#                #include fastcgi_params;
    #}

    location ~* \\.(jpg|jpeg|gif|png|bmp|ico|pdf|flv|swf|exe|html|htm|txt|css|js) {
            add_header Cache-Control public;
            add_header Cache-Control must-revalidate;
            expires 7d;
            access_log off;
    }

}
。。

server {
    listen 80 default_server;
    server_name  guillaumemolter.dv *.guillaumemolter.dv 

    root   /srv/www/guillaumemolter/htdocs;
    index  index.php;

    # Simple redirect - Force non SSL site to redirect traffic to SSL
    return 301 https://guillaumemolter.dv$request_uri;
    return 301 https://guillaumemolter.dv$request_uri;


    if (!-e $request_filename) {
            rewrite /wp-admin$ $scheme://$host$uri/ permanent;         
            rewrite ^/wp-app(/[^/]+)?(/wp-.*) /wp-app$2 last;      
            rewrite ^/wp-app(/[^/]+)?(/.*\\.php)$ /wp-app$2 last;
    }

    location / {
            try_files $uri $uri/ /wp-app/index.php?$args ;
    }

    location ~ \\.php$ {
            try_files      $uri = 404;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
    }

# Alternatively Pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

    #location ~ \\.php$ {
            #try_files $uri =404;
            #fastcgi_split_path_info ^(.+\\.php)(/.+)$;
            #fastcgi_pass unix:/var/run/php5-fpm.sock;
            #fastcgi_index index.php;
#                #include fastcgi_params;
    #}

    location ~* ^.+\\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
            access_log off; log_not_found off; expires max;
    }

    location = /robots.txt { access_log off; log_not_found off; }
    location ~ /\\. { deny  all; access_log off; log_not_found off; }
} 
。。

在你的索引中。php文件,将路径更改为/wp-app/wp-blog-header.php

<?php
 /**
 * Front to the WordPress application. This file doesn\'t do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

 /**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */
 define(\'WP_USE_THEMES\', true);

 /** Loads the WordPress Environment and Template */
 require( dirname( __FILE__ ) . \'/wp-app/wp-blog-header.php\' );
。。

此外,如果设置了SSl证书,请确保WordPress站点URL和主页URL正确。可以在WordPress下更改General Settings 或者将这两行代码放在wp配置中。php,其中“example.com”是站点的正确位置。请注意,这不一定是最佳修复,它只是将值硬编码到站点本身。使用此方法时,您将无法再在“常规设置”页面上编辑它们。

define(\'WP_HOME\',\'https://example.com\');
define(\'WP_SITEURL\',\'https://example.com\');

结束

相关推荐

带有php5-fpm和nginx的WordPress

我正在尝试使用php5 fpm和nginx以及远程服务器上的数据库来完成wordpress的安装。然而,wordpress不承认mysql的存在,不允许我连接到远程数据库。php中用于检查mysql扩展是否已加载的代码表示扩展未加载:<?php var_dump(extension_loaded(\'mysql\')); mysql_connect(); 结果如下:bool(false) PHP message: PHP Fatal error: Call to