Nginx诞生的背景

  Nginx 和 Apache 都属于web 服务器,在Nginx 之前,Apache 以开源、稳定一直是世界上公认和用户最多的服务器。但Apache技术诞生比较久远,目前难以满足高并发的应用场景。Nginx 以轻量、高并发就应运而生了。

Nginx拥有的能力

反向代理

  • 正向代理
    •   譬如访问google网站,国内是访问不到的,此时可以找一个国外的代理服务器C,C可以访问google。 正向代理服务器C代理的是客户端的请求,转发给指定服务端。服务端不知道是哪个客户端发过来的请求,客户端和代理服务器在同一个LAN。
  • 反向代理
    •   目前互联网发展迅猛,单点服务远不能满足当下的服务请求。譬如国内的大型购物网站都是分布式部署,客户端发送购物请求,都会经过反向代理,均衡的转发客户端的请求到分布式的服务端。此时反向代理服务对客户端是透明的,即反向代理隐藏了服务端信息。客户端不知道访问的是后端N个服务中哪个服务的资源,反向代理服务器和后端服务器是同一个LAN

负载均衡

  • 权重轮询(默认)  
    •   轮询1..N台服务,其中有宕机的会主动剔除,按照权重分发请求,权重越高,分发的请求越多。
  • ip_hash
    •   经过hash算法,把指定IP和指定的服务端绑定,即指定IP的请求转发到指定的服务端。可以解决session共享的问题,保持有状态请求。
  • fair
    •   智能调度算法,处理时间长的分发的请求少,处理时间短的分发的请求多。Nginx默认不支持,需要安装upstream_fair模块。
  • url_hash
    •   和ip_hash原理一致,只是把指定url和指定服务器绑定。

动静分离

  针对静态资源,如图片、js、css等交由Nginx处理,而动态请求交由后端服务器来处理。根据请求url来做区分。

WEB缓存

  Nginx可以对不同的文件做不同的缓存处理,配置灵活,并且支持FastCGI_Cache,主要用于对FastCGI的动态程序进行缓存。配合着第三方的ngx_cache_purge,对制定的URL缓存内容可以的进行增删管理

Windows版本安装

官网地址

安装

  •   和其他windows的app一样,一路next 。我安装在D:\nginx-1.16.0

基本使用

Nginx 的文件结构

...              #全局块

events {         #events块
   ...
}

http      #http块
{
    ...   #http全局块
    server        #server块
    { 
        ...       #server全局块
        location [PATTERN]   #location块
        {
            ...
        }
        location [PATTERN] 
        {
            ...
        }
    }
    server
    {
      ...
    }
    ...     #http全局块
}
  • 1、全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。
  • 2、events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
  • 3、http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
  • 4、server块:配置虚拟主机的相关参数,一个http中可以有多个server。
  • 5、location块:配置请求的路由,以及各种页面的处理情况。

版本1.16 默认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;
    #    }
    #}

}
View Code

相关文章:

  • 2022-12-23
  • 2021-12-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-19
  • 2021-12-12
猜你喜欢
  • 2021-06-27
  • 2022-12-23
  • 2021-09-12
相关资源
相似解决方案