shuijibaobao

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
 

1. 配置文件分析

1. nginx 官网

  1. nginx 官网:http://nginx.org/en/
  2. nginx 文档:http://nginx.org/en/docs/
  3. nginx 官网(中文):http://nginx.p2hp.com/
  4. nginx 文档(中文):http://nginx.p2hp.com/en/docs/index.html
  5. 推荐看英文

2. 配置文件(带注释)


#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;
    #    }
    #}

}

3. 配置文件(去掉注释)

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

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

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

4. 讲解

4.1 nginx 配置文件有三部分组成

1. 第一部分:全局块

从配置文件开始到 events 块之间的内容,主要会设置一些影响nginx 服务器整体运行的配置指令,主要包括配 置运行 Nginx 服务器的用户(组)、允许生成的 worker process 数,进程 PID 存放路径、日志存放路径和类型以 及配置文件的引入等。
比如上面第一行配置的: worker_processes 1;

2. 第二部分:events块

events 块涉及的指令 主要影响 Nginx 服务器与用户的网络连接,常用的设置包括是否开启对多 work process 下的网络连接进行序列化,是否 允许同时接收多个网络连接,选取哪种事件驱动模型来处理连接请求,每个 word process 可以同时支持的最大连接数等。
上述例子就表示每个 work process 支持的最大连接数为 1024.
这部分的配置对 Nginx 的性能影响较大,在实际中应该灵活配置。

3. 第二部分:http块

这算是 Nginx 服务器配置中最频繁的部分,代理、缓存和日志定义等绝大多数功能和第三方模块的配置都在这里。
需要注意的是:http 块也可以包括 http全局块、server 块。

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

4.2 全局块

1.#user nobody; #配置用户或者组,默认为nobody nobody。
2. worker_processes 1;:允许生成的进程数,默认为1;这是 Nginx 服务器并发处理服务的关键配置,worker_processes 值越大,可以支持的并发处理量也越多,但是会受到硬件、软件等设备的制约。
nginx分为两类:master(管理者)和worker(工作者),这里就是配置工作进程的地方。worker 数和服务器的 cpu 数相等是最为适宜的。设少了会浪费 cpu,设多了会造成 cpu 频繁切换上下文带来的损耗

3.#error_log:2.2中默认注释掉的配置,制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
4.#pid:2.2中默认注释掉的配置,指定nginx进程运行文件存放地址

4.3 events块

1. worker_connections 204800;:没个工作进程的最大连接数量。根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行。每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为:worker_processes*worker_connections
2. accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
3. multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
4. #use epoll; #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport

4.4 http块

1. include mime.types;:#文件扩展名与文件类型映射表
2. default_type application/octet-stream;:#默认文件类型,默认为text/plain
3. #access_log off; #取消服务日志
4. #log_format main ...:#自定义格式 为 main

  1.#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  2.#                  '$status $body_bytes_sent "$http_referer" '
  3.#                  '"$http_user_agent" "$http_x_forwarded_for"';
5. #access_log logs/access.log main;:# main 为日志格式的默认值
6. sendfile on;:#允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
7. #tcp_nopush on;:
8. keepalive_timeout 65;:#连接超时时间,默认为65s,可以在http,server,location块。
9. upstream块:定义服务器组合 myTomcats。在 proxy_pass 指令的后面使用
    upstream myTomcats {
       server 192.168.0.100:8080;
       server 192.168.0.101:8080;
       server example.com:8080 backup;  #热备
    }
10. #gzip on;:
11. server块: 
    * keepalive_requests 120; #单连接请求上限次数。
    * listen 80;:#监听端口
    * server_name localhost; :#监听地址
    * #charset koi8-r;:
    * #access_log logs/host.access.log main;:
    * location块:#请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
        1.#root path; #根目录
        2.#index vv.txt; #设置默认页
        3.proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
        4.deny 127.0.0.1; #拒绝的ip
        5.allow 172.18.5.54; #允许的ip

location / {
            root   html;
            index  index.html index.htm;
        }
12. #error_page 404 /404.html;:#错误页

4.5 http块中location

1. 描述

location 中可用的匹配命令有两种:普通字符串和正则表达式。~ 和~* 用于正则表达式,其他前缀和无任何前缀都用于普通字符串。正则表达式会根据匹配顺序,匹配到第一个正则表达式后停止搜索。普通字符串匹配则无视顺序,只会选择最精确的匹配。常用的匹配命令和作用如下:

命令	|作用
|-------|------|
~|	表示执行一个正则匹配,区分大小写
~*|	表示执行一个正则匹配,不区分大小写
^~ |	表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配其他。一般用来匹配目录
= |	进行普通字符精确匹配
无前缀 |	用于普通字符串
@ |	定义一个命名的location,使用在内部定向时,例如error_page,try_files

2. 示例

  1. 只匹配“/”。
    location = /{

}
2. 匹配任何请求,所有URI都是以“/”开始;更长字符匹配或正则表达式匹配会优先。
location / {
}
3. 匹配以gif、jpg、jpeg结尾的请求;但是遇到^~由它处理 。
ocation ~* .(gif|jpg|jpeg)$ {
}
4. 以/index/开头的请求,如果链接的状态为404。则会匹配到@index_error。
location /index/ {

error_page 404 @index_error;
}
location @index_error {
… }

3. 更多详解

nginx.conf 配置文件中 location 代码块详解:https://blog.csdn.net/lch551218/article/details/104256019

2. 负载均衡篇

2.1 算法分析(共5个)

官网文档:http://nginx.org/en/docs/http/load_balancing.html

2.1.1 round-robin

轮询算法(默认),每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

http {
    upstream myapp1 {
        server srv1.example.com;
        server srv2.example.com;
        server srv3.example.com;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://myapp1;
        }
    }
}

2.1.2 least-connected

最少连接算法, — next request is assigned to the server with the least number of active connections,

upstream myapp1 {
    least_conn;
    server srv1.example.com;
    server srv2.example.com;
    server srv3.example.com;
}

2.1.3 ip-hash

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

upstream myapp1 {
    ip_hash;
    server srv1.example.com;
    server srv2.example.com;
    server srv3.example.com;
}

2.1.4 weight

指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。

    server srv1.example.com weight=3;
    server srv2.example.com;
    server srv3.example.com;
}

2.1.5 fair(第三方)

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

upstream backend {
server server1;
server server2;
fair;
}

2.1.6 url_hash(第三方)

按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法

upstream backend {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}

2.2 upstream块及参数详解

本文摘抄自博客: https://blog.csdn.net/qq_40036754/article/details/127775066

posted on   水吉z  阅读(29)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
 
点击右上角即可分享
微信分享提示