4

I have a WordPress install that works perfectly on a Apache server. I am currently in the process of both changing my hosting company (the new one offer NGinx) and my development stack (moving to vvv).

Here is how the install is set-up:

It a Wordpress sub-domain multi-site install, with core in a sub-folder (/wp-app/), wp-content in another sub-folder (/wp-app-content/) and of course my index.php, wp-config.php and .htaccess in the root folder together with these 2 folders.

Here is my problem :

On the production version (apache), the admin works perfectly and the /wp-app/ folder is completely hidden. On the dev version (NGinx), the admin partially works but a lot of pages return a 404 and sometimes WP rewrite the url with wp-app.

What I think is the source of the problem :

I'm almost certain my NGinx rewrites are not good. I have found several feeds mentioning the issue, but they were either unanswered or the answer was partial and wasn't working for me.

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; }
}

The 2 commented lines are tests I made out of desperation....

Resources I read and used so far:

https://codex.wordpress.org/Nginx https://rtcamp.com/wordpress-nginx/tutorials/multisite/ And others, including 2 feeds from this site, that I can't post here because I don't have enough reputation.

Thank you very much for your help.

1

I use a very similar Nginx server set up and the following works great for me, however, I prefer to take a different route and split the server blocks configuration for "Non Secure - http" and "Secure - https" individually. Also as per your sub directory structure, you'd want to take a look at this rtcamp article in order to adjust your location / {} directive as follows:

# 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; }
} 

..

And in your index.php file, change the path to /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' );

..

Also, if a SSl certificate is set up, make sure WordPress site URL and Home URL are correct. It can be changed under WordPress General Settings or by placing the these two lines to your wp-config.php, where "example.com" is the correct location of your site. Note This is not necessarily the best fix, it's just hardcoding the values into the site itself. You won't be able to edit them on the General settings page anymore when using this method.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.