Nginx windows下搭建过程

本文详细介绍了Nginx的安装步骤与配置方法,包括解决常见安装问题、配置虚拟主机及基本测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

内容列表:

  • 简要介绍
  • 下载安装
  • 配置测试

一、简要介绍

Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。

Nginx作为负载均衡服务器:Nginx 既可以在内部直接支持 Rails 和 PHP 程序对外进行服务,也可以支持作为 HTTP代理服务器对外进行服务。Nginx采用C进行编写,不论是系统资源开销还是CPU使用效率都比 Perlbal 要好很多。

  作为邮件代理服务器:Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last. fm 描述了成功并且美妙的使用经验。

  Nginx 是一个安装非常的简单,配置文件非常简洁(还能够支持perl语法),Bugs非常少的服务器:Nginx 启动特别容易,并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动。你还能够不间断服务的情况下进行软件版本的升级。

(更多介绍参照百科,更多使用参照GOOGLE,广而告之)

二、下载安装

我们使用nginx 1.0.4 for windows版本,下载解压后放到C:\nginx-1.0.4(官网这样要求的,不知道放其它盘有没有问题)

运行cmd.exe到


有错误啊,也不知道启动成功没有,去看看日志吧。


logs里有出现了两个日志文件access.log,error.log, 打开error.log开到下面提示

2011/06/16 15:23:55 [emerg] 7136#4040: 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)
2011/06/16 15:24:14 [emerg] 6336#7052: 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)

去nginx官网搜了一下原因:

1 - Is there any firewall activated (e.g. windows Firewall)?
2 - Is there any program using port 80 (e.g. Skype) ?
Before starting NGINX type in the prompt :
TELNET 127.0.0.1 80
(NOTE: there is a space between the 80)
If it is successfull then there a program using the port.
3 - Have you tried to configure another port? If so, same error happens?

3个原因:

1.防火墙打开了(确实我的WIN7 windows Firewall打开了,但是关了后还是不能启动nginx)

2.80端口被占用了(确实有个默认的站点用的80端口,关闭了后再启动nginx正常了,80后为Nginx默认的配置)

怎么看出来的正常的,看一下logs下面是不是多了一个nginx.pid文件,cmd窗口了输入

C:\nginx-1.0.4>tasklist /fi "imagename eq nginx.exe"
结果如下:

这时说明已经启成功了,有两个进程。按照官方的解释,nginx是守护进程,其中一个是一直处理事件等待中,当有事件被触发时,就启动另外一个进程处理相关请求。


访问成功后可以logs下的access.log看到访问站点的request信息

127.0.0.1 - - [16/Jun/2011:15:58:56 +0800] "GET / HTTP/1.1" 200 151 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1;
Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2;
 .NET4.0C; .NET4.0E)"

127.0.0.1 - - [16/Jun/2011:15:58:56 +0800] "GET /favicon.ico HTTP/1.1" 404 570 "-" "Mozilla/4.0 (compatible; MSIE 8.0;
Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0;
InfoPath.2; .NET4.0C; .NET4.0E)"

127.0.0.1 - - [16/Jun/2011:15:59:21 +0800] "GET /favicon.ico HTTP/1.1" 404 570 "-" "Mozilla/4.0 (compatible; MSIE 7.0;
Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0;
InfoPath.2; .NET4.0C; .NET4.0E)"

如果没有成功可以再查看logs下的error.log原因,以下为其它操作命令

nginx -s stopquick exit
nginx -s quitgraceful quit
nginx -s reloadchanging configuration, starting a new worker, quitting an old worker gracefully
nginx -s reopenreopening log files

官网提供了详细的windows usage 安装说明,网上大部分写的都很乱。我第一次参照网上其它人没成功过-_-|||,发现官网有安装说明照着操作一遍成功了,所以建议大家以后多看官网说明以及源码,可以少走很多弯路。

三、配置测试

第二环节我们使用了默认的nginx.conf 。Nginx的配置文件都存于目录conf文件下,其中nginx.conf是它的主配置文件。

以下为我加上注释并配置的新的虚拟server

<span style="font-size:12px;">#运行用户
#user  nobody;
#开启进程数 <=CPU数
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 {
    #Linux下打开提高性能
    #use epoll;
    #每个进程最大连接数(最大连接=连接数x进程数)
    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;
    
    #设定请求缓冲
    client_header_buffer_size 1k;
    large_client_header_buffers 4 4k;

    #打开发送文件
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    
    #客户端上传文件大小控制
    client_max_body_size 8m;
    
    #打开gzip压缩
    #gzip  on;
    
    #设定负载均衡的服务器列表
    #upstream mysvr {
    #    #weigth参数表示权值,权值越高被分配到的几率越大
    #    #本机上的Squid开启3128端口
    #    #server 192.168.8.1:3128 weight=5;
    #    #server 192.168.8.2:80 weight=1;
    #    #server 192.168.8.3:80 weight=6;
    #}

    #第一个虚拟主机
    server {
        #监听IP端口
        listen       80;
        #主机名
        server_name  localhost;
        #root  
        
        #设置字符集
        #charset koi8-r;
        #本虚拟server的访问日志 相当于局部变量
        #access_log  logs/host.access.log  main;
        #日志文件输出格式
        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';
        
        location / {
            root   html;
            index  index.html index.htm;
        }
        
        #静态文件缓存时间设置
        #location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${         
        #    expires 30d;
        #}
        
        #静态文件缓存时间设置
        #location ~ .*\.(js|css)?${         
        #    expires 1h;
        #}
        
        #对本server"/"启用负载均衡
        #location / {
        #    proxy_pass http://mysvr;
        #    proxy_redirect off;
        #    proxy_set_header Host $host;
        #    proxy_set_header X-Real-IP $remote_addr;
        #    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        #    client_max_body_size 10m;
        #    client_body_buffer_size 128k;
        #    proxy_connect_timeout 90;
        #    proxy_send_timeout 90;
        #    proxy_read_timeout 90;
        #    proxy_buffer_size 4k;
        #    proxy_buffers 4 32k;
        #    proxy_busy_buffers_size 64k;
        #    proxy_temp_file_write_size 64k;
        #}
        
        #设定查看Nginx状态的地址
        #location /NginxStatus {
        #    stub_status on;
        #    access_log on;
        #    auth_basic “NginxStatus”;
        #    auth_basic_user_file conf/htpasswd;
        #}



        #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       localhost:8666;
        #主机名
        server_name  LIULJ2576;
        #WEB文件路径
        root         E:/Portal;
        #默认首页
        index        HomePage.html;        
        #location / {
        #    #这里相当于局部变量
        #    root   E:/Portal;
        #    index  HomePage.html;
        #}
    }


    # HTTPS server HTTPS SSL加密服务器
    #
    #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;
    #    }
    #}

}</span>


#号为注释内容,我们在cmd下运行nginx


启动成功,出错的话,可以查询日志(日志路径是配置文件指定的,你可以修改存到其它位置)

访问一下第二个server 配置的localhost:8666地址,结果:


Nginx官网conf的配置没有提供太多的文档,相学习更多的配置,例如缓存,负载均衡等等,可以参照Nginx中文网

原文地址:http://www.cnblogs.com/mecity/archive/2011/06/17/2082786.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值