Nginx - Custom configuration files location - nginx

I use Nginx with many domains. Some of these domains have custom configurations. I'm including these files inside the server blocks in the Nginx configurations.
For example:
server {
... some configurations things here...
include /var/somewhere/custom.conf;
etc.. etc..
}
The configuration files of Nginx are inside: /etc/nginx
To try and keep everything in one place and not have my custom configuration files all over the place I would like to place my custom configuration files inside /etc/nginx/some_directory
Can I create a sub directory inside /etc/nginx without it causing any issues with Nginx itself? I want to create /etc/nginx/some_directory/ and place my many custom configuration files inside it and include them.
I'm specifically asking this question because I don't want to break something on my production server.

If nginx doesn't know about a directory, it'll not touch it. You can verify that by greping against such pattern in nginx's codebase.
However, messing with a foreign folder structure might cause problems with permissions and ownership of the files, therefore either just use a pre-defined folders nginx prepared for you (/etc/nginx/sites-enabled and /etc/nginx/sites-available) which you can use with symlinks such as nginx itself does
# ls /etc/nginx/sites-enabled
default -> /etc/nginx/sites-available/default
# ls /etc/nginx/sites-available
default
otherwise you're getting into a situation what C/C++ programmers call an undefined behavior and there's no guarantee that what works now will work in the future / nginx doesn't change as well as the distro maintainers might mess with the folder structure and permissions for the packages in distro package manager.
Example:
Nginx might verify the full /etc/nginx tree's permissions and owners - if your folders/files don't match it might cause a warning or crash even. If it's installed by a package manager, it might cause issues when removing the package itself e.g. if the package manager attempts to remove only a known list of folders + afterwards the parent i.e. /etc/nginx by rmdir or similar. Situations you don't really want to get into and debug when you can use allowed folders or symlinks or your own folders that are not bound to an application or behavior except the one you define.

Related

Nginx location and try_files in subdirectory

I'm trying to configure nginx vhost for application and stucked.
App is in directory /site/verb
At this moment app have this kind of links and it is working:
http://example.com/verb/lt.php?some_args=some_args&some_args?some_args
What I need? I need add another link for my clients like below:
http://example.com/v/lt.php?some_args=some_args&some_args?some_args
It is only change from /verb to /v but I want to handle both (for compatibility reasons) with all arguments after .php extension.
Is it possible in nginx config? (I want to avoid creating symlinks in directory).
I tried symlinks but it is not good solution.

nginx default configuration files

What does it mean when I see both "nginx.conf" and "nginx.conf.default" configuration files? I also see "fastcgi.conf" and "fastcgi.conf.default" and "fastcgi.params" and "fastcgi.params.default" etc, in the same folder.
Does ".conf.default" get applied and then the ".conf" gets applied on top of it?
Nginx will strictly load the configuration from the .conf files unless you haven't specified otherwise (like using an include rule and include a file with other extension that .conf).
.conf.default files are not applied on top of the .conf files. In fact they are not applied at all.
As far as I know this behaviour appears on debian when you do update nginx (but I might be wrong).

nginx: Looking to serve multiple subdirectories as their own root

Using Nginx, is there was way to run multiple subdirectories as their own self contained roots (document root)?
I am going to have several (40+) projects run in subdirectories:
frontend.example.com/project_1
frontend.example.com/project_2
frontend.example.com/project_3
Each projects are pushed to their own location /var/www/html/project_name. There will not be a site at frontend.example.com
Some projects due to the way they are set up need to run as if they are the root. They contain sources like <img src="/images">. The page is looking at front.example.com/images and not front.example.com/project_a/images.
Can I run each of these projects with the subdirectories as the root folder; stopping at front.example.com/project_name/?
What I've tried that hasn't worked
Creating separate sites-available / sites-enabled and config files. This was tedious since i would need to do this for every site and I kept running into issues. I halted this path to save myself from a rabbit hole, but I can provide those config files, if this is the preferred direction.
What has worked so far...
In sites-available/default having aliases. So having location /images/ look at alias /var/www/html/project_a/images/. This works, but I am hard coding the project_a, and with potentially a lot of separate project, this is not ideal.

Which nginx-config file is enabled, /etc/nginx/conf.d/default.conf or /etc/nginx/nginx.conf?

There are two config files around, /etc/nginx/conf.d/default.conf
and /etc/nginx/nginx.conf, but which one is enabled?
I am running CentOS6.4 and nginx/1.0.15.
Technically, nginx.conf is all that matters, if you define every thing inside there it would still work, but to keep things organized, they use include, somewhere at the end of nginx.conf you'll see include /etc/nginx/conf.d/* and in some distros you'll also find include /etc/nginx/sites-enabled/* this is a convention to keep things organized, you create your server blocks in that conf.d or sites-enabled folder and it would be included here as if it's written in the nginx.conf file.
Of course you can add your own include lines there normally and create your own new conf folder that would be auto included.
TIP: These files are included in alphabetical order, you need to keep that in mind if you don't specify any server as default_server, because first one would be the default.
the general configuration of nginx is in /etc/nginx/nginx.conf. /etc/nginx/conf.d/default.conf is used to configure the default virtual host. For this you can also use sites-available and sites-enabled.
You can find more details at a blog entry from digital ocean How To Configure The Nginx Web Server On a Virtual Private Server
for save time.
if you just have 1 site to host, nginx.conf is ok. but,
if you have 2~n sites, for more clear config, you should use conf.d fold.

Change the path of a symlinked directory in NGINX

Lets say that I have a directory /var/www/assets/ and within that directory I have a symlink which points to a folder which contains all the latest asset files for my website.
/var/www/assets/asssets -> /var/www/website/releases/xxxxxxx/public/assets
If I configure NGINX to serve asset files from /var/www/assets with the domain assetfilesdomain.com and asset files are prefixed with the directory /asset/ then when that /asset folder's symlink is changed then the updated link is not reflected in NGINX. The way that I see it, NGINX grabs the resolved path for that asset folder when it is started.
Is there any way to get around this?
Reloading nginx (sending a HUP signal to the master process) seems to solve this issue (probably because it starts new workers and shuts down the old ones, gracefully).
it seems like you're using Capistrano. You can override deploy:restart and put the nginx reloading there.

Resources