32

I'm using the official windows version of nginx. I want to specify the windows equivalent of this configuration

location /static/ {
   alias /home/user/staticfiles/;
}

How do I specify windows file paths in the alias directive? Is this even possible?

6 Answers 6

24

These answers must be out of date. Using nginx 1.3.8 absolute paths with forward slashes works. Backslashes seem to work, but should be doubled. If they aren't then some, such as a trailing \" are taken literally.

location /static/ {
    # alias "C:\\foo\\bar\\...\\static\\";
    alias "C:/foo/bar/.../static/";
    expires 90d;
}

The quotes may not be required, but they seem like a good idea in case of embedded spaces.

One other thing I noticed is that it is important to match the url and alias path regarding ending with a trailing slash or not--a mismatch and it doesn't work.

2
  • Still works in nginx 1.8.1
    – Daniel
    Apr 25, 2016 at 5:14
  • 1
    Your advice to use quotes to handle whitespaces helped me alot!
    – Beat
    Jun 19, 2018 at 13:28
5

If you try to specify an absolute path like...

location / {
    alias C:\Users\SomeUser\mysite\static;
}

...then upon requesting a file from that location, you'll probably see errors in C:\nginx\logs\error.log like:

2011/11/11 12:53:16 [error] 6236#0: *1 open() "/cygdrive/c/nginx/C:\Users\SomeUser\mysite\static\somefile.css

When configuring nginx on Windows, specify any paths relative to the C:\nginx directory. This works:

location / {
    alias ../Users/SomeUser/mysite/static;
}

Personally, I was happy to learn this because it makes my nginx configurations a little more portable between Windows and Linux than I had expected them to be. To turn a Linux configuration file into one that works on Windows, for me it's basically just:

s|/home/myname/|../Users/Myname|
1
  • This works with nginx 1.9.10, Thanks!
    – NenadP
    Feb 9, 2016 at 14:14
3

For NGINX on Windows you have to use this method:

location /foo {
   root C://pathtoyourfile/folder1/folder2;
}

Where foo is an actual folder inside folder2.

2

You can try this:

  1. copy your static files into nginx/html/staticfiles
  2. set into nginx.conf

    location /static/ { alias /nginx/html/staticfiles/; }

1

seems that alias doesn't work at all for my windows system. I tried to type

alias "c:"

but even this doesn't work. And I cannot find a way to output the location after the config file is reloaded by nginx. So simply do not use alias in windows for nginx.

2
  • For nginx 1.5.6, the following setup works: alias ../../cde/folder;, suppose your nginx.exe locates at d:/abc/nginx, but the folder you try to alias locates at d:/cde/folder. Some sources mention that nginx was hardcoded with the path /cygdrive/c/nginx/, in which case alias /cygdrive/d/path/ should be used, but this does not work for me. Also deserve to mention that the forward slash at the end of the alias path should not be neglected.
    – TonyTony
    Nov 15, 2013 at 5:55
  • I am trying to migrate nginx.conf from linux to win10. The nginx executables are in C:\nginx, whereas the served files are on D:/bellaria_web/haus. The context 'root D:/bellaria_web/haus/;' does not seem to work. Can somebody explain what is wrong?
    – aag
    Jan 12, 2016 at 20:42
1

In the default configuration on Windows, you can find the following code, showing the default directory html in the nginx installation:

        location / {
            root   html;
            index  index.html index.htm;
        }

So, it looks like everything placed in html can be configured like this:

        location /next-big-company/ {
            alias html/next-big-company;
        }

In my case, it worked.

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – djdomi
    Nov 1, 2023 at 13:42

You must log in to answer this question.

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