nginx的使用介绍

什么是nginx?

Nginx是一款轻量级Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好

为什么使用nginx

在传统的Web项目中,并发量小,用户使用的少。

所以在低并发的情况下,用户可以直接访问tomcat服务器,然后tomcat服务器返回消息给用户。

但是随着公司客户的增加,并发量 也会越来越大,这个时候为了防止服务器在并发量量过大的时候崩溃,我们就需要多台服务器来增加我们的高并发处理能力;这个时候我们就需要对我们的服务器做负载均衡,让用户传递过来的请求,根据每台服务器的性能进行加权轮询;这个时候我们就需要一个处于客户端与服务器的中间件来做这个负载均衡;

nginx的特点

高并发、高性能

可扩展性好

高可靠性

热部署

BSD许可证

nginx如何配置

nginx的配置就比较简单,首先从官网下载一个nginx,nginx的文件特别小,只有1M左右

nginx: download

nginx支持跨平台,既可以再windows 上使用,也可以在linux系统上边使用;

下载之后解压后的目录结构如下;

主要的配置就在conf下的nginx.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的全局配置
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;
  //配置流
    upstream test{
       server  ip和端口  权重 weight=5;
       server ip和端口   权重 weight=1;
    } 
    server {
        listen       82;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
//代理对象
            proxy_pass http://test;
        }

        #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会去监听我们的82端口,当用户访问我们的82端口的时候,我们的nginx会将请求发送到我们的服务器上进行轮询,我们在指定服务端口后边配置上权重,nginx机会根据权重去做加权轮询,进而就可以为我们的服务器提高处理高并发的性能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

灰灰的996

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值