nginx初级的讲解

1,512 阅读3分钟

什么是nginx

nginx是一个高性能的http、反向代理的服务器.

场景

  • 反向代理
  • 解决跨域
  • 静态资源
  • api服务

优点

  • 高并发,高性能
  • 可扩展性好
  • 高可靠性
  • 热部署
  • BSD许可证

代理

正向代理

客户端配置一个代理服务器,通过这个代理服务器发送请求到目标服务器;然后从目标服务器得到响应内容转发给客户端的过程.

tip:客户端是知道有个代理服务器.

反向代理

客户端发送请求到反向代理服务器,反向代理服务器在转发请求到目标服务器;然后反向代理服务器从目标服务器得到响应内容转发给客户端的过程.

tip:客户端并不知道有反向代理服务器,认为反向代理服务器就是目标服务器,这样可以隐藏真实的服务器

负载均衡(采用的就是反向代理实现)

原理

有一个服务器集群,当用户访问网站时,先访问一个反向代理服务器,再让这个反向代理服务器在集群中选择一个压力较小的服务器,然后该访问请求引入该服务器.这样每个服务器压力都会趋于平衡分担了服务器压力,避免服务器崩溃.

场景

处理高并发

策略

轮询策略

每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,则自动剔除

weight

代表权重,默认为1,权重越高被分配的客户越多

ip_hash

每个请求按访问的ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题

fair(第三方)

按后端服务器的响应时间来分配请求,响应时间短的优先分配

动静分离

把动态资源与静态资源由不同的服务器来解析,加速解析速度,降低原来单个服务器的压力

nginx.conf的详细讲解

有三大模块

  • 全局块
  • event
  • http

全局块

从配置文件开始到events块之间的内容,主要设置一些影响nginx服务器整体运行的配置指令

event

主要影响nginx服务器与用户的网络连接

http

#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; #配置nginx支持哪些多媒体类型 
    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日志以及存放路径,并使用上面定义的main日志格式
    #access_log  logs/access.log  main;

    sendfile        on; # 开启高效文件传输模式
    #tcp_nopush     on; # 防止网络阻塞

    #keepalive_timeout  0;
    keepalive_timeout  65; # 长连接超时时间,单位秒

    #gzip  on; #开启gzip压缩输出

    # upstream  aaa{
    #   server 192.168.50.226:90;
    # } 

    #配置虚拟机
    server {
        listen       90;  #配置监听端口
        server_name  localhost; #配置服务名

        #charset koi8-r;   #配置字符集

        #access_log  logs/host.access.log  main; #配置本虚拟主机的访问日志
        # 模块用于分发请求的服务器组

        #默认的/请求,当访问路径中/,会被该location匹配到并且进行处理 http://192.168.50.226/ 处理
        location /public {
            alias  static; 
            autoindex on;
        }
        location / {
            root   html; #root是配置服务器的默认网站根目录位置,默认为nginx安装主目录下的html目录
            # index  index.html index.htm; #配置首页文件的名称
            index  ./example/demo.html ; #配置首页文件的名称
        }
        
        # 进行了跨域代理 

        location /cache {
           proxy_pass http://192.168.50.226:8888;
        }



        #配置404页面
        #error_page  404              /404.html;
        
        # 如果有重复则重定向到静态页面50x.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;
    #    }
    #}
}