0

I have a running nginx server with php5-fpm on a debian wheezy. I want /var/www/devel/ to be my development folder served at http://devel.example.com. In this directory I have multiple projects like /proj-1/ /proj-2/ /proj-2/ etc. - some of them need to use Wordpress.

Now with every folder containing Wordpress I realised I have to add a separate location (marked as ##SPE-LOC## in the following config) to enable rewrite aka pretty permalinks.

The config works, but what bothers me is, that I have to add the separate location for all the folders with Wordpress in them. How can I avoid those repeating blocks?

My nginx config:

server {
    listen 80;
    server_name devel.example.com;

    root /var/www/devel;
    index index.html index.php;

    # Default location
    location / {
        #try_files $uri $uri/ =404;
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    ##SPEC-LOC##
    location /proj-1 {
        try_files $uri $uri/ /proj-1/index.php?q=$uri&$args;
    }

    ##SPEC-LOC##
    location /proj-3/blog {
        try_files $uri $uri/ /proj-3/blog/index.php?q=$uri&$args;
    }

    # PHP
    location ~ \.php$ {
        try_files               $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass            unix:/var/run/php5-fpm.sock;
        fastcgi_index           index.php;
        fastcgi_param           SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include                 fastcgi_params;
    }

    # deny access to .htaccess files
    location ~ /\.ht {
            deny all;
    }
}

2 Answers 2

0

You can't. Sorry.

But if what you seek is to add scalability, i.e. to be able to add new projects without editing the nginx config, then you can implement a slightly different approach.

Create a folder in the same directory with your nginx config. For the sake of this example, I'll take the name project_configs. My nginx.conf is located in /usr/local/etc/nginx/nginx.conf so I'd just create /usr/local/etc/nginx/project_configs.

Instead of repeating location blocks, you can just add include project_configs/*.conf to your nginx config, and create a new file in project_configs for each project and write the location block in it.

Your nginx config should look like this:

server {
    listen 80;
    server_name devel.example.com;

    root /var/www/devel;
    index index.html index.php;

    # Default location
    location / {
        #try_files $uri $uri/ =404;
        try_files $uri $uri/ /index.php?q=$uri&$args;
    }

    #Include all of the project configs.
    include project_configs/*.conf;

    # PHP
    location ~ \.php$ {
        try_files               $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass            unix:/var/run/php5-fpm.sock;
        fastcgi_index           index.php;
        fastcgi_param           SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include                 fastcgi_params;
    }

    # deny access to .htaccess files
    location ~ /\.ht {
            deny all;
    }
}

And you can add as many configs as you want to project_configs. They should look like this:

proj-1.conf

location /proj-1 {
    try_files $uri $uri/ /proj-1/index.php?q=$uri&$args;
}

proj-3.conf

location /proj-3/blog {
    try_files $uri $uri/ /proj-3/blog/index.php?q=$uri&$args;
}

This way, you can add as many projects as you want without having to modify the nginx config, and without having a cluttered config.

Hope that helps.

2
  • Outsourcing the projects into extra config files is a nice idea to keep those out of the main config. Sadly not the 'true' answer but a nice idea for uncluttering :) Jan 21, 2015 at 8:13
  • This is what I usually do with similar situations. I will follow this question, I wonder if there's a better solution :)
    – Bugra Koc
    Jan 21, 2015 at 11:39
0

This may not be the cleanest or wisest solution but I would expect the following to work given your listed use cases...

# Default location
location / {
    try_files $uri $uri/ $uri/index.php?q=$uri&$args;
}

Depending on your implementation you MAY need an extra

/index.php?q=$uri&$args

The downside is that it will likely break any projects that don't use Wordpress's query string to make pretty URls.

1
  • If it's going to fail on other projects without wordpress than it is not what I seek - but it is a good idea for a folder with a lot of Wordpress projects in it :) Thanks for the idea! Jan 21, 2015 at 8:09

You must log in to answer this question.

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