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?



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.

  • but "custumer1" is a generic name. that was jus an example. real names are not based on IDs. – vladimir prieto Oct 1 '16 at 16:19
  • Then just modify the regex accordingly. – Tero Kilkanen Oct 1 '16 at 16:24
  • i've been thinking about it. but how can i achieve this when customer names are like nicknames, it could be anything! – vladimir prieto Oct 1 '16 at 18:11

Your configuration looks... strange. First of all, alias in nginx, is not same as Alias in apache. http://nginx.org/ru/docs/http/ngx_http_core_module.html#alias You should use root instead, if I undestand you right http://nginx.org/ru/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 obviuos 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;
  • based on your coments, you think it would be better to work with subdomains. like: htttp://customername.example.com ? – vladimir prieto Oct 1 '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 1 '16 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.