1

I am currently setting up our new server for our company, and we are developing alot of different apps, and right now we want to have all our wordpress apps under wp.domain.com/app1, wp.domain.com/app2, etc.

The reason the default isn't working is because we want to use the %postname% permalink for all apps.

I am currently managing by doing a rewrite for every single sub-folder, but I'd rather rewrite every single subfolder with one location block, so I don't have to edit the server block each time we upload a new application to our server.

There is also a possibility we upload to a sub-sub-directory, for example wp.domain.com/appgroup1/app3, wp.domain.com/appgroup1/app6, etc.

Here's my wp.domain.com config:

server {
  listen 80;

  root /usr/share/nginx/html/wp;
  index index.php index.html index.htm;
  server_name wp.domain.com;

  location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    try_files $uri $uri/ =404;
  }

  location ~ /(.*)/ {
    index index.php;
    try_files $uri $uri/ /$1/index.php?$args;
  }

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  #
  location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

    # With php5-cgi alone:
    #fastcgi_pass 127.0.0.1:9000;
    # With php5-fpm:
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
  }

  # deny access to .htaccess files, if Apache's document root
  # concurs with nginx's one

  location ~ /\.ht {
    deny all;
  }
}

Note that we don't have use a multi-site setup.

4
  • Post your complete nginx configuration in the question please. Commented Oct 16, 2014 at 12:43
  • Done, see edit.
    – Sven
    Commented Oct 16, 2014 at 13:07
  • This is going to be really messy. You should save yourself a lot of trouble, and just use subdomains. Commented Oct 16, 2014 at 13:22
  • So it isn't possible to apply an equivalent of Apache's rewrite on every sub-folder there is? I can live with us not putting them into "sub-sub-folders", but it must be possible to add a simple rewrite for %postname% permalinks in each top-level sub-directory, right?
    – Sven
    Commented Oct 16, 2014 at 13:40

2 Answers 2

1

This is time to use regex location blocks :

location ~ /(app1|app2|app3|groupapp1(?:/(subapp1|subapp2|subapp3)))/ { 
    try_files $uri $uri/ /$1/index.php$is_args$args;
    [ ... ]
}
9
  • This would still require me to edit the server block each time I add a app to our site and then restart nginx.
    – Sven
    Commented Oct 16, 2014 at 9:28
  • Then adapt the regex and capture everything if it's what you want using (.*). But it's likely to cause issues. Commented Oct 16, 2014 at 9:40
  • Tried with location ~ /(.*)/ { try_files $uri $uri/ /$1/index.php; index index.php; } but this downloads the index.php file for some reason.
    – Sven
    Commented Oct 16, 2014 at 9:47
  • @Svenskunganka Do you have a location block handling php files ? Commented Oct 16, 2014 at 9:49
  • Yes, the location /app/ { } works perfectly fine.
    – Sven
    Commented Oct 16, 2014 at 9:49
0

From what I see you can just left index index.php throughout the whole server {} block and remove any /app/ locations. You don't need to rewrite anything to index.php.

1
  • The thing I want is Wordpress's %postname% permalink on all the sites under wp.domain.com
    – Sven
    Commented Oct 16, 2014 at 9:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .