3

I have set up a server block on my nginx server, e.g. with domain testsite.com. I want to be able to install separate WordPress installations into direct child folders of the root folders, e.g. /var/www/html/testsite.com/childfolder1, /var/www/html/testsite.com/childfolder2 etc., so they can be reached by testsite.com/childfolder1, testsite.com/childfolder2 etc.

The manual way to create redirects would be to insert it like so:

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

and repeat it for every site to come. Using location / only covers the root directory. Is there a way to create a (regex?) wildcard rule that says: "For each direct sub directory, apply this try_files command" (which is obviously always the same for WordPress, just the folder names change)?

location /*anydirectsubdirectory* {
    index index.php;
    try_files $uri $uri/ /*anydirectsubdirectory*/index.php?$args;
}
cnst
  • 12,948
  • 7
  • 51
  • 75
physalis
  • 145
  • 1
  • 6
  • Isn't `location / { [...] }` sufficient? – gxx Jul 01 '16 at 01:02
  • Unfortunately, no. If I only use it that way it ignores the directive for all sub-directories. E.g. any index.php arguments in childfolder1, childfolder2 are being ignored, leading to 404. – physalis Jul 01 '16 at 01:04
  • Hm...could you please show the debug log of a request while using `location / [...]`? – gxx Jul 01 '16 at 01:07
  • "E.g. any index.php arguments in childfolder1, childfolder2 are being ignored, leading to 404." So the file is found, but the args aren't passed correctly? – gxx Jul 01 '16 at 01:13
  • Sorry, I have to revert. The `location /`directive is unfortunately not working at all, except for the root directory itself. One of the sites worked after I had used the explicit child directory for location, and after I changed it to `/` I obviously didn't reload nginx. Now none are working with the default directive. – physalis Jul 01 '16 at 01:48
  • See my answer on a related question http://serverfault.com/questions/586586/nginx-redirect-via-proxy-rewrite-and-preserve-url/586607#586607. Instead of `proxy_pass` and `proxy_set_header`, use `try_files` / `index` directives. The point there is using regular expression to capture a part of the URI into a variable and using that variable later in `try_files`. – Tero Kilkanen Jul 01 '16 at 07:34
  • Could you explain that a bit? What exactly should I set the location to if it should cover all (direct) sub-directories? /some/path is the variable part in my case :). – physalis Jul 01 '16 at 07:44

2 Answers2

4

I cannot attest whether what you want to do will work, but below is the conversion of your "pseudocode" into actual nginx configuration (and provided that a likewise copy-paste solution was working for you, this should continue working, too).

location ~ /(?<anydirectsubdirectory>[^/]+) {
    index index.php;
    try_files $uri $uri/ /$anydirectsubdirectory/index.php?$args;
}
cnst
  • 12,948
  • 7
  • 51
  • 75
  • Sorry for taking so long, but this is exactly what I needed. Now no matter what subdirectory, all links are working the way as intended. – physalis Jul 06 '16 at 21:18
  • Great, glad it works, thanks for accept! Also, I now +1'ed your question -- you now have a rep of 15, and are capable of doing +1 to other posts! Feel free to try on this one first. ;) – cnst Jul 06 '16 at 22:02
  • yes, thank you, already know that from the other stackoverflow network sites, and thus irrationally tried to +1 your answer before, hehe :). Thanks a bunch for your help! – physalis Jul 06 '16 at 22:53
  • @physalis, great, thanks! it's sad that so few users bother to even accept the answers to their own questions, let alone do the upvoting bit. – cnst Jul 06 '16 at 22:59
  • 1
    This place is really awesome - you can ask stupid questions, and if you behave and are clear enough, mostly get free (!) and friendly support. It’s only natural to show some little gratitude I believe :). – physalis Jul 07 '16 at 07:50
-1

TRY THIS~

server { listen 80; server_name example.com; charset utf-8; access_log logs/xxxxxx.access.log;

root   /var/www/html;

index index.php;
location = / { return 301 /cn/; }

location / {
    try_files $uri $uri/ /cn/index.php?q=$uri;
}
location /en {
    try_files $uri $uri/ /en/index.php?q=$uri;
}
location /my {
    try_files $uri $uri/ /my/index.php?q=$uri;
}
location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass   127.0.0.1:9000;
    include        fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
}

}

  • 1
    Hi @johnsongoey, can you elaborate a bit on what the above means? I don't have directories of the names /en and /my? Also, since I am using HHVM, the php directive is possibly not needed/usable for me, as fastcgi works through HHVM. – physalis Jul 01 '16 at 07:05