File Path of Nginx for Windows Under Cygwin / Ngin

Author: Robert Kwok at lc365 dot net
[阅读提示:中文在底部]
NOTE: This document is for Nginx compiled under Cygwin / GCC.

If your pages were put on Driver C, it's really easy to confige nginx.conf for visitors open your site just follow the examples in original conf/nginx.conf, well, for some reason, we usually did not put our pages on the Driver where Windows System Files lies on, Driver C.
Nginx for Windows Under Cygwin is different from Nginx for Unix-Like System.  Nginx was compiled by GCC under Cygwin, so he cannot recognize Windows Path (e.g. D:/www/) but Cygwin Path (e.g. /cygdrive/d/).
Now, I throw out some examples for your refference,

[NO FASTCGI]

We set D:\www\ for http://www.#yours.com, Driver D for example, other drivers are the same.

Bad:
location / {
            root  D:\www\;
            index  index.html index.htm;
        }

Good:
location / {
            root  /cygdrive/d/www/; # '/cygdrive/d/' equal to  'D:\' in Nginx under Cygwin
            index  index.html index.htm;
        }

If we want to set D:\www\newsB\ for http://www.#yours.com/newsA/, then, we'd better to use alias or rewrite directive of Nginx, something like

location /newsA/ {
            alias /cygdrive/d/www/newsB/;
        }
# Note:The alias directive cannot be used inside a regex-specified location. If you need to do this you must use a combination of rewrite and root.
As above, if we want another Driver's file, set F:\www2\ for http://www.#yours.com/vip/, then will be the following:
location /vip/ {
            alias /cygdrive/f/www2/; #  /cygdrive/f/ for Driver F
        }

[WITH FASTCGI]

As we see, Nginx under Cygwin can only know the Drivers Letter with the format "/cygdrive/d/", treat '/cygdrive/d/' as 'D:\', treat '/cygdrive/e/' as 'E:\', and so on, but if we use fastcgi with Nginx, things comes back. In nginx.conf we should use Cygwin paths (e.g. /cygdrive/d/www) for Nginx and native PHP paths (e.g. D:/www) for PHP scripts.
Sample:
    location ~ .*\.php$ { # Note that usually wrong as former ~ \.php$ , does not run as hoped
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  D:/www$fastcgi_script_name;
        # Here must use Windows native paths (D:/www) for PHP scripts, because
        # PHP is compiled natively and DO NOT know Cygwin paths (/cygdrive/d/www)
        include        fastcgi_params; # Please cut off SCRIPT_FILENAME line in file fastcgi_params
    }

=============================

由于Windows版本的Nginx其实是在Cygwin环境下编译的,所以Nginx使用的是Cygwin的路径格式,所以在Nginx的配置文件nginx.conf中,路径既不能使用*nix的格式,也不能使用Windows系统的格式,而要使用Cygwin的格式,即: C盘的C:\ 用/cygdrive/c/表示,D盘的D:\ 用/cygdrive/d/表示,以次类推。例如:

我们设置访问 http://www.#yours.com 时读取D:\www\
location / {
            root  /cygdrive/d/www/; # '/cygdrive/d/' = 'D:\' in Nginx under Cygwin
            index  index.html index.htm;
        }
如果我们需要在访问 http://www.#yours.com/newsA/ 时读取目录D:\www\newsB\,那么我们需要使用Nginx的alias或rewrite等指令,例如

location /newsA/ {
            alias /cygdrive/d/www/newsB/;
        }
如果我们需要设置访问http://www.#yours.com/vip/ 时读取另一个硬盘上的东西,比如F:\www2\ ,可以这样:
location /vip/ {
            alias /cygdrive/f/www2/; #  /cygdrive/f/ for Driver F
        }
另外需要注意的是,如果你使用了FASTCGI配置PHP,那么设置fastcgi_param  SCRIPT_FILENAME这行的时候,必须使用设置的Windows路径格式,因为这里的参数值是Nginx传递给PHP使用的,而PHP只能识别Windows格式,例:
    location ~ .*\.php$ { # 注意这里老版本用的“~ \.php$”好像有问题
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  D:/www$fastcgi_script_name;
        # 这里必须使用Windows格式路径(D:/www)
        include        fastcgi_params; # 注意fastcgi_params中去掉SCRIPT_FILENAME这行
    }


你可能感兴趣的:(PHP,nginx,windows,F#,gcc)