WordPress.org

Ready to get started?Download WordPress

Forums

Nested WordPress installations using Nginx (4 posts)

  1. davidossahdez
    Member
    Posted 1 year ago #

    I'm trying to setup a WordPress site to sell "Premium WordPress Themes" using Nginx. Let's say that the main domain is example.com and the subdomain is themes.example.com

    At this point everything is working properly. This is how my config file looks right now:

    # Main domain pointing to the main WordPress installation
    server {
        listen 80;
        root /srv/www/example.com/webdata;
        server_name example.com www.example.com localhost;
        include wordpress-site.conf;
    }
    
    # Subdomain pointing to another WordPress installation
    # This can be achieved using a WordPress Multisite setup,
    # but for this project, it needs to be separated.
    server {
        listen 80;
        root /srv/www/themes.example.com/webdata;
        server_name themes.example.com;
        include wordpress-site.conf;
     }

    Inside wordpress-site.conf are all the directives required for WordPress

    index index.php;
    
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }
    
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }
    
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
    
    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found off;
    }

    I'm getting trouble setting up the demo sites (to preview the premium themes online). The URL should look like this: themes.example.com/name-of-the-theme/demo.

    My first approach was to add a location block inside the subdomain directive. Something like this:

    # Subdomain pointing to another WordPress installation
    server {
        listen 80;
        root /srv/www/themes.example.com/webdata;
        server_name themes.example.com;
    
        location /name-of-the-theme/demo {
            # Points to another WordPress installation
            root /srv/www/themes.example.com/themes/name-of-the-theme/webdata;
        }
    
        include wordpress-site.conf;
     }

    The problem is that instead of open the index.php located in the specified root, I get a 404 error in themes.example.com because the page doesn't exist (obviously).

    How can I tell Nginx to use the specified WordPress Installation when the user type themes.example.com/name-of-the-theme/demo?

    I know that this can be achieved with Multisite Setup, but in this case I REALLY need separated installations.
    Thanks in advance!

    PD: This is how my directory structure looks like, if it helps. In the webdata directory are located all the WordPress files required for each site.

    [srv]
     |--[www]
         |--[example.com]  <---- Main Site
         |   |--[webdata]
         |   |--[logs]
         |--[themes.example.com]  <---- Subdomain
             |--[webdata]
             |--[logs]
             |--[themes]  <---- Demo Sites
                 |--[premium-theme-01]
                 |   |--[webdata]
                 |   |--[logs]
                 |--[premium-theme-02]
                 |   |--[webdata]
                 |   |--[logs]
                 |--[premium-theme-03]
                     |--[webdata]
                     |--[logs]
  2. Pothi
    Member
    Posted 1 year ago #

    Your current configuration doesn't work, because to serve PHP files for the demo site, the Nginx goes back to the "themes.example.com" site. Here's the possible solution...

    # Subdomain pointing to another WordPress installation
    server {
        listen 80;
        root /srv/www/themes.example.com/webdata;
        server_name themes.example.com;
    
        location /name-of-the-theme/demo {
            # Points to another WordPress installation
            root /srv/www/themes.example.com/themes;
    
            try_files $uri $uri/ /name-of-the-theme/demo/index.php;
    
            location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
            }
    
        }
    
        include wordpress-site.conf;
     }

    If it doesn't work, please post your error / access log too, that may help in troubleshooting further. If it works, you may want to copy the other configurations found in "wordpress-site.conf" and paste it inside the "location /name-of-the-theme/demo {" block.

    Update: I fixed an error with "root" directive. You may want to change the directory structure, to suit the above configuration. Alternatively, you may change the configuration to suit your directory structure!

  3. davidossahdez
    Member
    Posted 1 year ago #

    Hey Pothi, thanks! I didn't know that I can nest location blocks. Thank you for your advice it was VERY helpful. I made some changes to make this work. Take a look:

    location ~ ^/name-of-the-theme/demo {
            # Points to another WordPress installation
            root /srv/www/themes.example.com/themes;
            try_files $uri $uri/ /index.php?$args;
    
            location ~ \.php$ {
                    fastcgi_split_path_info ^(.+\.php)(/.+)$;
                    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    
                    fastcgi_pass unix:/var/run/php5-fpm.sock;
                    fastcgi_index index.php;
                    include fastcgi_params;
            }
    }

    The location block needs to use a regular expression because all the files (stylesheets, scripts and images) need to be redirected too. I was getting a 404 error with your code. The HTML loads correctly, but the resources don't. That little tilde makes the difference :D

  4. Pothi
    Member
    Posted 1 year ago #

    Thanks for the tip on tilde.

Topic Closed

This topic has been closed to new replies.

About this Topic