Welcome to Centmin Mod Community
Register Now

Wordpress Wordpress Multisite + FastCGI_Cache + Pagespeed + Cloudflare

Discussion in 'Blogs & CMS usage' started by Peter Downey, Jul 22, 2014.

  1. Peter Downey

    Peter Downey Member

    62
    23
    8
    May 28, 2014
    Ratings:
    +25
    Local Time:
    8:06 AM
    I wanted to give anyone that is hoping to do a similar install a bit of a head start. I'm not 100% certain that this is the absolute perfect setup, but here is my .conf file for the domain that is hosting my WordPress multisite (subdirectory) installation. If anyone else is doing something similar please feel free to share your thoughts or configs.

    WordPress is installed in the root directory, you'd need to alter some text if you were installing to /wp as per the instructions here. The base of the code is from the centminmod github, and originated out of the previous Google+ community.

    I've enabled fastcgi cache, pagespeed, and cloudflare. In addition, I've also added sitemap rewrites for Yoast SEO plugin. The sitemap portion seems to work with other sitemap plugins as well, such as Google XML sitemaps. I didn't test any others.

    I've renamed the domains for clarity. Starting at domain1.com you'll install the vhost and wordpress per the normal methods outlined elsewhere on the site. For each additional site that you'll be installing, you'll want to add additional domains to the server_name entry.

    --- Plugins that are required ----
    • Nginx Helper: Once you enable wordpress multisite, Nginx Helper displays a new option to create an Nginx Map, which is supposed to speed things up. You'll want to check that option. For ease of use, I believe that fastcgi cache + nginx helper may be the most beneficial setup for WordPress multisite due to the automatic creation of the nginx map.
    • WordPress MU Domain Mapping: You'll need this if you want to assign unique domains to the individual multisites.
    --- Notes ---
    • This is configured for IPv4 only as that is what my server uses. If using IPv6 and Cloudflare you'll need to add additional set_real_ip_from Cloudflare IPv6 addresses
    • This config is for a WordPress Multisite install that is configured for subdirectories, not subdomains. I recommend reading this to make the required changes, you can also refer to this for additional info and clarifications.
    Follow the instructions for nginx wordpress + fastcgi_cache + pagespeed. Then make your domain's .conf file look like the example below.

    Full domain1.conf
    Wordpress Multisite post 3.5 install with Nginx, FastCGI_Cache, Cloudflare, and Pagespeed.

    Code:
    map $uri $blogname{
    ~^(?<blogpath>/[^/]+/)sites/(.*)    $blogpath ;
    }
    map $blogname $blogid{
        default -999;
            include /home/nginx/domains/domain1.com/public/wp-content/uploads/nginx-helper/map.conf ;
    }
    server {
    server_name domain1.com www.domain1.com domain2.com www.domain2.com domain3.com www.domain3.com ;
    
    # ngx_pagespeed & ngx_pagespeed handler
    include /usr/local/nginx/conf/pagespeed.conf;
    include /usr/local/nginx/conf/pagespeedhandler.conf;
    include /usr/local/nginx/conf/pagespeedstatslog.conf;
    
    # Cloudflare
    set_real_ip_from 199.27.128.0/21;
    set_real_ip_from 173.245.48.0/20;
    set_real_ip_from 103.21.244.0/22;
    set_real_ip_from 103.22.200.0/22;
    set_real_ip_from 103.31.4.0/22;
    set_real_ip_from 141.101.64.0/18;
    set_real_ip_from 108.162.192.0/18;
    set_real_ip_from 190.93.240.0/20;
    set_real_ip_from 188.114.96.0/20;
    set_real_ip_from 197.234.240.0/22;
    set_real_ip_from 198.41.128.0/17;
    set_real_ip_from 162.158.0.0/15;
    set_real_ip_from 104.16.0.0/12;
    real_ip_header CF-Connecting-IP;
    
      # limit_conn limit_per_ip 16;
      # ssi  on;
    
      access_log /home/nginx/domains/domain1.com/log/access.log combined buffer=8k;
      error_log /home/nginx/domains/domain1.com/log/error.log;
      #access_log /var/log/nginx/access.cache.log cache;
    
      root /home/nginx/domains/domain1.com/public;
    # Rewrites for WordPress SEO XML Sitemap
    rewrite ^/sitemap_index.xml$ /index.php?sitemap=1 last;
    rewrite ^/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
    }
    ## Admin Rewrites
    if (!-e $request_filename) {
        rewrite /wp-admin$ $scheme://$host$uri/ permanent;
        rewrite ^(/[^/]+)?(/wp-.*) $2 last;
        rewrite ^/[^/]+(/.*.php)$ $1 last;
            }
    location / {
        include /usr/local/nginx/conf/wpsecure.conf;
        include /usr/local/nginx/conf/wpcache.conf;
        ## Permalinks
        try_files $uri $uri/ /index.php?$args;
    
        if (-f $request_filename/index.html){
                    rewrite (.*) $1/index.html break;
        }
    
        if (-f $request_filename/index.php){
                    rewrite (.*) $1/index.php;
        }
        if (!-f $request_filename){
                    rewrite (.*) /index.php;
        }
    }
    # block common exploits, sql injections etc
    include /usr/local/nginx/conf/block.conf;
    # Other includes
    
        include /usr/local/nginx/conf/staticfiles.conf;
        include /usr/local/nginx/conf/phpwpcache.conf;
        include /usr/local/nginx/conf/drop.conf;
    }
    Updated 8/1/2014: Corrected mistake in nginx map configuration. Removed .xml files from browser expires. Added notes and made the post a bit prettier.
    Updated 1/15/2015: Slight changes to code.
     
    Last edited: Jan 15, 2015
    • Like Like x 3
  2. eva2000

    eva2000 Administrator Staff Member

    29,042
    6,590
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,785
    Local Time:
    10:06 PM
    Nginx 1.13.x
    MariaDB 5.5
  3. eva2000

    eva2000 Administrator Staff Member

    29,042
    6,590
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,785
    Local Time:
    10:06 PM
    Nginx 1.13.x
    MariaDB 5.5
    Awesome stuff @Peter Downey (y)

    thanks for sharing. I am sure other Wordpress Multi-site Centmin Mod Nginx users will be appreciative of this too :)
     
  4. Peter Downey

    Peter Downey Member

    62
    23
    8
    May 28, 2014
    Ratings:
    +25
    Local Time:
    8:06 AM
    I'm just so happy to give back to this awesome community, if even in a small way. As a Linux noob, you and the rest of the users here have helped and taught me so much. I cleaned up the original post a bit too, hopefully it will read easier for people that wander across it.

    I've got to say, I've tried wp-ffpc, supercache, and w3 total cache all on single install sites. Moving to WordPress Multisite and fastcgi_cache was the best idea ever. The initial config is way more complicated, but it's totally worth it.
     
  5. eva2000

    eva2000 Administrator Staff Member

    29,042
    6,590
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,785
    Local Time:
    10:06 PM
    Nginx 1.13.x
    MariaDB 5.5
    Indeed, I've always seen Wordpress multi-site has more complicated and avoided it myself. I prefer have each Wordpress install with it's own set of files - makes moving sites between servers easier to. But I guess everything comes down to hands on experience - the more you use or do something, the easier it gets :)
     
    • Like Like x 1
  6. Peter Downey

    Peter Downey Member

    62
    23
    8
    May 28, 2014
    Ratings:
    +25
    Local Time:
    8:06 AM
    So I've been working on updating this config for a fresh WordPress install. I believe that with new multisite installations, the ms-files and blog.dir entries are rendered obsolete. Now if you happen to have an older database that you've upgraded over time that was previously using 3.4, the code online should function perfectly. But Wordpress 3.5 and above now store multisite files differently. Old multisite uploads would be in /files, now files are stored in /wp-content/uploads/sitenumber/year/month.

    Everywhere I look the configs are the same and appear to be outdated for fresh installs, this goes for Nginx.org, Wordpress codex, and rt-files. They all make references to ms-files and the /files directory. Wordpress 3.5 actually did away with ms-files.php, and unless your database specifically has it turned on (either due to a pre-3.4 install or you changing an option in the database), it shouldn't be using it. I've pieced this config together as best I can figure out from researching what a few others have documented but I can't guarantee that this is 100% correct or that it even functions correctly.

    That being said, admin dashboard and login works, images work, plugins work. The test site is fully functional, so things appear to be working. Your milage may vary though. If there is anyone else that has any experience in this I'd love to hear it. I'm not entirely sure that the nginx map works correctly btw, I'm still painfully new to this.

    Wordpress Multisite post 3.5 install with Nginx, FastCGI_Cache, Cloudflare, and Pagespeed.
    Code:
    map $uri $blogname{
    ~^(?<blogpath>/[^/]+/)sites/(.*)    $blogpath ;
    }
    map $blogname $blogid{
        default -999;
            include /home/nginx/domains/domain1.com/public/wp-content/uploads/nginx-helper/map.conf ;
    }
    server {
    server_name domain1.com www.domain1.com domain2.com www.domain2.com domain3.com www.domain3.com ;
    
    # ngx_pagespeed & ngx_pagespeed handler
    include /usr/local/nginx/conf/pagespeed.conf;
    include /usr/local/nginx/conf/pagespeedhandler.conf;
    include /usr/local/nginx/conf/pagespeedstatslog.conf;
    
    # Cloudflare
    set_real_ip_from 199.27.128.0/21;
    set_real_ip_from 173.245.48.0/20;
    set_real_ip_from 103.21.244.0/22;
    set_real_ip_from 103.22.200.0/22;
    set_real_ip_from 103.31.4.0/22;
    set_real_ip_from 141.101.64.0/18;
    set_real_ip_from 108.162.192.0/18;
    set_real_ip_from 190.93.240.0/20;
    set_real_ip_from 188.114.96.0/20;
    set_real_ip_from 197.234.240.0/22;
    set_real_ip_from 198.41.128.0/17;
    set_real_ip_from 162.158.0.0/15;
    set_real_ip_from 104.16.0.0/12;
    real_ip_header CF-Connecting-IP;
    
      # limit_conn limit_per_ip 16;
      # ssi  on;
    
      access_log /home/nginx/domains/domain1.com/log/access.log combined buffer=8k;
      error_log /home/nginx/domains/domain1.com/log/error.log;
      #access_log /var/log/nginx/access.cache.log cache;
    
      root /home/nginx/domains/domain1.com/public;
    # Rewrites for WordPress SEO XML Sitemap
    rewrite ^/sitemap_index.xml$ /index.php?sitemap=1 last;
    rewrite ^/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
    }
    ## Admin Rewrites
    if (!-e $request_filename) {
        rewrite /wp-admin$ $scheme://$host$uri/ permanent;
        rewrite ^(/[^/]+)?(/wp-.*) $2 last;
        rewrite ^/[^/]+(/.*.php)$ $1 last;
            }
    location / {
        include /usr/local/nginx/conf/wpsecure.conf;
        include /usr/local/nginx/conf/wpcache.conf;
        ## Permalinks
        try_files $uri $uri/ /index.php?$args;
    
        if (-f $request_filename/index.html){
                    rewrite (.*) $1/index.html break;
        }
    
        if (-f $request_filename/index.php){
                    rewrite (.*) $1/index.php;
        }
        if (!-f $request_filename){
                    rewrite (.*) /index.php;
        }
    }
    # block common exploits, sql injections etc
    include /usr/local/nginx/conf/block.conf;
    # Other includes
    
        include /usr/local/nginx/conf/staticfiles.conf;
        include /usr/local/nginx/conf/phpwpcache.conf;
        include /usr/local/nginx/conf/drop.conf;
    }
     
    Last edited: Jan 15, 2015
    • Like Like x 2
  7. eva2000

    eva2000 Administrator Staff Member

    29,042
    6,590
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,785
    Local Time:
    10:06 PM
    Nginx 1.13.x
    MariaDB 5.5
  8. eva2000

    eva2000 Administrator Staff Member

    29,042
    6,590
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,785
    Local Time:
    10:06 PM
    Nginx 1.13.x
    MariaDB 5.5
    FYI @Peter Downey I've stickied this thread to the top of the forum. It deserves it :)
     
    • Like Like x 1
  9. Daniel J. Lewis

    Daniel J. Lewis Award-winning podcaster and consultant

    72
    8
    8
    Oct 20, 2014
    Ratings:
    +11
    Local Time:
    8:06 AM
    1.8.0
    5.6
    What made you choose fastcgi cache and pagespeed for Multisite instead of the WP Super Cache method?
     
  10. Daniel J. Lewis

    Daniel J. Lewis Award-winning podcaster and consultant

    72
    8
    8
    Oct 20, 2014
    Ratings:
    +11
    Local Time:
    8:06 AM
    1.8.0
    5.6
    In my tests, this doesn't seem to perform as well (or as easily) as the simpler WP Super Cache method.
     
  11. Daniel J. Lewis

    Daniel J. Lewis Award-winning podcaster and consultant

    72
    8
    8
    Oct 20, 2014
    Ratings:
    +11
    Local Time:
    8:06 AM
    1.8.0
    5.6
    I think there's something wrong with either this approach, or the Centminmod recommendation. Something is omitting the trailing slash.

    So if I login to WP with /wp-admin, none of the admin pages work besides the dashboard. They want to go to /edit.php instead of /wp-admin/edit.php.

    But if I manually add the trailing slash, to /wp-admin/, then everything works.
     
  12. Daniel J. Lewis

    Daniel J. Lewis Award-winning podcaster and consultant

    72
    8
    8
    Oct 20, 2014
    Ratings:
    +11
    Local Time:
    8:06 AM
    1.8.0
    5.6
    I figured out the trailing slash problem.

    include /usr/local/nginx/conf/wpnocache.conf;

    Should not be in location /, but near the bottom of the site conf file with the other includes.
     
  13. eva2000

    eva2000 Administrator Staff Member

    29,042
    6,590
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,785
    Local Time:
    10:06 PM
    Nginx 1.13.x
    MariaDB 5.5
    i don't see a wpnocache.conf at all in @Peter Downey above config it seems
     
  14. Daniel J. Lewis

    Daniel J. Lewis Award-winning podcaster and consultant

    72
    8
    8
    Oct 20, 2014
    Ratings:
    +11
    Local Time:
    8:06 AM
    1.8.0
    5.6
    I may have put it in "location /" because of some unclear directions in the centminmod documentation.
     
  15. eva2000

    eva2000 Administrator Staff Member

    29,042
    6,590
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,785
    Local Time:
    10:06 PM
    Nginx 1.13.x
    MariaDB 5.5
    wpnocache is for non-fastcgi_cache setup :)
     
  16. Daniel J. Lewis

    Daniel J. Lewis Award-winning podcaster and consultant

    72
    8
    8
    Oct 20, 2014
    Ratings:
    +11
    Local Time:
    8:06 AM
    1.8.0
    5.6
    It's wpnocache that has the trailing slash code:
    Code:
    # Add trailing slash to */wp-admin requests.
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;
     
  17. eva2000

    eva2000 Administrator Staff Member

    29,042
    6,590
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,785
    Local Time:
    10:06 PM
    Nginx 1.13.x
    MariaDB 5.5
    it's also contained in /usr/local/nginx/conf/wpcache.conf which fastcgi_cache setup instructs to use at Nginx Wordpress Configuration Examples for rewrite rules & FastCGI Caching

    Code:
    #fastcgi_cache start
    set $no_cache 0;
    
    # POST requests and urls with a query string should always go to PHP
    if ($request_method = POST) {
            set $no_cache 1;
    }
    if ($query_string != "") {
            set $no_cache 1;
    }
    
    # Don't cache uris containing the following segments
    if ($request_uri ~* "(memcache.php|apc.php|/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
            set $no_cache 1;
    }
    
    # Don't use the cache for logged in users or recent commenters
    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in|wordpress-sec") {
            set $no_cache 1;
    }
    
    # Add trailing slash to */wp-admin requests.
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;
    
    # Centmin Mod Nginx has nginx_cache_purge module
    # added http://centminmod.com/nginx.html
        location ~ /purge(/.*) {
        fastcgi_cache_purge WPCACHE "$scheme$request_method$host$1";
        return 200;
        allow 127.0.0.1;
        deny all;
        } 
    I am not using wpnocache.conf for my fastcgi_cached wordpress blog at blog.centminmod.com and works fine

    but looks like Peter's multi site Wordpress config moved the wpsecure.conf lower down within location root / so move the 2 includes higher up within location root /
    Code:
    include /usr/local/nginx/conf/wpsecure.conf;
    include /usr/local/nginx/conf/wpcache.conf;
    
    so ends up like
    Code:
    map $uri $blogname{
        ~^(?<blogpath>/[^/]+/)files/(.*)    $blogpath ;
    }
    map $blogname $blogid{
        default -999;
            include /home/nginx/domains/domain1.com/public/wp-content/uploads/nginx-helper/map.conf ;
    }
    server {
    server_name domain1.com www.domain1.com domain2.com www.domain2.com domain3.com www.domain3.com domain4.com www.domain4.com domain5.com www.domain5.com;
    
    # ngx_pagespeed & ngx_pagespeed handler
    include /usr/local/nginx/conf/pagespeed.conf;
    include /usr/local/nginx/conf/pagespeedhandler.conf;
    include /usr/local/nginx/conf/pagespeedstatslog.conf;
    
    # Cloudflare
    set_real_ip_from 199.27.128.0/21;
    set_real_ip_from 173.245.48.0/20;
    set_real_ip_from 103.21.244.0/22;
    set_real_ip_from 103.22.200.0/22;
    set_real_ip_from 103.31.4.0/22;
    set_real_ip_from 141.101.64.0/18;
    set_real_ip_from 108.162.192.0/18;
    set_real_ip_from 190.93.240.0/20;
    set_real_ip_from 188.114.96.0/20;
    set_real_ip_from 197.234.240.0/22;
    set_real_ip_from 198.41.128.0/17;
    set_real_ip_from 162.158.0.0/15;
    set_real_ip_from 104.16.0.0/12;
    real_ip_header CF-Connecting-IP;
    
      # limit_conn limit_per_ip 16;
      # ssi  on;
    
      access_log /home/nginx/domains/domain1.com/log/access.log combined buffer=8k;
      error_log /home/nginx/domains/domain1.com/log/error.log;
      access_log /var/log/nginx/access.cache.log cache;
    
      root /home/nginx/domains/domain1.com/public;
    #Yoast sitemap
        location ~ ([^/]*)sitemap(.*)\.x(m|s)l$ {
        rewrite ^/sitemap\.xml$ /sitemap_index.xml permanent;
        rewrite ^/([a-z]+)?-?sitemap\.xsl$ /index.php?xsl=$1 last;
        rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last;
        rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
    
        ## following lines are options. Needed for wordpress-seo addons
        rewrite ^/news_sitemap\.xml$ /index.php?sitemap=wpseo_news last;
        rewrite ^/locations\.kml$ /index.php?sitemap=wpseo_local_kml last;
        rewrite ^/geo_sitemap\.xml$ /index.php?sitemap=wpseo_local last;
        rewrite ^/video-sitemap\.xsl$ /index.php?xsl=video last;
        access_log off;
    }
    location ~ ^(/[^/]+/)?files/(?<rt_file>.+) {
            try_files /wp-content/blogs.dir/$blogid/files/$rt_file /wp-includes/ms-files.php?file=$rt_file ;
            access_log off;    log_not_found off; expires max;
        }
    
          location / {
    
    include /usr/local/nginx/conf/wpsecure.conf;
    include /usr/local/nginx/conf/wpcache.conf;
    
        if (-f $request_filename/index.html){
                    rewrite (.*) $1/index.html break;
        }
    
        if (-f $request_filename/index.php){
                    rewrite (.*) $1/index.php;
        }
    
        if (!-f $request_filename){
                    rewrite (.*) /index.php;
        }
    
    # block common exploits, sql injections etc
    include /usr/local/nginx/conf/block.conf;
    }
    # Pass uploaded files to wp-includes/ms-files.php.
        rewrite /files/$ /index.php last;
    
        if ($uri !~ wp-content/plugins) {
            rewrite /files/(.+)$ /wp-includes/ms-files.php?file=$1 last;
        }
    
        # Rewrite multisite '.../wp-.' and '.../.php'.
        if (!-e $request_filename) {
            rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
            rewrite ^/[_0-9a-zA-Z-]+.(/wp-admin/.\.php)$ $1 last;
            rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
        }
      include /usr/local/nginx/conf/staticfiles.conf;
      include /usr/local/nginx/conf/phpwpcache.conf;
      #include /usr/local/nginx/conf/php.conf;
      include /usr/local/nginx/conf/drop.conf;
      #include /usr/local/nginx/conf/errorpage.conf;
      }
    if the problem with include /usr/local/nginx/conf/wpcache.conf; is same as with wpnocache.conf which both contain admin add slash within location root, then could be specific to multi-site setup ?

    As I said, never used multi-site setup myself
     
    Last edited: Oct 23, 2014
  18. Daniel J. Lewis

    Daniel J. Lewis Award-winning podcaster and consultant

    72
    8
    8
    Oct 20, 2014
    Ratings:
    +11
    Local Time:
    8:06 AM
    1.8.0
    5.6
    With wpcache.conf inside
    Code:
    location /
    , /wp-admin does not get a trailing slash.

    My WordPress Multisite is installed in the domain root. Here's my site's conf:
    Code:
    # redirect from non-www to www 
    # uncomment, save file and restart Nginx to enable
    #server {
    #            listen   80;
    #            server_name MYDOMAIN.com;
    #            return 301 $scheme://www.MYDOMAIN.com$request_uri;
    #       }
    
    map $uri $blogname{
        ~^(?<blogpath>/[^/]+/)files/(.*)    $blogpath ;
    }
    map $blogname $blogid {
        default -999;
            include /home/nginx/domains/MYDOMAIN.com/public/wp-content/uploads/nginx-helper/map.conf ;
    }
    
    server {
      server_name MYDOMAIN.com www.MYDOMAIN.com vultr2.MYDOMAIN.com *.com;
    
      # ngx_pagespeed & ngx_pagespeed handler
      include /usr/local/nginx/conf/pagespeed.conf;
      include /usr/local/nginx/conf/pagespeedhandler.conf;
      include /usr/local/nginx/conf/pagespeedstatslog.conf;
    
      # limit_conn limit_per_ip 16;
      # ssi  on;
    
      access_log /home/nginx/domains/MYDOMAIN.com/log/access.log combined buffer=32k;
      access_log /var/log/nginx/access.cache.log cache;
      error_log /home/nginx/domains/MYDOMAIN.com/log/error.log;
    
      root /home/nginx/domains/MYDOMAIN.com/public;
    
      #Yoast sitemap
        location ~ ([^/]*)sitemap(.*)\.x(m|s)l$ {
        rewrite ^/sitemap\.xml$ /sitemap_index.xml permanent;
        rewrite ^/([a-z]+)?-?sitemap\.xsl$ /index.php?xsl=$1 last;
        rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last;
        rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
    
        ## following lines are options. Needed for wordpress-seo addons
        rewrite ^/video-sitemap\.xsl$ /index.php?xsl=video last;
        access_log off;
      }
    
      location ~ ^(/[^/]+/)?files/(?<rt_file>.+) {
        try_files /wp-content/blogs.dir/$blogid/files/$rt_file /wp-includes/ms-files.php?file=$rt_file ;
        access_log off;    log_not_found off; expires max;
      }
    
      location / {
        if (-f $request_filename/index.html){
          rewrite (.*) $1/index.html break;
        }
    
        if (-f $request_filename/index.php){
          rewrite (.*) $1/index.php;
        }
    
        if (!-f $request_filename){
          rewrite (.*) /index.php;
        }
    
        include /usr/local/nginx/conf/wpsecure.conf;
        include /usr/local/nginx/conf/wpcache.conf;
        include /usr/local/nginx/conf/phpwpcache.conf;
    
        # block common exploits, sql injections etc
        #include /usr/local/nginx/conf/block.conf;
      }
    
      # Pass uploaded files to wp-includes/ms-files.php.
      rewrite /files/$ /index.php last;
    
      if ($uri !~ wp-content/plugins) {
        rewrite /files/(.+)$ /wp-includes/ms-files.php?file=$1 last;
      }
    
      # Rewrite multisite '.../wp-.' and '.../.php'.
      if (!-e $request_filename) {
        rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
        rewrite ^/[_0-9a-zA-Z-]+.(/wp-admin/.\.php)$ $1 last;
        rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
      }
    
      include /usr/local/nginx/conf/staticfiles.conf;
      include /usr/local/nginx/conf/php.conf;
      #include /usr/local/nginx/conf/phpstatus.conf;
      include /usr/local/nginx/conf/drop.conf;
      #include /usr/local/nginx/conf/errorpage.conf;
    }
    
     
  19. eva2000

    eva2000 Administrator Staff Member

    29,042
    6,590
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,785
    Local Time:
    10:06 PM
    Nginx 1.13.x
    MariaDB 5.5
    try just creating a new include file at /usr/local/nginx/conf/wpadminslash.conf with contents
    Code:
    # Add trailing slash to */wp-admin requests.
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;
    and adding it to end of vhost
    Code:
    # redirect from non-www to www
    # uncomment, save file and restart Nginx to enable
    #server {
    #            listen   80;
    #            server_name MYDOMAIN.com;
    #            return 301 $scheme://www.MYDOMAIN.com$request_uri;
    #       }
    
    map $uri $blogname{
        ~^(?<blogpath>/[^/]+/)files/(.*)    $blogpath ;
    }
    map $blogname $blogid {
        default -999;
            include /home/nginx/domains/MYDOMAIN.com/public/wp-content/uploads/nginx-helper/map.conf ;
    }
    
    server {
      server_name MYDOMAIN.com www.MYDOMAIN.com vultr2.MYDOMAIN.com *.com;
    
      # ngx_pagespeed & ngx_pagespeed handler
      include /usr/local/nginx/conf/pagespeed.conf;
      include /usr/local/nginx/conf/pagespeedhandler.conf;
      include /usr/local/nginx/conf/pagespeedstatslog.conf;
    
      # limit_conn limit_per_ip 16;
      # ssi  on;
    
      access_log /home/nginx/domains/MYDOMAIN.com/log/access.log combined buffer=32k;
      access_log /var/log/nginx/access.cache.log cache;
      error_log /home/nginx/domains/MYDOMAIN.com/log/error.log;
    
      root /home/nginx/domains/MYDOMAIN.com/public;
    
      #Yoast sitemap
        location ~ ([^/]*)sitemap(.*)\.x(m|s)l$ {
        rewrite ^/sitemap\.xml$ /sitemap_index.xml permanent;
        rewrite ^/([a-z]+)?-?sitemap\.xsl$ /index.php?xsl=$1 last;
        rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last;
        rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
    
        ## following lines are options. Needed for wordpress-seo addons
        rewrite ^/video-sitemap\.xsl$ /index.php?xsl=video last;
        access_log off;
      }
    
      location ~ ^(/[^/]+/)?files/(?<rt_file>.+) {
        try_files /wp-content/blogs.dir/$blogid/files/$rt_file /wp-includes/ms-files.php?file=$rt_file ;
        access_log off;    log_not_found off; expires max;
      }
    
      location / {
        if (-f $request_filename/index.html){
          rewrite (.*) $1/index.html break;
        }
    
        if (-f $request_filename/index.php){
          rewrite (.*) $1/index.php;
        }
    
        if (!-f $request_filename){
          rewrite (.*) /index.php;
        }
    
        include /usr/local/nginx/conf/wpsecure.conf;
        include /usr/local/nginx/conf/wpcache.conf;
        include /usr/local/nginx/conf/phpwpcache.conf;
    
        # block common exploits, sql injections etc
        #include /usr/local/nginx/conf/block.conf;
      }
    
      # Pass uploaded files to wp-includes/ms-files.php.
      rewrite /files/$ /index.php last;
    
      if ($uri !~ wp-content/plugins) {
        rewrite /files/(.+)$ /wp-includes/ms-files.php?file=$1 last;
      }
    
      # Rewrite multisite '.../wp-.' and '.../.php'.
      if (!-e $request_filename) {
        rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
        rewrite ^/[_0-9a-zA-Z-]+.(/wp-admin/.\.php)$ $1 last;
        rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
      }
    
      include /usr/local/nginx/conf/wpadminslash.conf;
      include /usr/local/nginx/conf/staticfiles.conf;
      include /usr/local/nginx/conf/php.conf;
      #include /usr/local/nginx/conf/phpstatus.conf;
      include /usr/local/nginx/conf/drop.conf;
      #include /usr/local/nginx/conf/errorpage.conf;
    }
     
  20. Daniel J. Lewis

    Daniel J. Lewis Award-winning podcaster and consultant

    72
    8
    8
    Oct 20, 2014
    Ratings:
    +11
    Local Time:
    8:06 AM
    1.8.0
    5.6
    I moved things around, and I think I have it working now.

    Code:
    # redirect from non-www to www 
    # uncomment, save file and restart Nginx to enable
    #server {
    #            listen   80;
    #            server_name MYDOMAIN.com;
    #            return 301 $scheme://www.MYDOMAIN.com$request_uri;
    #       }
    
    map $uri $blogname{
        ~^(?<blogpath>/[^/]+/)files/(.*)    $blogpath ;
    }
    map $blogname $blogid {
        default -999;
            include /home/nginx/domains/MYDOMAIN.com/public/wp-content/uploads/nginx-helper/map.conf ;
    }
    
    server {
      server_name MYDOMAIN.com www.MYDOMAIN.com vultr2.MYDOMAIN.com *.com;
    
      # ngx_pagespeed & ngx_pagespeed handler
      include /usr/local/nginx/conf/pagespeed.conf;
      include /usr/local/nginx/conf/pagespeedhandler.conf;
      include /usr/local/nginx/conf/pagespeedstatslog.conf;
    
      # limit_conn limit_per_ip 16;
      # ssi  on;
    
      access_log /home/nginx/domains/MYDOMAIN.com/log/access.log combined buffer=32k;
      access_log /var/log/nginx/access.cache.log cache;
      error_log /home/nginx/domains/MYDOMAIN.com/log/error.log;
    
      root /home/nginx/domains/MYDOMAIN.com/public;
    
      #Yoast sitemap
        location ~ ([^/]*)sitemap(.*)\.x(m|s)l$ {
        rewrite ^/sitemap\.xml$ /sitemap_index.xml permanent;
        rewrite ^/([a-z]+)?-?sitemap\.xsl$ /index.php?xsl=$1 last;
        rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last;
        rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
    
        ## following lines are options. Needed for wordpress-seo addons
        rewrite ^/video-sitemap\.xsl$ /index.php?xsl=video last;
        access_log off;
      }
    
      location ~ ^(/[^/]+/)?files/(?<rt_file>.+) {
        try_files /wp-content/blogs.dir/$blogid/files/$rt_file /wp-includes/ms-files.php?file=$rt_file ;
        access_log off;    log_not_found off; expires max;
      }
    
      location / {
        include /usr/local/nginx/conf/wpsecure.conf;
        include /usr/local/nginx/conf/wpcache.conf;
    
        if (-f $request_filename/index.html){
          rewrite (.*) $1/index.html break;
        }
    
        if (-f $request_filename/index.php){
          rewrite (.*) $1/index.php;
        }
    
        if (!-f $request_filename){
          rewrite (.*) /index.php;
        }
    
        # block common exploits, sql injections etc
        include /usr/local/nginx/conf/block.conf;
      }
    
      # Pass uploaded files to wp-includes/ms-files.php.
      rewrite /files/$ /index.php last;
    
      if ($uri !~ wp-content/plugins) {
        rewrite /files/(.+)$ /wp-includes/ms-files.php?file=$1 last;
      }
    
      # Rewrite multisite '.../wp-.' and '.../.php'.
      if (!-e $request_filename) {
        rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
        rewrite ^/[_0-9a-zA-Z-]+.(/wp-admin/.\.php)$ $1 last;
        rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
      }
    
      include /usr/local/nginx/conf/staticfiles.conf;
      include /usr/local/nginx/conf/phpwpcache.conf;
      #include /usr/local/nginx/conf/php.conf;
      #include /usr/local/nginx/conf/phpstatus.conf;
      include /usr/local/nginx/conf/drop.conf;
      #include /usr/local/nginx/conf/errorpage.conf;
    }