Windows下Nginx初入门

来源:互联网 发布:手机去蓝光软件 编辑:程序博客网 时间:2024/05/22 15:45

下载

目前(2016.5.17),nginx的稳定版本是1.10.0,在官网下载先,http://nginx.org/en/download,建议使用stable version版

这是一个zip文件,解压后即可使用

 

启动

绿色文件,无须安装,直接即可启动。

据我所知,3种启动途径,其实都类似:

一、双击nginx.exe图标,可见黑窗口一闪而过,启动完毕。

二、命令行到nginx目录,输入nginx启动。(注,此方式命令行窗口无任何提示,且被锁定)

三、命令行到nginx目录,输入start nginx启动,此方式不锁定

 

启动后,默认情况下(无修改配置),可见到有两个nginx的进程,一个是master process,一个是worker processes。

 

测试

默认nginx部署了些静态内容,我们可通过它测试nginx是否在工作。

默认的配置文件(NGINX_HOME/conf/nginx.conf)如下:

#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  65;


    #gzip  on;


    server {
        listen       80;
        server_name  localhost;


        #charset koi8-r;


        #access_log  logs/host.access.log  main;


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


        #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  /scripts$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 ssl;
    #    server_name  localhost;


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


    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;


    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;


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


}

 

通过观察配置文件的非注释项(参考Nginx配置文件nginx.conf中文详解),大概可知:

1、启动了1个worker processes

2、worker_connections,最大并发数为1024

3、include mime.types,引入mime.types文件所声明的文件扩展名与文件类型映射

4、application/octet-stream,默认使用application/octet-stream

5、sendfile,开启高效文件传输模式

6、监听本机“localhost”的80端口

7、映射目录为“当前目录的html目录”

8、出现500、502、503、504错误,则映射到50x.html

 

浏览地址http://localhost,即可访问其默认页面,即映射到NGINX_HOME/html/index.html

其他静态内容,如html、图片,可自行添加测试。

 

日志

日志默认位于NGINX_HOME/logs/,可见:

1、access.log,访问日志

2、error.log,异常日志

3、nginx.pid,进程(仅在启动nginx后才有此日志)

常见错误

如果启动失败 可以看下logs目录下 error.log 文件里的错误信息。

我在第一次安装的时遇到两个错误,也是最容易碰到的问题,在这里列出来方便大家碰到相同的问题时快速解决。

1.  端口占用问题

我的配置文件里服务侦听的是 80 端口,由于机器上部署了IIS,80端口被默认站点占用,把站点关闭就可以了,这个问题在错误日志里记录是这样的。

 

2015/01/15 10:44:12 [emerg] 8800#5988: bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)

 

碰到类似的错误,请确认端口是否被占用或被防火墙屏蔽

 

2.Nginx所在目录有中文

错误日志大致输出一下内容

2015/01/15 11:55:55 [emerg] 5664#8528: CreateFile() "E:\软件\nginx-1.7.8/conf/nginx.conf" failed (1113: No mapping for the Unicode character exists in the target multi-byte code page)

3. 启用缓存时报错

2015/01/15 17:26:50 [emerg] 17068#20356: shared zone "cache_one" has no equal addresses: 02CF0000 vs 02A200002015/01/15 17:26:50 [alert] 11536#11228: worker process 17068 exited with code 1

我一直没有找到解决的方法,有人说重启服务,或者缓存设置大一点就可以了,我试了一下没有用的,官网原文是这样讲的,只能认为windwos下无解了。

: The cache and other modules which require shared memory support do : not work in Windows Vista and later due to address space layout : randomization being enabled in these Windows versions.

0 0
原创粉丝点击