nginx catch-all server_name for WordPress Multisite

April 15, 2016 1.2k views
Nginx WordPress

Is it possible to do something like?

server_name   * ;

I don't want to have to manually add each site like this:

server_name examplewp.com *.examplewp.com shoppingsite.com companysite.org;

As there may be hundreds of sites on a multisite.

This is my current code:

server {

  listen 80 ;

  server_name   mainsite.com  *.mainsite.com site1.co site2.co site3.co ;
  access_log   /srv/www/mainsite.com/logs/access.log;
  error_log    /srv/www/mainsite.com/logs/error.log;

  root  /srv/www/mainsite.com/current/web;
  index index.php index.htm index.html;

  charset utf-8;


  rewrite /wp-admin$ $scheme://$host$uri/ last;
  rewrite ^/(wp-.*.php)$ /wp/$1 last;
      rewrite ^/(wp-(content|admin|includes).*) /wp/$1 last;
  add_header X-Content-Type-Options "nosniff" always;
  add_header X-Frame-Options SAMEORIGIN;
  add_header X-Xss-Protection "1; mode=block" always;


  include includes.d/mainsite.com/*.conf;
  include wordpress.conf;

  location ~ \.php$ {
    try_files $uri =404;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    fastcgi_param DOCUMENT_ROOT $realpath_root;
    fastcgi_pass unix:/var/run/php5-fpm-wordpress.sock;
    }
}

"mainsite.com.conf" [readonly] 41L, 1301C

I tried this and it did't work:

server {

  listen 80 default_server ;

  server_name   _ ;
  access_log   /srv/www/mainsite.com/logs/access.log;
  error_log    /srv/www/mainsite.com/logs/error.log;

  root  /srv/www/mainsite.com/current/web;
  index index.php index.htm index.html;

  charset utf-8;


  rewrite /wp-admin$ $scheme://$host$uri/ last;
  rewrite ^/(wp-.*.php)$ /wp/$1 last;
      rewrite ^/(wp-(content|admin|includes).*) /wp/$1 last;
  add_header X-Content-Type-Options "nosniff" always;
  add_header X-Frame-Options SAMEORIGIN;
  add_header X-Xss-Protection "1; mode=block" always;


  include includes.d/mainsite.com/*.conf;
  include wordpress.conf;

  location ~ \.php$ {
    try_files $uri =404;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    fastcgi_param DOCUMENT_ROOT $realpath_root;
    fastcgi_pass unix:/var/run/php5-fpm-wordpress.sock;
    }
}

"mainsite.com.conf" [readonly] 41L, 1301C

I'm a complete nginx noob and any help is much appreciated!!

1 comment
  • Problem solved. There was another .conf file (no-default.conf) that also had default_server defined. Once I deleted no-default.conf the catch-all was working.

1 Answer

This question was answered by @michaelharrigan:

Problem solved. There was another .conf file (no-default.conf) that also had default_server defined. Once I deleted no-default.conf the catch-all was working.

View the original comment

Have another answer? Share your knowledge.