3

The root (http://www.example.com/) will point to /var/www/wordpress

How can I allow each one to be viewable in browser?

working -- http://www.example.com
error -- http://www.example.com/wordpress2
error -- http://www.example.com/htmlsite

Here is the structure:

first wordpress: /var/www/wordpress
second wordpress: /var/www/wordpress2
static html page: /var/www/htmlsite

server {
root /var/www/wordpress;
index index.php;
...

location / {
  try_files $uri $uri/ /index.php$is_args$args;
}

location /wordpress2 {
  root /var/www/wordpress2;
  try_files $uri $uri/ /index.php$is_args$args;
}

location /htmlsite {
  root /var/www/htmlsite;
  try_files $uri $uri/ =404;
}

}

If I do this root /var/www; instead, then /wordpress2 and /wordpress3 works:

server {
root /var/www;
index index.php;
...

location / {
  try_files $uri $uri/ =404;
}

location /wordpress2 {
  try_files $uri $uri/ /wordpress2/index.php$is_args$args;
}

location /wordpress3 {
  try_files $uri $uri/ /wordpress3/index.php$is_args$args;
}

location /htmlsite {
  root /var/www/htmlsite;
  try_files $uri $uri/ =404;
}

}
Charles
  • 41
  • 1
  • 1
  • 5
  • You need to define a server for each Wordpress site, not a location. Have a look at my tutorial, which has example config files: https://www.photographerstechsupport.com/tutorials/hosting-wordpress-on-aws-tutorial-pt1-introduction-configuration-downloads/ – Tim Jan 19 '17 at 19:40
  • @EEAA the question isn't that well worded, but I don't think it deserved to be put on hold. Suspect a non-native English speaker. – Tim Jan 19 '17 at 19:41
  • @Tim It was phantom edited since the original posting. The original had no questions in it. I'll re-open. – EEAA Jan 19 '17 at 19:42
  • You could put wordpress2 and wordpress3 in one rule with a wildcard, see https://serverfault.com/a/787599/128892 – rubo77 May 12 '21 at 18:06

3 Answers3

7

This should work the way you want to.

Since the URI is appended to the directory specified in root directive, we need to specify it only once. Only the `try_files´ has to be specified for each location separately.

server {
    root /var/www/wordpress;
    index index.php;
    ...

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location /wordpress2 {
        root /var/www;
        try_files $uri $uri/ /wordpress2/index.php$is_args$args;
    }

    location /htmlsite {
        root /var/www;
        try_files $uri $uri/ =404;
    }
}
Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
0

I assume you have three Wordpress instances on three domains. In that case you need to define servers for each website and Wordpress install.

If you want multiple Wordpress installs on one domain then your approach is generally correct, but you need to define the handover to PHP. I have a good tutorial on that, but really you can just google "Nginx Wordpress tutorial" and find 100 tutorials.

Suggest you start with getting one working then add others.

Tim
  • 31,888
  • 7
  • 52
  • 78
  • Tried your google search suggestion and looked at first page results but only show single install without much suggestion to my situation. Thanks. – Charles Jan 19 '17 at 20:00
  • You need to describe your situation and problem better. You haven't stated your desired end state (one domain, many domains) and haven't said exactly what's wrong. Plus your Nginx config file having a lack of PHP fastcgi_pass shows you haven't really read or understood appropriate tutorials, or you haven't included your full config file. – Tim Jan 19 '17 at 20:22
0

This is all you need.

    server {
            listen 80;
            root /var/www/wordpress;
            server_name example.com www.example.com;
            index index.php; 

    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

    location ~* \.(css|gif|ico|jpeg|jpg|js|png|woff|woff2|ttf|ttc|otf|eot)$ {
            expires max;
            log_not_found off;
        }


        location / {
            #try_files $uri $uri/ =404;
            try_files $uri $uri/ /index.php$is_args$args;
        }


     location /wordpress2 {
      root /var/www/wordpress/wordpress2;
      index index.php;
      try_files $uri $uri/ =404;

      location ~ /wordpress2 /(.+\.php)$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
     }

     location /htmlsite {
      root /var/www/wordpress/htmlsite;
      index index.php;
      try_files $uri $uri/ =404;

      location ~ /htmlsite /(.+\.php)$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
     }
 }
Don Dilanga
  • 242
  • 3
  • 8
  • sorry I'm confused. www.example.com is working but I see your path include extra folder - do I need to include? /var/www/wordpress/wordpress2 or should it be /var/www/wordpress2 – Charles Jan 19 '17 at 20:07
  • I assumed the "wordpress2" folder is in "wordpress" folder, if that's the case then leave it like that, otherwise use the respective path instead of the one I posted. :) as an example if the wordpress2 folder is in /var/www/wordpress2 then use the root as. location /wordpress2 { root /var/www/wordpress2; – Don Dilanga Jan 19 '17 at 20:14
  • This doesn't work, because nginx appends the URI to the location specified in `root` directive. That is, in this case if one requests `http://www.example.com/wordpress2/example.html`, nginx will try to show file `/var/www/wordpress/wordpress2/wordpress2/example.html`. – Tero Kilkanen Jan 19 '17 at 20:17
  • It works like this. when you visited the example.com/wordpress2 the respective block which is the "location /wordpress2 {" is called, and then its root path is used as the reference to look for the files to be called. since I use index index.php, the index.php is called in default. – Don Dilanga Jan 19 '17 at 20:20
  • The config for location /htmlsite failed when I use nginx -t (invalid number of arguments) – Charles Jan 19 '17 at 20:20
  • can you try it again. I think you might have not copied the last curly bracket which was out of the code block. – Don Dilanga Jan 19 '17 at 20:22
  • Did you test it also for static files inside `/wordpress2`? – Tero Kilkanen Jan 19 '17 at 20:26
  • yeah it works for both dynamic (PHP), and static contents fine. – Don Dilanga Jan 19 '17 at 20:30
  • I copy and pasted [http://imgur.com/a/Gkp3M] from your code and it does not pass `nginx -t` but once I remove that part test pass. – Charles Jan 19 '17 at 20:46
  • does at least example.com/wordpress2 work? – Don Dilanga Jan 19 '17 at 20:57
  • you have to use server_name example.com www.example.com; for www version to work. – Don Dilanga Jan 19 '17 at 21:07