Nginx学习笔记<一>Nginx基础

什么是Nginx
Nginx是高性能的HTTP和反向代理的服务器,处理高并发能力是十分强大的,能经受高负载的考验,有报告代表能支持高达 50,000 个并发链接数php

Nginx做用
1.反向代理
a.正向代理
须要在客户端(浏览器)配置代理服务器,经过代理服务器进行互联网访问
8D2ZTSZ{ZKJG_}`57%U{14H.pnghtml

b.反向代理
其实客户端对代理是无感知的,由于客户端不须要任何配置就能够访问,咱们只 须要将请求发送到反向代理服务器,由反向代理服务器去选择目标服务器获取数据后,在返 回给客户端,此时反向代理服务器和目标服务器对外就是一个服务器,暴露的是代理服务器 地址,隐藏了真实服务器 IP 地址。
M)D)LO7W~OP0[7@TORWTJ5S.pngnginx

2.负载均衡
单个服务器解决不了,增长服务器的数量,而后将请求分发到各个服务器上,将原先请求集中到单个Nginx服务器上的 状况改成将请求分发到多个服务器上,将负载分发到不一样的服务器,也就是咱们所说的负载均衡
1KFFR~4T@6U]F{`WK%8Z{AM.png浏览器

3.动静分离
为了加快网站的解析速度,能够把动态页面和静态页面由不一样的服务器来解析,加快解析速度。下降原来单个服务器的压力。
JB[C4_YDLHEC$YMWXK2HY(7.png缓存

nginx经常使用命令
1.使用nginx命令前提条件:必须进入nginx录/usr/local/nginx/sbin
2.查看nginx版本号
./nginx -v
3.启动nginx
./nginx
4.关闭nginx
./nginx -s stop
5.从新加载nginx
./nginx -s reload服务器

nginx配置文件
vi /usr/local/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 {
    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  192.168.148.131;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /html/{
            root   /data/;
            index  index.html index.htm;
        }

        location /image/{
            root   /data/;
            autoindex on;
        }

        #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由三部分组成
1.全局块
从配置文件开始到 events 块之间的内容,主要会设置一些影响nginx 服务器总体运行的配置指令,主要包括配 置运行 Nginx 服务器的用户(组)、容许生成的 worker process 数,进程 PID 存放路径、日志存放路径和类型以 及配置文件的引入等。
好比上面第一行配置的session

worker_processes  1;

这是 Nginx 服务器并发处理服务的关键配置,worker_processes 值越大,能够支持的并发处理量也越多,可是 会受到硬件、软件等设备的制约
2.event块
如:并发

events {
    worker_connections  1024;
}

events 块涉及的指令主要影响 Nginx 服务器与用户的网络链接,经常使用的设置包括是否开启对多 work process 下的网络链接进行序列化,是否容许同时接收多个网络链接,选取哪一种事件驱动模型来处理链接请求,每一个 word process 能够同时支持的最大链接数等。
上述例子就表示每一个 work process 支持的最大链接数为 1024。app

3.http块

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    server {
        listen       80;
        server_name  192.168.148.131;
        location /html/{
            root   /data/;
            index  index.html index.htm;
        }
        
        location /image/{
            root   /data/;
            autoindex on;
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

这算是 Nginx 服务器配置中最频繁的部分,代理、缓存和日志定义等绝大多数功能和第三方模块的配置都在这里。须要注意的是:http 块也能够包括 http全局块、server 块a.http全局块http全局块配置的指令包括文件引入、MIME-TYPE 定义、日志自定义、链接超时时间、单连接请求数上限等。 b.server 块这块和虚拟主机有密切关系,虚拟主机从用户角度看,和一台独立的硬件主机是彻底同样的,该技术的产生是为了 节省互联网服务器硬件成本。 每一个 http 块能够包括多个 server 块,而每一个 server 块就至关于一个虚拟主机。而每一个 server 块也分为全局 server 块,以及能够同时包含多个 locaton 块。 <1> 全局 server 块 最多见的配置是本虚拟机主机的监听配置和本虚拟主机的名称或IP配置。 <2> location 块 一个 server 块能够配置多个 location 块。 这块的主要做用是基于 Nginx服务器接收到的请求字符串(例如 server_name/uri-string),对虚拟主机名称 (也能够是IP 别名)以外的字符串(例如 前面的 /uri-string)进行匹配,对特定的请求进行处理。地址定向、数据缓 存和应答控制等功能,还有许多第三方模块的配置也在这里进行

相关文章
相关标签/搜索