Nginx with multiple sites in subfolders

I would like to configure Nginx in such a way that I need a minimal amount of effort to add new sites.

I see it this way: when creating a new site, I put it in a subfolder under /var/www , add the new location to the nginx configuration file, which includes only the configuration template for the desired type of site. It might look like this:

 server { listen 80; server_name localhost; root /var/www; location /site1 { include drupal.conf; } location /site2 { include wordpress.conf; } } 

But unfortunately, this does not work in my case. The problem is nested locations . I have the following lines in one of the included templates:

 ... location /core/ { deny all; } location / { try_files $uri $uri/ @rewrite; } .... 

Nginx gives me the following errors:

location "/ core /" is outside the location "/ site1" in ...

location "/" is outside the location "/ site1" in ...

Therefore, I need to specify the full path for each site (for example, /site1/core/ ), but then I can not extract it as one part of reuse.

Earlier, as an alternative, I configured several server directives with different server_name ( site1.locahost , site2.localhost ...) and edited the /etc/hosts . In this case, I do not need nested places if everything was under the domain root. But, as I said, I’m looking for a way to simplify the workflow as much as possible, and editing /etc/hosts seems like an extra step that I’m avoiding.

So the question is, how best to deal with these situations? How to organize local work on different sites?

+6
source share
2 answers

At home and at work, I use a combination of Bind9 and Nginx to solve this problem. This requires some configuration, but after that you will no longer have to touch the nginx configuration file. I have added some restrictions below.

Customization

Setting up a DNS server, (Bind9, dnsmasq)

1) Set up a local DNS server and create a node called DEV 2) Create a name record in DEV for

 * A 127.0.0.1 

And restart the DNS server. 3) Make sure you can dig * .dev and make sure you return to 127.0.0.1.

Configure Nginx

1) In your nginx.conf or wherever you store your conf.d stuff, create a vHost entry that looks something like this: you can adapt it to your needs.

 server { listen 80; server_name *.dev; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; if ($host ~* ^(.*).dev$) { set $site $1; } if (!-d /var/www/$site/) { return 404; } location ~ index.php$ { fastcgi_split_path_info ^(.+\.php)(.*)$; fastcgi_pass backend; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/$site/$fastcgi_script_name; include fastcgi_params; fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_intercept_errors on; fastcgi_ignore_client_abort off; fastcgi_connect_timeout 60; fastcgi_send_timeout 180; fastcgi_read_timeout 180; fastcgi_buffer_size 128k; fastcgi_buffers 4 256k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; } location ~ / { try_files $uri $uri/ /index.php?$args; } } 

2) Restart the nginx service.

3) Profit


Once this is set up, to create a new site all you have to do is create a new folder in / var / www / .

 mkdir -p /var/www/sitename/ 

This site and the PHP under it can be accessed through sitename.dev.

As mentioned earlier, there are some limitations to this. sitename must be all lowercase and contain spaces or special characters (including periods). Secondly, it really only works for sites downloaded via index.php.

If you have radically different site structures, you can change a few things to give you a more reliable setting. For example, you can write your configuration so that it looks something like this.

 server { listen 80; server_name *.*.dev; [...] if ($host ~* ^(.*).(.*).dev$) { set $site $1; set $folder $2; } if (!-d /var/www/$folder/$site/) { return 404; } [...] fastcgi_param SCRIPT_FILENAME /var/www/$folder/$site/$fastcgi_script_name; [...] } 

And suppose you upgraded your DNS server to answer..dev, then you can write our directories as follows to give you an idea.

 /var/www/wordpress/site1 /var/www/wordpress/site2 /var/www/wordpress/site3 /var/www/zend/site1 /var/www/zend/site2 /var/www/zend/site3 

As I said, I use this setting at home and when working with +15 people. Our setup of work is a little more complicated (a common server, each has its own home folder), but it works fine. Personally, I prefer working on subdomains rather than localhost paths.

Hope this helps!

+5
source

How about using something like the alias function suggested by nginx?

http://wiki.nginx.org/HttpCoreModule#alias

If this does not work for your workflow, does symbolic binding to the / core directory prevent this error?

0
source

Source: https://habr.com/ru/post/949500/


All Articles