{"id":16407508,"url":"https://github.com/llitfkitfk/nginx-cookbook","last_synced_at":"2025-06-20T07:34:40.820Z","repository":{"id":68610838,"uuid":"525222433","full_name":"llitfkitfk/nginx-cookbook","owner":"llitfkitfk","description":"nginx使用手册","archived":false,"fork":false,"pushed_at":"2022-08-24T04:01:08.000Z","size":11,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-05T18:47:17.515Z","etag":null,"topics":["cookbook","nginx"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/llitfkitfk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-08-16T03:50:49.000Z","updated_at":"2022-08-24T09:30:50.000Z","dependencies_parsed_at":"2023-03-22T09:23:59.543Z","dependency_job_id":null,"html_url":"https://github.com/llitfkitfk/nginx-cookbook","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/llitfkitfk%2Fnginx-cookbook","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/llitfkitfk%2Fnginx-cookbook/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/llitfkitfk%2Fnginx-cookbook/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/llitfkitfk%2Fnginx-cookbook/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/llitfkitfk","download_url":"https://codeload.github.com/llitfkitfk/nginx-cookbook/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240390718,"owners_count":19793782,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cookbook","nginx"],"created_at":"2024-10-11T06:14:08.180Z","updated_at":"2025-02-23T23:14:27.075Z","avatar_url":"https://github.com/llitfkitfk.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Nginx 使用手册\n\n## 目录\n\n- [1. Nginx 基础](#1-nginx-基础)\n  - [1.1 安装](#11-安装)\n  - [1.2 相关文件](#12-相关文件)\n  - [1.3 命令](#13-命令)\n  - [1.4 静态文件服务](#14-静态文件服务)\n- [2. 负载均衡](#2-负载均衡)\n  - [2.1 HTTP 负载均衡](#21-http-负载均衡)\n  - [2.2 TCP 负载均衡](#22-tcp-负载均衡)\n  - [2.3 UDP 负载均衡](#23-udp-负载均衡)\n  - [2.4 负载均衡方法](#24-负载均衡方法)\n- [3. 流量管理](#3-流量管理)\n  - [3.1 A/B 测试](#31-ab-测试)\n  - [3.2 限制连接数](#32-限制连接数)\n  - [3.3 限制速率](#33-限制速率)\n  - [3.4 限制带宽](#34-限制带宽)\n\n### 1. Nginx 基础\n\n#### 1.1 安装\n\n```\n# 安装nginx包\napt-get install -y nginx\n# 开机启动\nsystemctl enable nginx\n# 开启nginx服务\nsystemctl start nginx\n```\n\n- docker 容器\n\n```\ndocker run -d -p 80:80 -p 443:443 --name nginx nginx\n```\n\n#### 1.2 相关文件\n\n```\n# 默认配置路径\n/etc/nginx\n# 默认配置文件\n/etc/nginx/nginx.conf\n# 默认http服务配置文件\n/etc/nginx/conf.d/\n# 默认日志文件\n/var/log/nginx/\n```\n\n#### 1.3 命令\n\n```\n# 测试配置文件\nnginx -t\n# 动态命令 stop/quit/reload/reopen\nnginx -s \u003csignal\u003e\n# 动态重载\nnginx -s reload\n```\n\n#### 1.4 静态文件服务\n\n```\nserver {\n    listen 80 default_server;\n    # www.gokit.info 可以改为其他domain\n    server_name www.gokit.info;\n    location / {\n        root /usr/share/nginx/html;\n        index index.html index.htm;\n    }\n}\n```\n\n### 2. 负载均衡\n\n#### 2.1 HTTP 负载均衡\n\n```\n# 文件: /etc/nginx/conf.d/http_load_balancing_test.conf\nupstream backend {\n    server 192.168.100.101:80   weight=1;\n    server web.gokit.info:80    weight=2;\n    server web2.gokit.info:80   backup;\n}\nserver {\n    location / {\n        proxy_pass http://backend;\n    }\n}\n```\n\n#### 2.2 TCP 负载均衡\n\n```\n# 文件: /etc/nginx/stream.conf.d/tcp_load_balancing_test.conf\n\nupstream redis_read {\n    server redis1.gokit.info:6789   weight=5;\n    server redis2.gokit.info:6789;\n    server 192.168.100.101:6789     backup;\n}\nserver {\n    listen 6789;\n    proxy_pass redis_read;\n}\n\n```\n\n- 注意如果使用 tcp 负载均衡\n  需要在/etc/nginx/nginx.conf 文件内添加 stream 模块\n\n```\nuser nginx;\n...\nevents {\n    ...\n}\n\n# 添加以下内容\nstream {\n    include /etc/nginx/stream.conf.d/*.conf;\n}\n# 添加以上内容\n\nhttp {\n    ...\n}\n```\n\n#### 2.3 UDP 负载均衡\n\n```\n# 文件: /etc/nginx/stream.conf.d/udp_load_balancing_test.conf\n\nupstream ntp {\n    server ntp1.gokit.info:456   weight=2;\n    server ntp1.gokit.info:456;\n}\nserver {\n    listen 456 udp;\n    proxy_pass ntp;\n}\n\n```\n\n#### 2.4 负载均衡方法\n\n- Round robin - 轮询 (默认)\n\n```\nupstream backend {\n    server backend1.gokit.info;\n    server backend2.gokit.info;\n}\n```\n\n- Least connections - 最少连接\n\n```\nupstream backend {\n    least_conn;\n    server backend1.gokit.info;\n    server backend2.gokit.info;\n}\n```\n\n- IP hash - IP 散列\n\n```\nupstream backend {\n    ip_hash;\n    server backend1.gokit.info;\n    server backend2.gokit.info;\n}\n```\n\n### 3. 流量管理\n\n#### 3.1 A/B 测试\n\n```\nhttp {\n    split_clients \"${remote_addr}\" $site_root_folder {\n        20% \"/var/www/site1\"\n        *   \"/var/www/site2\"\n    }\n    server {\n        listen 80;\n        server_name static-test.gokit.info;\n        root $site_root_folder;\n        location / {\n            index index.html;\n        }\n    }\n\n    split_clients \"${remote_addr}AAA\" $variant {\n        20% \"backendv2\";\n        *   \"backendv1\";\n    }\n    server {\n        listen 80;\n        server_name proxy-test.gokit.info;\n        location / {\n            proxy_pass http://$variant;\n        }\n    }\n}\n```\n\n#### 3.2 限制连接数\n\n```\nhttp {\n    limit_conn_zone $binary_remote_addr zone=limitbyaddr:16m;\n    limit_conn_status 429; # 429 Too Many Requests\n    server {\n        limit_conn limitbyaddr 40;\n    }\n}\n```\n\n#### 3.3 限制速率\n\n```\nhttp {\n    limit_req_zone $binary_remote_addr zone=limitbyaddr:16m rate=4r/s;\n    limit_req_status 429;\n    server {\n        limit_req zone=limitbyaddr burst=16 delay=8;\n    }\n}\n```\n\n#### 3.4 限制带宽\n\n```\nlocation /download/ {\n    limit_rate_after 16m;\n    limit_rate 1m; # 每秒1m\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fllitfkitfk%2Fnginx-cookbook","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fllitfkitfk%2Fnginx-cookbook","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fllitfkitfk%2Fnginx-cookbook/lists"}