nginx配置文件分析

来源:互联网 发布:java 获取绝对路径 编辑:程序博客网 时间:2024/05/21 14:45

为什么我们使用Nginx而不是Apache?

Apache和Nginx都有能力提供每秒钟庞大的请求服务,但是随着并发数量的增加,Apache的性能开始下降,然而Nginx的性能几乎不会下降。

ngnix提供网页的速度比apache快50%,处理请求的个数是Apache的2~3倍!

这里最好的一点是:因为Nginx是基于事件的,它不用为每个请求产生新的进程或线程,所以它的内存使用很低。

详情见:http://www.cnblogs.com/zhwl/p/3338647.html


以下是ngnix的配置文件


#启动进程数,即启动ngnix服务的个数,通常设置和cpu的数量相等。

worker_processes  1;

#全局错误日志及PID文件
error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;

#nginx进程ID
pid        logs/nginx.pid;


events {
        #单个后台worker process进程的最大并发链接数
    # 并发总数是 worker_processes 和 worker_connections 的乘积
    # 即 max_clients = worker_processes * worker_connections
    worker_connections  1024;
    
}


http {
    include       mime.types;   #设定mime类型,类型由mime.type文件定义
    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 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,
        #功能是优化文件拷贝,提高web sever性能
    #对于普通应用,必须设为 on,
    #如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,
    #以平衡磁盘与网络I/O处理速度,降低系统的uptime.
    
    sendfile        on;
    
    #tcp_nopush     on;

    #keepalive 维持http连接时间
    keepalive_timeout  65;

        #默认情况下,gzip压缩是关闭的,该功能是面向客户端的, gzip压缩功能是减少带宽,但会增加服务器CPU的开销,Nginx默认只对text/html进行压缩 ,如果要对html之外的内容进行压缩传输,我们需要手动来调。
    #gzip  on;

        #负载均衡server配置
        upstream myapp {
            server 192.168.1.101:8086;
            server 192.168.1.101:8087;
        }
        
        #配置虚拟主机
    server {
        listen       10013; #虚拟主机端口
        server_name  localhost; #虚拟主机域名

        charset utf-8; #字符编码

        access_log  logs/host.access.log  main;#访问日志

                #代理
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass  http://myapp; #代理地址,指向上面的myapp
        }

        #404错误提示页
        error_page  404              /404.html;

                #服务端错误提示页        
                error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

                ########以下是配置php代理############
        # 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;
        #}
    }

    #一个ngxin可以配置多个虚拟主机,配置同上
    # 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##############
    # 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;
    #    }
    #}

}

0 0