1

i got this working nginx config:

server {
  listen 80;

  server_name mydomain.com;
  root /var/www/mydomain/wordpress;

  index index.html index.php;

  location /customer1 {
    alias /var/www/mydomain/customers/custumer1;
  }

  location /customer2 {
    alias /var/www/mydomain/customers/custumer2;
  }
...
  location /customerN {
    alias /var/www/mydomain/customers/custumerN;
  }

where customer1...customerN are nicknames.

the problem is that custmers are growing fast. so, is there a way to make this config more efficient? is there a way to create arrays maybe?

thanks!

2 Answers2

0

You can use this location block:

location ~ ^/customer(?<customerid>[0-9])$ {
    alias /var/www/mydomain/customers/customer$customerid;
}

Here we use a regular expression to capture the customer number into $customerid variable and then use the variable at alias statement.

Regular expressions are a very powerful method of matching different kind of text strings, so you can easily adapt this to cover for longer numbers etc.

Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
0

Your configuration looks... strange. First of all, alias in nginx, is not same as Alias in apache. http://nginx.org/docs/http/ngx_http_core_module.html#alias You should use root instead, if I undestand you right http://nginx.org/docs/http/ngx_http_core_module.html#root

If customer URI-s are like http://example.com/random_customer_name

Then you should something like that:

location / {
    root /var/www/mydomain/customers;
    try_files $uri $uri/ @php;
}

location @php {
    root /var/www/mydomain/wordpress;
    # I think you need some proxying to apache or php-pfm here
}

But it have some obvious issues, like user with names wp-login, wp-admin and so on. So it is better to have URI like http://example.com/users/random_customer_name And config will be as simple as that:

location /users {
    root /var/www/mydomain/customers;
}
Paul
  • 3,037
  • 6
  • 27
  • 40
Hardy Rust
  • 162
  • 4
  • based on your coments, you think it would be better to work with subdomains. like: htttp://customername.example.com ? – vladimir prieto Oct 01 '16 at 19:12
  • That would be safer, but you will have to either create DNS-records and virtualhosts dynamically, or use wildcard subdomains. – Hardy Rust Oct 01 '16 at 19:15