I have the following config files and locations:
etc/ngnix/nginix.conf
var/etc/nginx/sites-available/myproject
etc/ngnix/conf.d/default.conf
etc/ngnix/conf.d/web.conf
I'm confused regarding each conf file role, rules, when to use one or another, are they loaded one after another, or just one, are directives overwriting ?
The nginx configuration file is called nginx.conf and on most systems is located at etc/nginx/nginx.conf.
nginx.conf may optionally contain include statements to read parts of the configuration from other files. See this document for more. Read your nginx.conf file to identify which files and directories are sourced and in which context and which order.
Some distributions are delivered with an nginx.conf file that sources additional files from directories such as /conf.d/ and /sites-enabled/.
There is also a convention on some distributions to symlink files between /sites-available/ and /sites-enabled/.
The nginx -T command (uppercase T) is useful to list the entire configuration across all the included files.
Related
I'm struggling to figure out the meaning of each nginx configuration file located in different directories. So far I've found these directories/files related:
/etc/nginx/nginx.conf - this is the main file that nginx will read. It also has several include directives to reference configurations defined in several other files.
/etc/nginx/conf.d/*.conf - in my default installation, there's one default.conf file at this location. Is this the one that I should go to edit if I want to, for example, add my own website?
/etc/nginx/sites-available - Sometimes there's a file named 'default' here. What is this directory for and what's this configuration file? What's its relationship with the default.conf file under conf.d?
/etc/nginx/sites-enabled - Another confusion. What's this directory then? And on my installation I see a symbolic link file pointing to the 'default' file in the sites-available directory. What's the point of doing this, separating into 2 directories but link them by some means?
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.
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.
While I'm compiling NGINX, I get this message:
nginx path prefix: "/tmp/app"
nginx binary file: "/tmp/app/progs/nginx/sbin/nginx"
nginx configuration prefix: "/tmp/app/progs"
nginx configuration file: "/tmp/app/progs/nginx.conf"
Does NGINX use the path prefix or the configuration prefix for include directives in nginx.conf?
The documentation suggests that it's the "prefix path":
–prefix=path defines a directory that will keep server files. This same directory will also be used for all relative paths set by configure (except for paths to libraries sources) and in the nginx.conf configuration file. It is set to the /usr/local/nginx directory by default.
By contrast:
–conf-path=path sets the name of an nginx.conf configuration file. If needs be, NGINX can always be started with a different configuration file, by specifying it in the command-line parameter -c file. By default the file is named prefix/conf/nginx.conf.
However, this is a documentation bug, and your include paths will in fact be relative to the "config path".
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.