← 回到文章

在windows7下配置nginx的过程和一些问题

最近心血来潮,在windows下使用nginx做为服务器进行开发,由于对这东西十分陌生,所以出了一些问题。 配置过程十分简单,但是对于我这样的新手来说,貌似比较辛苦。 简单来说: 1、到nginx for windows由第三方编译的nginx Windows 版本下载 2、下载非安装版的php 3、将下载下来的nginx-0.7.59.zip包解压到你想到的位置(文件夹路径不能包含中文名) 4、直接运行nginx.exe,在浏览器中输入http://localhost,可以看到nginx的欢迎页面 5、配置nginx 我的配置如下:
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  185;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   D:/project;#网站根目录 需要与下面的fastcgi_param 对应
            index  index.html index.htm index.php;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
        #    root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  D:/project$fastcgi_script_name; #这个需要注意
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

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


    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
    #    ssl_prefer_server_ciphers   on;

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

}
6、使用RunHiddenConsole 隐藏php-cgi.exe的命令行,写一个批处理如下:
@echo off
echo Starting PHP FastCGI...
RunHiddenConsole D:\work\php\php-cgi.exe -b 127.0.0.1:9000 -c  D:\work\php\php.ini
7、关闭批处理
@echo off
echo Stopping nginx...
taskkill /F /IM nginx.exe > nul
echo Stopping PHP FastCGI...
taskkill /F /IM php-cgi.exe > nul
exit
其它可以参考 http://hi.baidu.com/rokaye/blog/item/3a44f6cbaf45dc19be09e6fd.html 【其它问题】 1、nginx网站根目录设置问题 fastcgi_param 和root 需要对应 2、The page you are looking for is temporarily unavailable. 打开某些页面时可能会出现The page you are looking for is temporarily unavailable.问题,在错误日志中可以看到 upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET 解决方案:修改 keepalive_timeout 185; 【补充说明】 在 Windows 下自动安装 Nginx 的项目:Farseer 这是淘宝 UED 部门的 明城 捣鼓的一个自动安装工具工具。地址:http://code.google.com/p/gracecode/wiki/Farseer
最后更新于 2009-11-24 18:52:23