nginx学习笔记

nginx基本概念:

  1. nginx是什么?做什么事?

    Nginx (engine x) 是一个高性能的HTTP反向代理web服务器,其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好,连接高并发的情况下,Nginx是Apache服务不错的替代品,能够支持高达 50,000 个并发连接数的响应;

  2. 负载均衡

    Nginx通过反向代理实现负载均衡的,使用Nginx服务实现负载均衡的时候,用户的访问首先会访问到Nginx服务器,然后Nginx服务器再从服务器集群表中选择压力较小的服务器,然后将该访问请求引向该服务器。若服务器集群中的某个服务器崩溃,那么从待选服务器列表中将该服务器删除,也就是说一个服务器假如崩溃了,那么Nginx就肯定不会将访问请求引入该服务器了。

    负载均衡策略:

    轮询默认方式 在轮询中,如果服务器down掉了,会自动剔除该服务器。
    weight 权重方式 在轮询策略的基础上指定轮询的几率。
    ip_hash 依据ip分配方式 确保了相同的客户端的请求一直发送到相同的服务器,以保证session会话,可以解决session不能跨服务器的问题。
    least_conn 最少连接方式 把请求转发给连接数较少的后端服务器
    fair 响应时间方式 响应时间短的优先分配
    url_hash 依据URL分配方式 按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,要配合缓存命中来使用

     

  3. 反向代理

    正向代理:客户端非常明确要访问的服务器的地址,但由于安全等因素可能无法直接访问,此时有代理服务器代替客户端发送请求。

    反向代理:客户端发送了请求,但不知道哪个服务器最终会去解决这个请求,因为由nginx代理接受了,nginx根据你的配置文件、再将请求转交给真正的服务器、从而实现保障内安全、或者实现一些负载均衡的操作。

  4. 动静分离

    为了加快网站的解析速度,可以把动态页面和静态页面由不同的服务器来解析,加快解析速度,降低原来单个服务器的压力;

     

nginx安装、常用命令和配置文件

  1. 在win10系统中安装nginx

    下载nginx-1.18.0.zip压缩包,解压开直接用;

  2. nginx常用命令

    验证配置是否正确: nginx  -t
    查看Nginx的详细的版本号:nginx  -V
    查看Nginx的简洁版本号:nginx  -v
    启动Nginx:start  nginx
    快速停止或关闭Nginx:nginx   -s   stop
    正常停止或关闭Nginx:nginx   -s   quit
    配置文件修改重装载命令:nginx   -s  reload
  3. nginx配置文件

    nginx.conf:

    #user  nobody; # 全局快
    worker_processes  1; # 允许生成的进程数,默认为1
    ​
    #error_log  logs/error.log;         # 配置日志路径、日志级别
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    ​
    #pid        logs/nginx.pid;
    ​
    # events块
    events {
        worker_connections  1024; # 最大连接数
    }
    ​
    # http块
    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;
    ​
        # server块
        server {
            listen       80;        # 监听端口
            server_name  localhost; # 监听地址
            keepalive_requests 100; # 单连接请求上线次数
            #charset koi8-r;
    ​
            #access_log  logs/host.access.log  main;
    ​
            location / {
                root   html;
                index  index.html index.htm;
                deny 127.0.0.1;    # 拒绝的ip
                allow 172.18.5.54; # 允许的ip
            }
    ​
            #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;
        #    }
        #}
    ​
    }

     

    • 1、全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。

    • 2、events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。

    • 3、http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。

    • 4、server块:配置虚拟主机的相关参数,一个http中可以有多个server。

    • 5、location块:配置请求的路由,以及各种页面的处理情况。

       

nginx配置实例

  1. 反向代理实例

    # 反向代理实例一
    server {
        listen 80;
        server_name 127.0.0.1;
        charset utf-8;
        location / {
            root html;
            proxy_pass http://www.baidu.com;
            index index.html index.htm;
        }
    }
    ​
    # 反向代理实例二
    server {
        listen 8090;
        server_name 127.0.0.1;
        charset utf-8;
        location ~ /baidu/ {
            root html;
            proxy_pass https://www.baidu.com;
            index index.html index.htm;
        }
        location ~ /bili/ {
            root html;
            proxy_pass https://www.bilibili.com; #不能这么写:https://www.bilibili.com/;
            index index.html index.htm;
        }
    }

     

  2. 负载均衡实例

    # 负载均衡配置(放在http全局块)
        upstream myserver {
            ip_hash;
            server 127.0.0.1:8090 weight=1;
            server 127.0.0.1:10011 weight=1;
        }
    ​
    # 负载均衡实例
    server {
        listen 8081;
        server_name 127.0.0.1;
        charset utf-8;
        location / {
            root html;
            proxy_pass http://myserver;
            proxy_connect_timeout 80;
        }
    }

     

  3. 动静分离实例

    # 动静分离配置
    server {
        listen 8082;
        server_name 127.0.0.1;
        location ~ .*\.(jpg|png)?$ {
            root E:/Software/image/images;
            #index index.html index.htm;
        }
        location /data/ {
            root E:/Software/image/data;
            #index index.html index.htm;
        }
    }

     

  4. nginx配置高可用集群实例

     

nginx执行原理

 

posted @ 2021-03-07 11:19  尘世间迷茫的小书童  阅读(132)  评论(0编辑  收藏  举报