在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
我的配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#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的命令行,写一个批处理如下:

1
2
3
@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、关闭批处理

1
2
3
4
5
6
@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

在windows7下配置nginx的过程和一些问题》上有4条评论

  1. Pingback引用通告: 在windows7下配置nginx的过程和一些问题 « Gerry Blog

胖子进行回复 取消回复

电子邮件地址不会被公开。 必填项已用*标注


*

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>