30

I'm trying to set up Nginx on my Windows development environment. I can't find how to create something similar to "sites-enabled" on Linux where Nginx would look for (links to) active virtual host configurations.

Is there a way to do something similar with a directory with shortcuts to the actual configuration files and Nginx scanning that directory? Or is there another way to hook up a virtual host configuration other than copying the host configuration to nginx.conf?

Akshay barahate
  • 649
  • 8
  • 23
Marc
  • 6,773
  • 10
  • 44
  • 68

5 Answers5

38

In windows you have to give full path of the directory where the config files are located. There are two files to update: nginx.conf, which tells nginx where to find web sites, and localhost.conf, which is the configuration for a web site.

It is assumed that nginx is installed in C:\nginx. If the installation directory is at another path, you will have to update that path accordingly, wherever it appears in the following two configuration files.

nginx.conf

Location: C:\nginx\conf

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    #to read external configuration.
    include "C:/nginx/conf/sites-enabled/*.conf";
}

localhost.conf

Location: C:\nginx\conf\sites-enabled

server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}
Anish B.
  • 9,111
  • 3
  • 21
  • 41
Umanda
  • 4,737
  • 3
  • 23
  • 28
19

The "sites-enabled" approach as used by some Linux packages of nginx utilize include directive, which understands shell wildcards, see http://nginx.org/r/include. You may use it in your own config as well, e.g.

http {
    ...
    include /path/to/sites/*.conf;
}

Note though that such approach might be very confusing (in particular, it would be hard to tell which server{} is the default one unless you use default_server explicitly).

Maxim Dounin
  • 6,365
  • 1
  • 26
  • 27
  • None of these work for me: `include f:\code\mysite\dev-ops\nginx\dev\mysite.conf;` `include "f:\code\mysite\dev-ops\nginx\dev\mysite.conf";` `include f:/code/mysite/dev-ops/nginx/dev/mysite.conf;` – Ryan May 01 '14 at 17:20
  • 1
    @Ryan escape your backslashes. – Greg Aug 06 '18 at 09:17
  • @Greg Thanks for the tip! – Ryan Aug 06 '18 at 12:20
  • I am trying to include a custom config like: include "C:\\nginx-1.19.6\\conf\\conf.d\\*.conf"; But its not working – Sahil Dave Jan 05 '21 at 12:23
4

The following worked for me but only AFTER I moved my main nginx exe folder from c:/Program Files (x86)/nginx-1.7.0 to c:/nginx-1.7.0 (because I think it doesn't handle spaces in file paths well):

http {
    ...
    include "f:/code/mysite/dev-ops/nginx/dev/mysite.conf";
}
Ryan
  • 22,332
  • 31
  • 176
  • 357
2

Till the time of writing this answer (7 Jan 2022) non of the other answers fully answer this question.
Wildcards (include a/*.b) just include a list of vhosts which cannot be disabled/enabled. sites-enabled and sites-available is about being able to disable a vhost without deleting the corresponding config file.
Nginx has only one config file (nginx.conf), which in turn includes other files. The ability to include files is what led to enabled/available design. So the directory structure goes as follows:

conf // or whatever
|__nginx.conf
|__sites-enabled
|____default // symlink to sites-available/default.conf
|__sites-available
|____default.conf // You can omit the extension but I just like it
|____whatever.conf
|____some vhost.conf
|____another vhost.conf
|____disabled vhost.conf
|____other config files ...
# nginx.conf
http {
  # ...
  include path/to/sites-enabled/*; # include the enabled ones
}

In windows (cmd) you do:

mklink Link Target
# for example
mklink default X:/path/to/nginx/conf/sites-available/default.conf

Many think that windows doesn't have symlinks, it does :-)

I use a slightly more complex config directory structure, for development:

conf/
|__nginx.conf
|__sites-enabled/
|____some-site // sites-available/some-site/{env} where {env} is either dev or prod
|__sites-available/
|____some-site/
|______prod.conf
|______dev.conf
|______dev/
|________www.conf // vhost (server {}) for the www subdomain
|________api.conf // same as above but for the api subdomain
|________root.conf // vhost for the top level domain (e.g example.com without any subdomain prefix)
|______prod/
|________www.conf
|________api.conf
|________root.conf
|______snippets/
|________http.conf  // listen on ipv4 80/ipv6 80 and redirect http to https
|________https.conf // listen on ipv4 443 ssl/ipv6 443 ssl and `include`s ssl.conf
|________ssl.conf  // ssl config, pay attention to permission
Ahmed Shaqanbi
  • 525
  • 5
  • 15
0

You can include your config with a relative path in your nginx.config (the relative path is just the path of the config file itself in contrast to the logs path for example):

http {
  include       mime.types;
  default_type  application/octet-stream;
  include       ../sites-enabled/*.conf;
  ...
}
Flow
  • 29
  • 4