├── .editorconfig ├── .gitignore ├── .travis.yml ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── docs ├── .vuepress │ ├── config.js │ ├── override.styl │ └── public │ │ └── logo.jpg ├── README.md ├── config │ └── README.md ├── example │ ├── README.md │ ├── add_header.md │ ├── autoindex.md │ ├── cors.md │ ├── domain.md │ ├── echo.md │ ├── error-page.md │ ├── expires.md │ ├── http-concat.md │ ├── https.md │ ├── image-valid.md │ ├── nodejs-proxy.md │ ├── proxy_pass.md │ ├── split-log.md │ └── www-permanent.md ├── guide │ ├── README.md │ ├── dir.md │ ├── error.md │ ├── linux-install.md │ └── nginx-configure-descriptions.md └── variable │ ├── README.md │ ├── client.md │ ├── server.md │ └── url.md ├── netlify.sh ├── package.json └── yarn.lock /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | trim_trailing_whitespace = true 9 | 10 | [*.{js,vue,scss}] 11 | insert_final_newline = true 12 | 13 | [*.md] 14 | trim_trailing_whitespace = false 15 | indent_size = 4 -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | 39 | dist -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "8" 4 | cache: 5 | directories: 6 | - node_modules 7 | script: 8 | - npm run build 9 | deploy: 10 | local-dir: dist 11 | provider: pages 12 | skip_cleanup: true 13 | github_token: $GH_TOKEN 14 | on: 15 | branch: master -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # 贡献指南 2 | 3 | 非常感谢您关注 Nginx 入门教程项目,在提交您的贡献之前,请务必认真阅读以下准则。 4 | 5 | - [问题反馈](#Issue) 6 | - [开发指南](#develop) 7 | - [常用脚本命令(npm scripts)](#npm-scripts) 8 | - [项目结构](#dir-spec) 9 | - [提交请求(pull request)](#pull-request) 10 | - [文档说明](#docs-spec) 11 | 12 | 13 | ## 问题反馈 14 | 15 | - 请避免提交重复的 Issue,在提交之前搜索现有的 Issue 。 16 | - 请确定 Issue 的类型,并在 Issue 内容描述清晰,我们将根据内容对 Issue 打上对应的 label 。 17 | 18 | 19 | ## 开发设置 20 | 21 | 项目依赖 [VuePress](https://vuepress.vuejs.org/) ,需要安装 [Node.js](https://nodejs.org/) 版本8+ ,下载项目到本地后安装依赖 `yarn install` 。 22 | 23 | 24 | ### 常用脚本命令 25 | 26 | ``` bash 27 | # 启动本地 HTTP 服务器用来本地预览 28 | $ yarn dev 29 | 30 | # 编译 markdown 文档为 html 文件 31 | $ yarn build 32 | ``` 33 | 34 | 35 | ### 项目结构 36 | 37 | ``` 38 | docs - 文档目录 39 | ├── .vuepress - 文档配置 40 | ├── README.md - 文档首页 41 | ├── config - 配置文档目录 42 | ├── example - 示例文档目录 43 | ├── guide - 入门文档目录 44 | └── variable - 变量文档目录 45 | ``` 46 | 47 | 48 | ### 提交请求(pull request) 49 | 50 | 1. fork [xuexb/learn-nginx](https://github.com/xuexb/learn-nginx) 51 | 1. 把个人仓库(repository)克隆到电脑上,并安装所依赖的插件。 52 | 1. 开始编辑文件,可以通过命令 `yarn dev` 预览编辑的效果。 53 | 1. 推送(push)分支。 54 | 1. 建立一个新的合并申请(pull request)并描述变动。 55 | 56 | 57 | ## 文档说明 58 | 59 | - 文档基于 Markdown 格式编写。 60 | - 文档编辑地址可通过访问文档左下角编辑按钮获得。 61 | - 文档内链接使用绝对路径,如: `[新的起点](/guide/dir.html)` 。 62 | - 使用中文半角标点符号。 63 | - 名词区分大小写。 64 | - 英文、代码、链接前后添加空格。 65 | - 新增文档需要添加文档对应的目录配置(`docs/.vuepress/config.js`)。 66 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 前端小武 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [Nginx 入门教程](https://xuexb.github.io/learn-nginx/) 2 | 3 | [![](https://travis-ci.org/xuexb/learn-nginx.svg?branch=master)](https://github.com/xuexb/learn-nginx) 4 | [![Netlify Status](https://api.netlify.com/api/v1/badges/891550d0-806a-4e2d-9e0d-b0c3d8afec94/deploy-status)](https://app.netlify.com/sites/learn-nginx/deploys) 5 | 6 | 学习 Nginx 配置, 包括: 编译安装、反向代理、重定向、重写、缓存、跨域配置等。 7 | 8 | ## 起因 9 | 10 | 起源是我的博客,博客从 IIS 伪静态起,我就一直需要接触一些配置,后来遇到了 Nginx ,被她『美丽动人的简约』打动,至些博客开始了各种折腾之路,比如:负载均衡、反向代理、前置缓存等,我把这些操作都记录到了 GitHub ,只是闲时写点笔记,也不成体系。 11 | 12 | 直到 [VuePress](https://vuepress.vuejs.org) 这么简约的文档起步,我意识到可以重新构建下站点了,起名叫:Nginx 入门教程。 13 | 14 | 基于 VuePress 构建在 GitHub Pages 中,PR 时基于 [Netlify](https://www.netlify.com) 自动部署预览测试环境方便回归。 15 | 16 | ## 欢迎你的加入 17 | 18 | 但我的个人能力有限,水平也有限,只能大概的构建出我的思路,希望有想法的朋友提交代码,一起完善她!阅读 [贡献指南](CONTRIBUTING.md) 查看更多开发者帮助。 19 | 20 | ## LICENSE 21 | 22 | MIT 23 | -------------------------------------------------------------------------------- /docs/.vuepress/config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | dest: 'dist', 3 | base: '/learn-nginx/', 4 | locales: { 5 | '/': { 6 | lang: 'zh-CN', 7 | title: 'Nginx 入门教程', 8 | description: '学习 Nginx 配置, 包括: 编译安装、反向代理、重定向、重写、缓存、跨域配置等', 9 | }, 10 | }, 11 | head: [ 12 | ['link', { rel: 'icon', href: `/logo.jpg` }], 13 | ], 14 | themeConfig: { 15 | repo: 'xuexb/learn-nginx', 16 | editLinks: true, 17 | docsDir: 'docs', 18 | locales: { 19 | '/': { 20 | label: '中文', 21 | selectText: '语言', 22 | editLinkText: '编译该页面', 23 | lastUpdated: '最后更新', 24 | nav: [ 25 | { 26 | text: '入门', 27 | link: '/guide/', 28 | }, 29 | { 30 | text: '变量', 31 | link: '/variable/', 32 | }, 33 | { 34 | text: '示例', 35 | link: '/example/', 36 | }, 37 | ], 38 | sidebar: { 39 | '/guide/': genSidebarConfigGuide('入门'), 40 | '/variable/': genSidebarConfigVariable('变量'), 41 | '/example/': genSidebarConfigExample('示例'), 42 | } 43 | }, 44 | } 45 | } 46 | } 47 | 48 | function genSidebarConfigGuide (title) { 49 | return [ 50 | { 51 | title, 52 | collapsable: false, 53 | children: [ 54 | '', 55 | 'dir', 56 | 'linux-install', 57 | 'nginx-configure-descriptions', 58 | 'error', 59 | ] 60 | } 61 | ] 62 | } 63 | 64 | function genSidebarConfigVariable (title) { 65 | return [ 66 | { 67 | title, 68 | collapsable: false, 69 | children: [ 70 | '', 71 | 'server', 72 | 'client', 73 | 'url', 74 | ] 75 | } 76 | ] 77 | } 78 | 79 | function genSidebarConfigExample (title) { 80 | return [ 81 | { 82 | title, 83 | collapsable: false, 84 | children: [ 85 | '', 86 | 'error-page', 87 | 'www-permanent', 88 | 'nodejs-proxy', 89 | 'image-valid', 90 | 'https', 91 | 'domain', 92 | 'echo', 93 | 'http-concat', 94 | 'split-log', 95 | 'expires', 96 | 'autoindex', 97 | 'add_header', 98 | 'proxy_pass', 99 | ] 100 | } 101 | ] 102 | } 103 | 104 | -------------------------------------------------------------------------------- /docs/.vuepress/override.styl: -------------------------------------------------------------------------------- 1 | .theme-container.page-main-max 2 | .content 3 | max-width 80% -------------------------------------------------------------------------------- /docs/.vuepress/public/logo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xuexb/learn-nginx/e6b6fc46c284aa405720d6e180a31cd0bbeb87f7/docs/.vuepress/public/logo.jpg -------------------------------------------------------------------------------- /docs/README.md: -------------------------------------------------------------------------------- 1 | --- 2 | home: true 3 | heroImage: /logo.jpg 4 | actionText: 快速上手 → 5 | actionLink: /guide/ 6 | features: 7 | - title: 0️⃣ 从零开始 8 | details: 记录了从不会 Nginx 到可以写一些常用的配置过程 9 | - title: 🔥 丰富示例 10 | details: 结合使用场景,涵盖 WEB 开发中常用的示例,以及对应的说明 11 | - title: 🌐 开源 12 | details: 代码完全开源在 GitHub ,希望大家一起共建 13 | footer: MIT Licensed | Copyright © 2018-present @xuexb 14 | --- 15 | -------------------------------------------------------------------------------- /docs/config/README.md: -------------------------------------------------------------------------------- 1 | # 配置 2 | -------------------------------------------------------------------------------- /docs/example/README.md: -------------------------------------------------------------------------------- 1 | # 示例 2 | 3 | 包括常用的一些 Nginx 示例和使用技巧,如果你在使用中遇到问题,欢迎去提 [Issue](https://github.com/xuexb/learn-nginx/issues/new) 。 -------------------------------------------------------------------------------- /docs/example/add_header.md: -------------------------------------------------------------------------------- 1 | # add_header 指令技巧 2 | 3 | 官方的介绍: 4 | 5 | > Adds the specified field to a response header provided that the response code equals 200, 201 (1.3.10), 204, 206, 301, 302, 303, 304, 307 (1.1.16, 1.0.13), or 308 (1.13.0). The value can contain variables. 6 | > 7 | > There could be several add_header directives. These directives are inherited from the previous level if and only if there are no add_header directives defined on the current level. 8 | > 9 | > If the always parameter is specified (1.7.5), the header field will be added regardless of the response code. 10 | 11 | 意思也就是说话在响应状态码**成功**时,`add_header` 指令才生效,并且当前《作用域》下没有 `add_header` 指令时,会向上层继承。 12 | 13 | 在使用过程中难免会遇到上级指令被覆盖的情况,如: 14 | 15 | ```nginx 16 | server { 17 | add_header x-name nginx; 18 | 19 | location / { 20 | root /path; 21 | } 22 | 23 | location /static/ { 24 | add_header x-name2 nginx2; 25 | } 26 | } 27 | ``` 28 | 29 | 当匹配到 `/` 时,由于 `location /` 中没有 `add_header` 指令,所以会继承 `server` 中的 `x-name` ,而当匹配到 `/static/` 时,由于内容已经有 `add_header` 指令,则上层的 `x-name` 不会被继承,导致只会输出 `x-name2` 。 30 | 31 | ## 使用 include 语法 32 | 33 | 因为项目中的应用往往配置会有很多,我们可以把具体的功能拆分成独立的配置文件,使用 `include` 引用进来,如: 34 | 35 | - `inc/no-cache.conf` - 无缓存 36 | - `inc/cache-max.conf` - 缓存最大 37 | - `inc/sts.conf` - STS 38 | - `inc/xss.conf` - XSS 安全过滤 39 | - `inc/php.conf` - PHP FastCGI 40 | - ... 41 | 42 | 这样就可以按需引用了,如: 43 | 44 | ```nginx 45 | location / { 46 | include inc/sts.conf; 47 | include inc/security.conf; 48 | include vhost/xiaoshuo.io/no-cache.conf; 49 | } 50 | 51 | # png,jpg 转 webp 52 | location ~* \.(jpg|png|meibanfawojiuxiangchangdianyirangquanzhongdadian)$ { 53 | include inc/sts.conf; 54 | include inc/security.conf; 55 | include vhost/xiaoshuo.io/cache-max.conf; 56 | 57 | if ($cookie_webp = '1') { 58 | rewrite ^/(.*)$ /$1.webp last; 59 | } 60 | } 61 | location ~* \.webp$ { 62 | include inc/sts.conf; 63 | include inc/security.conf; 64 | include vhost/xiaoshuo.io/cache-max.conf; 65 | try_files $uri $uri/ @webp; 66 | } 67 | location @webp { 68 | content_by_lua_file 'lua/webp.lua'; 69 | } 70 | 71 | # http-concat + max cache 72 | location /style/js/ { 73 | include inc/sts.conf; 74 | include inc/security.conf; 75 | include vhost/xiaoshuo.io/cache-max.conf; 76 | concat on; 77 | concat_types application/javascript; 78 | concat_max_files 30; 79 | default_type application/javascript; 80 | } 81 | location /style/css/ { 82 | include inc/sts.conf; 83 | include inc/security.conf; 84 | include vhost/xiaoshuo.io/cache-max.conf; 85 | concat on; 86 | concat_types text/css; 87 | concat_max_files 30; 88 | default_type text/css; 89 | } 90 | 91 | # 时效性高的接口 92 | location ~* ^/((so\/(.+))||ajax)\.php$ { 93 | include inc/sts.conf; 94 | include inc/security.conf; 95 | include vhost/xiaoshuo.io/php.conf; 96 | add_header cache-control 'private, max-age=0, no-cache'; 97 | } 98 | 99 | # 其实所有的 php 100 | location ~* \.php$ { 101 | set $skip_cache 0; 102 | if ($request_method = POST) { 103 | set $skip_cache 1; 104 | } 105 | if ($arg_nocache = "1") { 106 | set $skip_cache 1; 107 | } 108 | 109 | fastcgi_cache_key $scheme$request_method$host$request_uri; 110 | fastcgi_cache_use_stale 111 | error 112 | timeout 113 | invalid_header 114 | updating 115 | http_500 116 | http_503; 117 | fastcgi_cache_bypass $skip_cache; 118 | fastcgi_no_cache $skip_cache; 119 | fastcgi_cache_valid 200 5m; 120 | fastcgi_cache_valid any 1m; 121 | fastcgi_cache m_xiaoshuo_php_cache; 122 | 123 | add_header x-php-cache-status $upstream_cache_status; 124 | include inc/sts.conf; 125 | include inc/security.conf; 126 | include vhost/xiaoshuo.io/cache-small.conf; 127 | include vhost/xiaoshuo.io/php.conf; 128 | } 129 | ``` -------------------------------------------------------------------------------- /docs/example/autoindex.md: -------------------------------------------------------------------------------- 1 | # 配置默认主页、目录浏览 2 | 3 | ## 设置默认主页 4 | 5 | 直接可以使用目录形式打开的页面称为默认主页,一般常见的有: `index.html`、`index.htm`、`index.php` 这些,要吧通过配置来完成,如: 6 | 7 | ```nginx 8 | server { 9 | root /网站根目录; 10 | 11 | # 设置默认主页,支持多个,按优先级来,空格分格 12 | index index.html index.htm index.php; 13 | } 14 | ``` 15 | 16 | ## 设置目录浏览 17 | 18 | 当一个目录内没有默认主页的文件时,直接访问目录会报 `403 Forbidden` 错误,而启用目录浏览功能后可以直接列出当前目录下的文件、文件夹,如: 19 | 20 | ```nginx 21 | server { 22 | root /网站根目录; 23 | 24 | # 优先使用默认主页 25 | index index.html index.htm; 26 | 27 | # 当默认主页不存在时直接列出目录内文件树 28 | autoindex on; 29 | } 30 | ``` 31 | 32 | 但多 `autoindex` 相关可官方文档: [http://nginx.org/en/docs/http/ngx_http_autoindex_module.html](http://nginx.org/en/docs/http/ngx_http_autoindex_module.html) 33 | 34 | 注意,**线上运行环境最好别开启该配置**,因为这将直接暴露你的文件~ -------------------------------------------------------------------------------- /docs/example/cors.md: -------------------------------------------------------------------------------- 1 | # 配置 CORS 跨域 2 | 3 | ## 设置允许所有的请求 4 | 5 | ```nginx 6 | server { 7 | location / { 8 | add_header 'Access-Control-Allow-Origin' '*'; 9 | } 10 | } 11 | ``` 12 | 13 | ## 只允许GET请求 14 | 15 | ```nginx 16 | server { 17 | location / { 18 | add_header 'Access-Control-Allow-Origin' '*'; 19 | add_header 'Access-Control-Request-Method' 'GET'; 20 | } 21 | } 22 | ``` 23 | 24 | ## 请求白名单 25 | 26 | ```nginx 27 | server { 28 | location / { 29 | # 白名单 30 | if ($http_origin ~* (baidu\.com|github.xuexb.com)$) { 31 | add_header 'Access-Control-Allow-Origin' '$http_origin'; 32 | 33 | # 允许cookie 34 | add_header 'Access-Control-Allow-Credentials' true; 35 | 36 | # 只允许某些方法 37 | add_header 'Access-Control-Request-Method' 'GET, POST, OPTIONS'; 38 | 39 | # 支持获取其她字段, 需要前端设置 `xhr.withCredentials = true` 40 | add_header 'Access-Control-Allow-Headers' 'User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; 41 | } 42 | } 43 | } 44 | ``` 45 | 46 | ## iconfont 字体跨域配置 47 | 48 | 49 | ```nginx 50 | server { 51 | root xxx; 52 | 53 | # 使用location来匹配以字体文件 54 | location ~* \.(eot|otf|ttf|woff|svg)$ { 55 | add_header Access-Control-Allow-Origin *; 56 | } 57 | } 58 | ``` 59 | 60 | 但如果你的 `location` 已经配置了, 可以使用 `if` 判断添加, 如: 61 | 62 | ```nginx 63 | server { 64 | location / { 65 | # 使用判断请求文件来添加 66 | if ($document_uri ~ \.(eot|otf|ttf|woff|svg)$) { 67 | add_header Access-Control-Allow-Origin *; 68 | } 69 | } 70 | } 71 | ``` -------------------------------------------------------------------------------- /docs/example/domain.md: -------------------------------------------------------------------------------- 1 | # 配置泛域名转发 2 | 3 | 有的时候,我们需要配置一些自定义的子域名,如: 4 | 5 | - `xuexb.user.demo.com` 6 | - `a01.user.demo.com` 7 | 8 | 这时候就需要域名的 DNS 解析一个泛域名 `*.user.demo.com` 到服务器,Nginx 可以配置如下: 9 | 10 | ## 子域名转发到子目录 11 | 12 | ```nginx 13 | server { 14 | listen 80; 15 | server_name ~^([\w-]+)\.user\.demo\.com$; 16 | 17 | location / { 18 | proxy_set_header X-Real-IP $remote_addr; 19 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 20 | proxy_set_header Host $http_host; 21 | proxy_set_header X-NginX-Proxy true; 22 | proxy_pass http://127.0.0.1:8080/$1$request_uri; 23 | } 24 | } 25 | ``` 26 | 27 | 以上配置表示: 28 | 29 | - `xuexb.user.demo.com/path?a=1` -> `127.0.0.1:8080/xuexb/path?a=1` 30 | - `a01.user.demo.com/path?a=1` -> `127.0.0.1:8080/a01/path?a=1` 31 | 32 | 这样后端就可以根据子目录解析不同的规则,甚至 Nginx 可以再进行链接重写。 33 | 34 | ## 子域名配置不同的目录 35 | 36 | ```nginx 37 | server { 38 | listen 80; 39 | server_name ~^([\w-]+)\.user\.demo\.com$; 40 | 41 | root /home/user/wwwroot/user/$1; 42 | } 43 | ``` 44 | 45 | 以上配置可以把不同的子域名分发到不同的目录中,做到路径分离的功能,如: 46 | 47 | - `xuexb.user.demo.com` -> `/home/user/wwwroot/user/xuexb`; 48 | - `a01.user.demo.com` -> `/home/user/wwwroot/user/a01`; -------------------------------------------------------------------------------- /docs/example/echo.md: -------------------------------------------------------------------------------- 1 | # 学习使用 echo 模块 2 | 3 | Nginx echo模块是在nginx程序上扩展了 `echo` 输出字符的功能,对于调试很方便,项目地址: 。 4 | 5 | ## 安装 6 | 7 | > 目前支持 `nginx-1.11.2`,高版本可能安装失败,我装 `1.11.13` 失败了,更多支持nginx的版本见: [https://github.com/openresty/echo-nginx-module#compatibility](https://github.com/openresty/echo-nginx-module#compatibility) 8 | 9 | 去 [release](https://github.com/openresty/echo-nginx-module/releases) 下载最新的安装包,并解压. 10 | 11 | 在配置 Nginx 时用: `./configure --add-module=/你的解压路径`,并编译安装,如果是重新编译安装可参考: [重新编译安装](/guide/linux-install.html#重新编译安装) 12 | 13 | ## 使用 14 | 15 | > 只是研究了一些常用的 16 | 17 | ### echo - 输出字符 18 | 19 | - 语法: `echo [options] ...` 20 | - 默认值: `no` 21 | 22 | ```conf 23 | # 简单的hello,world! 24 | server { 25 | location = /api/hello { 26 | echo "hello,world!"; 27 | } 28 | } 29 | ``` 30 | 31 | 默认echo会自动换行,不需要换行可以: `echo -n xxx;`; 32 | 33 | ### echo_before_body,echo_after_body - 页面前、后插入内容 34 | 35 | - 语法: `echo_before_body [options] [argument]...` 36 | - 默认值: `no` 37 | 38 | ```conf 39 | server { 40 | # 简单的输出 41 | location = /api/hello { 42 | echo_before_body echo_before_body; 43 | echo "hello,world!"; 44 | echo_after_body echo_after_body; 45 | } 46 | 47 | # 常见于代理页面前、后插内容 48 | location = /api/proxy_before_after { 49 | echo_before_body echo_before_body; 50 | echo_before_body echo_before_body; 51 | proxy_pass http://127.0.0.1; 52 | echo_after_body echo_after_body; 53 | echo_after_body echo_after_body; 54 | } 55 | } 56 | ``` 57 | 58 | ### echo_sleep - 请求等待 59 | 60 | - 语法: `echo_sleep ` 61 | - 默认值: `no` 62 | 63 | ```conf 64 | server { 65 | # 简单的输出 66 | location = /api/sleep { 67 | echo 1; 68 | 69 | # 等待1秒 70 | echo_sleep 1; 71 | 72 | echo 2; 73 | } 74 | } 75 | ``` 76 | 77 | ### echo_location_async,echo_location - 请求指定路径 78 | 79 | - 语法: `echo_location_async []` 80 | - 默认值: `no` 81 | 82 | ```conf 83 | location /main { 84 | # 异步调用/sub 85 | echo_location_async /sub; 86 | 87 | echo world; 88 | } 89 | 90 | location /main2 { 91 | # 同步调用/sub 92 | echo_location_async /sub; 93 | 94 | echo world; 95 | } 96 | 97 | location /sub { 98 | echo hello; 99 | } 100 | ``` 101 | 102 | 异步跟同步的区别是: 103 | 104 | 1. 异步会并行的去请求 105 | 2. 同步等待当前请求结束才会往下执行 106 | 107 | 下面这个整个时间为2s,因为新路径最大是2s: 108 | ```conf 109 | location /main { 110 | echo_location_async /sub1; 111 | echo_location_async /sub2; 112 | echo "took $echo_timer_elapsed sec for total."; 113 | } 114 | location /sub1 { 115 | echo_sleep 2; 116 | } 117 | location /sub2 { 118 | echo_sleep 1; 119 | } 120 | ``` 121 | 122 | 下面这个整个时间为3s,因为需要等待`/sub1`完成才进入`/sub2`: 123 | ```conf 124 | location /main { 125 | echo_location /sub1; 126 | echo_location /sub2; 127 | echo "took $echo_timer_elapsed sec for total."; 128 | } 129 | location /sub1 { 130 | echo_sleep 2; 131 | } 132 | location /sub2 { 133 | echo_sleep 1; 134 | } 135 | ``` 136 | 137 | 可以通过第二个参数传`querystring`: `echo_location_async /sub 'foo=Foo&bar=Bar';` 138 | 139 | ### echo_foreach_split - 分隔循环 140 | 141 | - 语法: `echo_foreach_split ` 142 | 143 | ```conf 144 | location /split { 145 | echo_foreach_split ',' $arg_list; 146 | echo "item: $echo_it"; 147 | echo_end; 148 | } 149 | ``` 150 | 151 | 上面配置当访问: `/split?list=cat,dog,mouse` 时会输出: 152 | 153 | ``` 154 | item: cat 155 | item: dog 156 | item: mouse 157 | ``` 158 | 159 | `$arg_list`是对应`$args.list` 160 | 161 | 那么配置全`echo_location_async`可以做成`nginx-combo`服务了,如: 162 | 163 | ```conf 164 | location = /api/combo { 165 | echo_foreach_split ',' $query_string; 166 | echo "/* combo: $echo_it */"; 167 | echo_location_async $echo_it; 168 | echo; 169 | echo_end; 170 | } 171 | ``` 172 | 173 | 访问: `/api/combo?/a.js,/b.js`,需要注意的是文件的路径必须以`/`开始,因为匹配的`location`,当然真的 `nginx-combo` 服务请看: [nginx-http-concat](/example/http-concat.html) 174 | 175 | --- 176 | 177 | 其他变量请去官网查看~ 178 | 179 | ## link 180 | 181 | - [官网](https://github.com/openresty/echo-nginx-module) 182 | - [我写的在线Demo](//echo.xuexb.com) -------------------------------------------------------------------------------- /docs/example/error-page.md: -------------------------------------------------------------------------------- 1 | # Nginx 状态码配置和错误文件 2 | 3 | 4 | ```nginx 5 | server { 6 | # 配置访问 /test.js 时报 403 错 7 | location /test.js { 8 | return 403; 9 | } 10 | 11 | # 配置访问 /404 时报 404 错 12 | location /404 { 13 | return 404; 14 | } 15 | 16 | # 配置访问 /500 时报 500 错 17 | location /500 { 18 | return 500; 19 | } 20 | 21 | # 把指定状态码指向这个文件 uri 22 | error_page 500 502 503 504 /status.html; 23 | error_page 404 /status.html; 24 | } 25 | ``` 26 | 27 | 如: 28 | 29 | ```nginx 30 | server { 31 | listen 80; 32 | server_name test.me; 33 | 34 | root /Users/xiaowu/work/test.me; 35 | 36 | # 用if匹配任何以 403 开头的,会匹配到 /4034444 37 | if ($request_uri ~* ^/403) { 38 | return 403; 39 | } 40 | 41 | # 用location匹配 /500/ /500,但不匹配 /500/1 42 | location ~* "^/500/?$" { 43 | return 500; 44 | } 45 | 46 | # 用if匹配以 /501/ 开头的,匹配 /501/1,/501/1/2 但不匹配 /501 47 | if ($request_uri ~* ^/501/) { 48 | return 501; 49 | } 50 | 51 | # 用location匹配 /502/ /502 /502/1 /502/1/2 52 | location ~* "^/502(/.*)?$" { 53 | return 502; 54 | } 55 | 56 | # 用location只匹配 /503 57 | location = /503 { 58 | return 503; 59 | } 60 | } 61 | ``` 62 | 63 | ### error_page配置小提示 64 | 65 | 注意 `error_page` 配置时加 `=` 和不加 `=` 的区别,加了 `=` 表示响应为指定的 `http status code` ,默认为 200,不加 `=` 为原错误的状态码~ 66 | 67 | ```nginx 68 | # 这样可以访问错误页面时 http status 为 404 ,并且页面内容是 404.html 的内容 69 | error_page 404 /404.html 70 | error_page 404 500 /404.html; 71 | 72 | # 这样配置访问错误页面时 http status 为 200 ,但页面内容是 404.html 的内容 73 | error_page 404 500 = /404.html; 74 | 75 | # 这样配置访问错误页面时 http status 为 404 ,但页面内容是 404.html 的内容 76 | error_page 404 500 =404 /404.html; 77 | 78 | # 也可以把404请求直接301到某个域上 79 | error_page 404 =301 https://xuexb.com/404; 80 | ``` 81 | 82 | 这样就可以根据自己需求配置错误页为指定的状态码,因为非 200 的状态码可能会被浏览器拦截。 83 | -------------------------------------------------------------------------------- /docs/example/expires.md: -------------------------------------------------------------------------------- 1 | # 配置浏览器缓存 2 | 3 | 使用 `expires` 参数。 4 | 5 | ## 不缓存 6 | 7 | ```nginx 8 | server { 9 | expires -1; 10 | } 11 | ``` 12 | 13 | 输出Response Headers: 14 | 15 | ``` 16 | Cache-Control:no-cache 17 | ``` 18 | 当文件没有变更时会返回 304 ,有变更时会是 200 ,如果强制命中 200 可以再添加: `if_modified_since off;` 忽略 Request Headers 里的 `If-Modified-Since` 字段。 19 | 20 | ## 缓存 21 | 22 | ```nginx 23 | server { 24 | expires 1d; 25 | } 26 | ``` 27 | 28 | 1d为1天,单位如下: 29 | 30 | ``` 31 | ms milliseconds 32 | s seconds 33 | m minutes 34 | h hours 35 | d days 36 | w weeks 37 | M months,30 days 38 | y years,365 days 39 | ``` 40 | 41 | 如果希望最大缓存可以: 42 | 43 | ```nginx 44 | server { 45 | expires max; 46 | } 47 | ``` 48 | 49 | 输出Response Headers: 50 | 51 | ``` 52 | Cache-Control:max-age=315360000 53 | ``` 54 | 55 | ## 根据链接设置缓存时间 56 | 57 | 58 | ```nginx 59 | server { 60 | # 设置为1月 61 | set $expires_time 1M; 62 | 63 | # 针对后台不缓存 64 | if ($request_uri ~* ^/admin(\/.*)?$) { 65 | set $expires_time -1; 66 | } 67 | 68 | # 针对静态文件缓存最大 69 | if ($request_uri ~* ^/static(\/.*)?$) { 70 | set $expires_time max; 71 | } 72 | 73 | # 设置吧 74 | expires $expires_time; 75 | } 76 | ``` -------------------------------------------------------------------------------- /docs/example/http-concat.md: -------------------------------------------------------------------------------- 1 | # 使用 nginx-http-concat 2 | 3 | nginx-http-concat 是一个 Nginx 扩展模块,用来合并 HTTP 请求。 4 | 5 | ## 1. 下载 6 | 7 | 访问 [https://github.com/alibaba/nginx-http-concat/releases](https://github.com/alibaba/nginx-http-concat/releases) 官网下载最新的源包,如: 8 | 9 | ```bash 10 | # 下载 11 | wget https://github.com/alibaba/nginx-http-concat/archive/1.2.2.tar.gz 12 | 13 | # 解压并记录解压后的目录 14 | tar xzf 1.2.2.tar.gz 15 | ``` 16 | 17 | ## 2. 安装 18 | 19 | 使用 [编译安装](/guide/install.html) ,在配置 configure 时添加参数: 20 | 21 | ```shell 22 | # 配置 23 | ./configure 其他编译参数 --add-module=/刚才解压的目录 24 | 25 | # 安装 26 | make 27 | [sudo] make install 28 | ``` 29 | 30 | 注意: 如果是重新编译安装时不要运行 `make install`,可参数: [重新编译安装](/guide/linux-install.html#重新编译安装) 31 | 32 | 如我的配置: 33 | 34 | ```shell 35 | ./configure 36 | --... 37 | --add-module=/home/work/src/nginx-http-concat-1.2.2 38 | ``` 39 | 40 | > 如果有多个 `--add-module` 分别对应写上即可 41 | 42 | ## 3. 配置 43 | 44 | 使用 location 匹配到你想要匹配的路径,对其设置参数: 45 | 46 | ```nginx 47 | server { 48 | location /static/css/ { 49 | concat on; 50 | concat_types text/css; 51 | concat_max_files 20; 52 | } 53 | 54 | location /static/js/ { 55 | concat on; 56 | concat_types application/javascript; 57 | concat_max_files 30; 58 | } 59 | } 60 | ``` 61 | 62 | 重启服务,如: `nginx -s reload` 。 63 | 64 | 更新配置项点击: [https://github.com/alibaba/nginx-http-concat#module-directives](https://github.com/alibaba/nginx-http-concat#module-directives) 65 | 66 | ## 4. 使用 67 | 68 | 现在就可以通过url中的`??`来合并了,比如: `/static/css/??a.css,path/b.css` 。 -------------------------------------------------------------------------------- /docs/example/https.md: -------------------------------------------------------------------------------- 1 | # 配置 HTTPS 2 | 3 | 首先配置支持 HTTPS 必须让 Nginx 开启 `http_ssl_module` 模块,[点击查看nginx编译安装参数](/guide/nginx-configure-descriptions.html) ,可以使用`nginx -V`查看是否开启`TLS SNI support enabled`。 4 | 5 | 购买/生成 SSL 证书,可以使用免费的证书,比如:[Let's Encrypt,免费好用的 HTTPS 证书](https://imququ.com/post/letsencrypt-certificate.html) 。 6 | 7 | ```conf 8 | # 配置 HTTPS 9 | 10 | # 配置个http的站点,用来做重定向,当然如果你不需要把 HTTP->HTTPS 可以把这个配置删了 11 | server { 12 | listen 80; 13 | 14 | # 配置域名 15 | server_name www.xxoo.com xxoo.com; 16 | 17 | # 添加 STS, 并让所有子域支持, 开启需慎重 18 | add_header strict-transport-security 'max-age=31536000; includeSubDomains; preload'; 19 | 20 | # 配置让这些 HTTP 的访问全部 301 重定向到 HTTPS 的 21 | rewrite ^(.*) https://www.xxoo.com$1 permanent; 22 | } 23 | 24 | # 配置 HTTPS 25 | server { 26 | # 配置域名 27 | server_name www.xxoo.com xxoo.com; 28 | 29 | # https默认端口 30 | listen 443; 31 | 32 | # 添加STS, 并让所有子域支持, 开启需慎重 33 | add_header strict-transport-security 'max-age=31536000; includeSubDomains; preload'; 34 | 35 | # https配置 36 | ssl on; 37 | ssl_certificate /xxoo/www.xxoo.com.crt; 38 | ssl_certificate_key /xxoo/www.xxoo.com.key; 39 | 40 | # 其他按正常配置处理即可... 41 | } 42 | ``` 43 | 44 | > 注意,这里证书的格式是 `.crt` 的。 45 | 46 | ### 配置后的访问规则 47 | 48 | 输入链接 | 最终访问链接 49 | --- | --- 50 | http://www.xxoo.com | https://www.xxoo.com 51 | http://www.xxoo.com/404/500 | https://www.xxoo.com/404/500 52 | http://xxoo.com | https://www.xxoo.com 53 | https://www.xxoo.com | -(原链接不变) 54 | https://xxoo.com/500 | https://www.xxoo.com/500 55 | 56 | ### 强烈推荐 57 | 58 | 使用 支持泛域名证书申请了,好赞。 -------------------------------------------------------------------------------- /docs/example/image-valid.md: -------------------------------------------------------------------------------- 1 | # 配置图片防盗链 2 | 3 | 防盗链是指当图片不是自己网站打开时返回 403 或者指定图片,是通过请求的来路判断是否是自己的站点来设置响应。 4 | 5 | ### 语法 6 | 7 | `valid_referers none | blocked | server_names | string` 8 | 9 | * `none`:表示没有来路 10 | * `blocked`:表示有来路 11 | * `server_names`:来路里包含当前域名 12 | * `string`:(忽略端口) 13 | * 如果是字符串:一个域名验证的规则,`*` 表示通配符 14 | * 如果是以 `~` 开头:正则表达式,排除https://或http://开头的字符串 15 | 16 | 以上参数可以叠加一起使用。 17 | 18 | ### 例子 19 | 20 | ```conf 21 | server { 22 | 23 | # 配置所有图片 24 | location ~* \.(gif|jpg|png|bmp)$ { 25 | # 验证可以是没有来路、或者有来路时来路匹配xuexb.com、或者匹配当前域名 26 | valid_referers none blocked *.xuexb.com server_names; 27 | 28 | # 如果验证不通过则返回403 29 | if ($invalid_referer) { 30 | return 403; 31 | } 32 | } 33 | } 34 | ``` -------------------------------------------------------------------------------- /docs/example/nodejs-proxy.md: -------------------------------------------------------------------------------- 1 | # Node.js 反向代理 2 | 3 | 服务端如果使用`nodejs`运行服务,由于端口不能同时多个服务占用,而服务器中可能又是多个网站,那么可以使用 Nginx 做反向代理,比如有这些网站域名和端口: 4 | 5 | 域名 | 端口 6 | --- | --- 7 | www.xxoo.com | 8001 8 | www.xo.com | 8002 9 | www.xo.cn | 8003 10 | 11 | 当然一个服务器上的网站可能还有更多,可以通过配置 Nginx 转发来代理这些端口分发,如: 12 | 13 | ```conf 14 | server { 15 | server_name www.xxoo.com; 16 | listen 80; 17 | 18 | # 设置这个网站的根目录 19 | root /wwwroot/www.xxoo.com/; 20 | 21 | # 由于下面配置了文件不存在则代码到 Node.js 中,那么直接访问目录(不带默认主页)的话会有问题,这里做下判断 22 | # 如果访问目录下有 index.html 文件,则直接重写到该文件 23 | # break 表示重写且停止,但 url 不变,而 permanent 表示301重定向,url 会更新 24 | if ( -f $request_filename/index.html ){ 25 | rewrite (.*) $1/index.html break; 26 | } 27 | 28 | # 如果请求的文件不存在,则代理到 Node.js 29 | if ( !-f $request_filename ){ 30 | rewrite (.*) /index.js; 31 | } 32 | 33 | # 代理node服务 8001 34 | location = /index.js { 35 | # 设置一些代理的header信息,这些信息将被透传到 Node.js 服务的header信息里 36 | proxy_set_header Connection ""; 37 | proxy_set_header X-Real-IP $remote_addr; 38 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 39 | proxy_set_header Host $http_host; 40 | proxy_set_header X-NginX-Proxy true; 41 | 42 | # 代理服务 43 | proxy_pass http://127.0.0.1:8001$request_uri; 44 | 45 | # 忽略其他重写 46 | proxy_redirect off; 47 | } 48 | } 49 | ``` 50 | 51 | 配置之后,比如你网站根目录里有 `index.html` 文件,访问 `url` 如: 52 | 53 | 访问链接 | 解析过程 | 备注 54 | --- | --- | --- 55 | www.xxoo.com/index.html | Nginx | 由于文件存在,直接使用 Nginx 输出 56 | www.xxoo.com | Nginx | 由于判断该目录下有 `index.html` 文件,则自动重写到该文件,但 `url` 不变 57 | www.xxoo.com/xx.html | Nginx -> Node.js:8001 | 由于文件不存在,使用 Nginx 代理到 Node.js 的 8001 端口 58 | www.xxoo.com/xxoo/ | Nginx -> Node.js:8001 | 首先判断该目录是否存在
如果存在再判断是否有 `index.html` 文件
一旦不成立,直接代理到 Node.js -------------------------------------------------------------------------------- /docs/example/proxy_pass.md: -------------------------------------------------------------------------------- 1 | # proxy_pass url 反向代理的坑 2 | 3 | 说到 Nginx 就不得不说 Nginx 的反向代理是多么的好用,一个指令 `proxy_pass` 搞定反向代理,对于接口代理、负载均衡很是实用,但 `proxy_pass` 指令后面的参数很有讲究。 4 | 5 | 网上有很多什么绝对路径、相对路径的说法,其实在实际的应用中就分为两种情况: 6 | 7 | ## url 只是 host 8 | 9 | 这里指不包含 `$uri` ,如: 10 | 11 | - `http://host` - √ 12 | - `https://host` - √ 13 | - `http://host:port` - √ 14 | - `https://host:port` - √ 15 | - `http://host/` - x 16 | - `http://host:port/` - x 17 | 18 | 这时候 `location` 匹配的完整路径将直接透传给 url ,如: 19 | 20 | ```nginx 21 | // 访问: / 后端: / 22 | // 访问: /api/xx 后端: /api/xx 23 | // 访问: /api/xx?aa 后端: /api/xx?aa 24 | location / { 25 | proxy_pass http://node:8080; 26 | } 27 | 28 | // 访问: /api/ 后端: /api/ 29 | // 访问: /api/xx 后端: /api/xx 30 | // 访问: /api/xx?aa 后端: /api/xx?aa 31 | // 访问: /api-xx?aa 后端: 32 | location /api/ { 33 | proxy_pass http://node:8080; 34 | } 35 | 36 | // 访问: /api/ 后端: /api/ 37 | // 访问: /api/xx 后端: /api/xx 38 | // 访问: /api/xx?aa 后端: /api/xx?aa 39 | // 访问: /api-xx?aa 后端: /api-xx?aa 40 | location /api { 41 | proxy_pass http://node:8080; 42 | } 43 | ``` 44 | 45 | ## url 包含路径 46 | 47 | 注意,这里的路径哪怕只是一个 `/` 也是存在的,如: 48 | 49 | - `http://host` - x 50 | - `https//host/` - √ 51 | - `http://host:port` - x 52 | - `https://host:port/` - √ 53 | - `http://host/api` - √ 54 | - `http://host/api/` - √ 55 | 56 | 当 `proxy_pass url` 的 `url` 包含路径时,匹配时会根据 `location` 的匹配后的链接透传给 `url` ,注意匹配后就是这样: 57 | 58 | | `location` 规则 | 访问的原始链接 | 匹配之后的路径 | 59 | | --- | --- | --- | 60 | | `location /` | `/` | ` ` | 61 | | `location /` | `/a` | `a` | 62 | | `location /` | `/a/b/c?d` | `a/b/c?d` | 63 | | `location /a/` | `/a/` | ` ` | 64 | | `location /a/` | `/a/b/c?d` | `b/c?d` | 65 | 66 | 明白匹配之后的路径后,在 `proxy_pass url` 包含路径时,将会把匹配之后的路径透传给 `url` ,如: 67 | 68 | 69 | ```nginx 70 | // 访问: / 后端: / 71 | // 访问: /api/xx 后端: /api/xx 72 | // 访问: /api/xx?aa 后端: /api/xx?aa 73 | location / { 74 | proxy_pass http://node:8080/; 75 | } 76 | 77 | // 访问: /api/ 后端: / 78 | // 访问: /api/xx 后端: /xx 79 | // 访问: /api/xx?aa 后端: /xx?aa 80 | // 访问: /api-xx?aa 未匹配 81 | location /api/ { 82 | proxy_pass http://node:8080/; 83 | } 84 | 85 | // 访问: /api 后端: / 86 | // 访问: /api/ 后端: // 87 | // 访问: /api/xx 后端: //xx 88 | // 访问: /api/xx?aa 后端: //xx?aa 89 | // 访问: /api-xx?aa 后端: /-xx?aa 90 | location /api { 91 | proxy_pass http://node:8080/; 92 | } 93 | 94 | // 访问: /api/ 后端: /v1 95 | // 访问: /api/xx 后端: /v1xx 96 | // 访问: /api/xx?aa 后端: /v1xx 97 | // 访问: /api-xx?aa 未匹配 98 | location /api/ { 99 | proxy_pass http://node:8080/v1; 100 | } 101 | 102 | // 访问: /api/ 后端: /v1/ 103 | // 访问: /api/xx 后端: /v1/xx 104 | // 访问: /api/xx?aa 后端: /v1/xx 105 | // 访问: /api-xx?aa 未匹配 106 | location /api/ { 107 | proxy_pass http://node:8080/v1/; 108 | } 109 | ``` 110 | 111 | 由以上规则可以看出,当 `proxy_pass url` 中包含路径时,结尾的 `/` 最好同 `location` 匹配规则一致。 112 | 113 | ## 当 proxy_pass 遇到正则 114 | 115 | 当 `location` 以正则形式匹配时,`proxy_pass` 就不能以 `/` 结束了,也就是不能包含路径了,比如错误的: 116 | 117 | ```nginx 118 | location ~* ^/api/ { 119 | proxy_pass http://host/; 120 | } 121 | 122 | location / { 123 | if ($uri ~* ^/api/) { 124 | proxy_pass http://host/; 125 | } 126 | } 127 | ``` 128 | 129 | 解决办法就是把链接中的路径去掉。 130 | 131 | ## 重写代理链接 - url rewrite 132 | 133 | 当原始链接(浏览器访问的链接)和代理服务器链接规则不一致时,可以使用 Nginx URL Rewrite 功能去动态的重写,如: 134 | 135 | ```nginx 136 | location ~* ^/api/ { 137 | rewrite ^/api/(.*) /?path=$1 break; 138 | proxy_pass http://node:8080; 139 | } 140 | ``` 141 | 142 | 以上请求会把匹配 `/api/` 的链接重写为 `/?path=` 的链接透传给 `node:8080` 服务,有意思的是当使用 `rewrite` 指令并且生效后,`proxy_pass url` 链接中的路径会被忽略,如: 143 | 144 | ```nginx 145 | // 访问: / 后端: /node/ 146 | // 访问: /api 后端: /node/api 147 | // 访问: /api/ 后端: /?path= 148 | // 访问: /api/a/b/c 后端: /?path=a/b/c 149 | location / { 150 | rewrite ^/api/(.*) /?path=$1 break; 151 | proxy_pass http://node:8080/node/; 152 | } 153 | ``` -------------------------------------------------------------------------------- /docs/example/split-log.md: -------------------------------------------------------------------------------- 1 | # 日志切割 - Shell 2 | 3 | 由于 Nginx 的日志(包括 access 和 error)默认都是存放在一个文件夹内,当时间长了,日志文件体积越来越大,不易管理,可以自己写个脚本来处理,其实就是定时定点去把日志移动下位置。 4 | 5 | ```bash 6 | #!/bin/sh 7 | 8 | # 日志源目录 9 | sourceDir='/var/log/nginx/xuexb.com/last' 10 | 11 | # 日志备份目录 12 | backDir='/var/log/nginx/xuexb.com/back' 13 | 14 | echo "split-logs start: $(date +%Y-%m-%d %H:%M:%S)" 15 | 16 | ls $sourceDir | while read filename 17 | do 18 | mkdir -p "$backDir/$(date +%Y%m%d)/" 19 | mv "$sourceDir/$filename" "$backDir/$(date +%Y%m%d)/" 20 | echo "$sourceDir/$filename => $backDir/$(date +%Y%m%d)/$filename" 21 | done 22 | 23 | # 刷新nginx 24 | kill -USR1 `cat /var/run/nginx.pid` 25 | 26 | echo "split-logs end: $(date +%Y-%m-%d %H:%M:%S)" 27 | echo "----------------" 28 | ``` 29 | 30 | 有几个注意的: 31 | 32 | 1. 日志源目录 - 该目录存放日志文件,如 access.log,error.log,当然如果你的站点比较多,可以用域名来命名,比如: xuexb.com.access.log,xuexb.com.error.log 。 33 | 2. 日志备份目录 - 会以`Ymd`命名文件夹名存放在该目录。 34 | 3. 刷新 Nginx - 由于是直接移动日志文件,移动后需要刷新下 Nginx 使其再生成日志文件,当然也可以 `nginx -s reload` ,但 `reload` 要比 `kill` 的慢,而 `kill -USR1` 是无损的。 35 | 4. 定时任务 - 需要使用 `root` 帐户下使用 `crontab -e` 插入一条定时任务,定时 23:55 开始分割,后面是把错误和信息输出到指定文件,方便调试,如: 36 | 37 | ``` 38 | 55 23 * * * sh /你的目录/split-log >> /var/log/nginx/crontab.log 2>&1 39 | ``` 40 | 41 | --- 42 | 43 | 当然这只是日志切割的一种,还有很多种方法~ -------------------------------------------------------------------------------- /docs/example/www-permanent.md: -------------------------------------------------------------------------------- 1 | # 主域301重定向 2 | 3 | 你的网站可能有多个域名访问,比如:`www.xuexb.com`、`xuexb.com` 等,设置主域意思是不管用户输入哪个域名,都会 `301` 重定向到主域上,设置主域可以对 SEO 更友好,比如: 4 | 5 | > 以xuexb.com为主域 6 | 7 | ``` 8 | www.xuexb.com => xuexb.com 9 | www.xuexb.com/search/xxoo => xuexb.com/search/xxoo 10 | www.xuexb.com/a/b/c/404.html => xuexb.com/a/b/c/404.html 11 | ``` 12 | 13 | 配置文件核心: 14 | 15 | ```nginx 16 | server { 17 | # 设置多个域名 18 | server_name www.xuexb.com xuexb.com; 19 | 20 | # 判断host是不是xuexb.com,如果不是则直接301重定向,permanent表示301 21 | if ( $host != 'xuexb.com' ){ 22 | rewrite ^/(.*)$ http://xuexb.com/$1 permanent; 23 | } 24 | 25 | # 其他规则 26 | } 27 | ``` -------------------------------------------------------------------------------- /docs/guide/README.md: -------------------------------------------------------------------------------- 1 | # Nginx 简介 2 | 3 | Nginx(发音同engine x)是一个异步框架的 Web 服务器,也可以用作反向代理,负载平衡器 和 HTTP 缓存。该软件由 [Igor Sysoev](https://zh.wikipedia.org/wiki/%E4%BC%8A%E6%88%88%E7%88%BE%C2%B7%E8%B3%BD%E7%B4%A2%E8%80%B6%E5%A4%AB) 创建,并于2004年首次公开发布。同名公司成立于2011年,以提供支持。Nginx 是一款免费的开源软件,根据类 BSD 许可证的条款发布。一大部分Web服务器使用 Nginx ,通常作为负载均衡器。[[1]][wiki] 4 | 5 | ## Nginx的特点 [[2]][jianshu] 6 | 7 | - 更快: 8 | - 单次请求会得到更快的响应。 9 | - 在高并发环境下,Nginx 比其他 WEB 服务器有更快的响应。 10 | - 高扩展性: 11 | - Nginx 是基于模块化设计,由多个耦合度极低的模块组成,因此具有很高的扩展性。许多高流量的网站都倾向于开发符合自己业务特性的定制模块。 12 | - 高可靠性: 13 | - Nginx 的可靠性来自于其核心框架代码的优秀设计,模块设计的简单性。另外,官方提供的常用模块都非常稳定,每个 worker 进程相对独立,master 进程在一个 worker 进程出错时可以快速拉起新的 worker 子进程提供服务。 14 | - 低内存消耗: 15 | - 一般情况下,10000个非活跃的 `HTTP Keep-Alive` 连接在 Nginx 中仅消耗 `2.5MB` 的内存,这是 Nginx 支持高并发连接的基础。 16 | - 单机支持10万以上的并发连接:**理论上,Nginx 支持的并发连接上限取决于内存,10万远未封顶。** 17 | - 热部署: 18 | - master 进程与 worker 进程的分离设计,使得 Nginx 能够提供热部署功能,即在 7x24 小时不间断服务的前提下,升级 Nginx 的可执行文件。当然,它也支持不停止服务就更新配置项,更换日志文件等功能。 19 | - 最自由的 BSD 许可协议: 20 | - 这是 Nginx 可以快速发展的强大动力。BSD 许可协议不只是允许用户免费使用 Nginx ,它还允许用户在自己的项目中直接使用或修改 Nginx 源码,然后发布。 21 | 22 | [wiki]:https://zh.wikipedia.org/wiki/Nginx "wiki" 23 | [jianshu]:https://www.jianshu.com/p/99d50fcc5cd6 "简书" 24 | 25 | ## 你可能需要掌握的 26 | 27 | - Linux 服务器和一些常用的操作命令 28 | - 域名,当然如果是本地玩玩也可以是 Hosts 29 | - 基本的正则表达式 30 | 31 | ## 声明 32 | 33 | 本项目部分内容是来自网络资源,如有侵权请联系 `fe.xiaowu@gmail.com` 。 34 | 35 | ## 相关链接 36 | 37 | - [Nginx 官网](http://nginx.org/) 38 | -------------------------------------------------------------------------------- /docs/guide/dir.md: -------------------------------------------------------------------------------- 1 | # Nginx 目录建议 2 | 3 | [安装 Nginx](./linux-make.md) 时可以选择配置文件的路径,由于 Nginx 程序后续可能会升级版本,但配置基本上就是一份,那么推荐使用配置和程序分离的方式,遵循: 4 | 5 | 1. 配置文件独立管理, 不存放在nginx程序目录内 6 | 1. 每个站点独立一个配置文件 7 | 2. 每个站点独立的日志文件 8 | 3. 提取公用的配置文件 9 | 10 | 如: 11 | 12 | ``` 13 | $dir/wwwroot/ - 网站根目录,以域名为文件夹名称 14 | ./xuexb.com/ 15 | ./static.xuexb.com/ 16 | 17 | $dir/src/ - 安装源包 18 | 19 | $dir/local/nginx/ - nginx相关根目录 20 | ./conf/ - 配置文件 21 | ./nginx.conf - 配置主入口 22 | ./inc - 通用配置 23 | ./vhost/ - 各站点的配置,以 `域名.conf` 命名 24 | ./xuexb.com.conf 25 | ./static.xuexb.com.conf 26 | 27 | ./1.11.1/ - 各个版本的nginx 28 | ./1.11.2/ 29 | 30 | $dir/logs/ - 日志相关目录,内以 `域名.type.log` 命名 31 | ./last/ - 最新的日志 32 | ./xuexb.com.error.log 33 | ./xuexb.com.access.log 34 | ./back/ - 天级备份日志 35 | ./20170908/ 36 | ``` 37 | 38 | 这样分离之后不管是 Nginx 主程序版本升级,还是修改某个站点配置,还是快速查找某个站点日志都是得心应手~ 39 | -------------------------------------------------------------------------------- /docs/guide/error.md: -------------------------------------------------------------------------------- 1 | # 常见错误 2 | 3 | ### 配置不生效 4 | 5 | 更新配置后使用 `[sudo] /your/path/nginx -s reload` 热重载服务。 6 | 7 | ### nginx: [emerg] getpwnam("nginx") failed 8 | 9 | 表示该用户 `nginx` 不存在, 解决方法: 10 | 11 | 1. 在 `nginx.conf` 里添加 `user nobody;` 12 | 2. 创建用户和用户对应的分组 13 | 14 | ### nginx: [emerg] getgrnam("xiaowu") failed 15 | 16 | 表示用户分组不存在,解决方法: 17 | 18 | 1. 在 `nginx.conf` 里添加 `user nobody;` 19 | 2. 创建用户对应的分组 20 | 21 | ### nginx: [alert] could not open error log file: open() "/logs/error.log" failed (13: Permission denied) 22 | 23 | 启动 Nginx 的用户权限不够导致无法写入日志文件,常见于非 `root` 用户启动报错。 24 | 25 | ### nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use) 26 | 27 | 80端口被占用启动失败,修改端口或者杀死占用者再启动即可。 28 | 29 | ### nginx: [error] open() "nginx.pid" failed (2: No such file or directory) 30 | 31 | pid 进程 id 文件不存在,可能文件被删除或者已经停止,在停止 Nginx 时会使用该进程id,如果不存在将失败,可以手动 kill 掉。 32 | 33 | ### nginx: [emerg] unknown "realpath_roots_xxx" variable 34 | 35 | 变量 `$realpath_roots_xxx` 不存在 36 | 37 | ### nginx: [emerg] "add_header" directive is not allowed here in xx 38 | 39 | `add_header` 指令不能直接在 `if` 判断内,可以在 `http`、`server`、`server.location`、`server.location.if` 下。 40 | 41 | ### nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in xx 42 | 43 | 这是 `proxy_pass` 指令在正则匹配时不能使用包含路径的链接,如以下都会报错: 44 | 45 | ```nginx 46 | location ~* ^/api/ { 47 | proxy_pass http://host/; 48 | } 49 | 50 | location / { 51 | if ($uri ~* ^/api/) { 52 | proxy_pass http://host/; 53 | } 54 | } 55 | ``` 56 | 57 | 解决办法就是把 `/` 去掉,更多关于 `proxy_pass` 请看:[`proxy_pass` 技巧](/example/proxy_pass.html) 。 -------------------------------------------------------------------------------- /docs/guide/linux-install.md: -------------------------------------------------------------------------------- 1 | # Linux 中编译安装和配置 Nginx 2 | 3 | > 注意:本示例在 Centos 6.5 、Centos 7.3 中运行。 4 | 5 | ## 下载安装包并解压 6 | 7 | ```bash 8 | # 进入约定的安装包目录 9 | cd /约定目录/src/ 10 | 11 | # 下载nginx安装文件,这里以 nginx-1.11.1 为例,其他的nginx包可以去官网查找 12 | wget http://nginx.org/download/nginx-1.11.1.tar.gz 13 | 14 | # 把安装包解压,会自动解压到 /约定目/src/nginx-1.11.1/ 文件夹里 15 | tar xzf nginx-1.11.1.tar.gz 16 | ``` 17 | 18 | ## nginx的配置文件 19 | 20 | 其实完全可以使用 Nginx 的默认配置文件,默认配置文件编译后的地址在 `程序目录/conf/nginx.conf` ,但如果你会频繁的更新 Nginx 版本的话配置文件最后"分离",类似前后端分离一样,这样可以少"吵架"(解耦合),我们单独的把 Nginx 的所有配置,包括所有站点的配置、SSL 证书都放在 `/约定目录/nginx-conf/` 目录里,如: 21 | 22 | ``` 23 | /约定目录/nginx-conf/ 24 | 25 | # nginx配置文件 26 | ./nginx.conf 27 | 28 | # nginx的其他配置 29 | ./mime.types 30 | 31 | # 站点配置 32 | ./conf/ 33 | # 各个子站点目录 34 | ./www.xxoo.com.conf 35 | ./www.a.com.conf 36 | ... 37 | ``` 38 | 39 | `/约定目录/nginx-conf/conf/` 目录里存放以网站为单位的配置文件,文件名以网站域名命名,这样可以配置分离,互不影响,而且好定位问题。 40 | 41 | ## 配置nginx安装参数 42 | 43 | ```shell 44 | # 创建对应版本的程序目录,这个目录用来存放编译后的文件 45 | mkdir -p /约定目录/local/nginx-1.11.1/ 46 | 47 | # 进入安装包解压后的目录 48 | cd /约定目录/src/nginx-1.11.1/ 49 | 50 | # 开始配置 51 | ./configure --prefix=/home/local/nginx-1.11.1 --conf-path=/home/local/nginx-conf/vhost/nginx.conf --with-http_ssl_module --with-http_realip_module --with-http_dav_module --with-http_gzip_static_module --with-http_v2_module 52 | ``` 53 | 54 | 其他的安装编译配置可点击:[Nginx 编译参数](http://www.ttlsa.com/nginx/nginx-configure-descriptions/) 55 | 56 | ### 编译nginx 57 | 58 | ``` 59 | # 开始编译并安装,可能需要sudo权限 60 | make 61 | [sudo] make install 62 | ``` 63 | 64 | ## 验证是否安装成功 65 | 66 | ``` 67 | # 进入nginx执行目录 68 | cd /约定目录/local/nginx-1.11.1/sbin/ 69 | ``` 70 | 71 | 运行 `./nginx -v` 查看版本,结果如: 72 | 73 | ``` 74 | nginx version: nginx/1.11.1 75 | ``` 76 | 77 | 如果你有开启 `http_ssl_module` ,可运行:`./nginx -V` 查看是否支持,结果如: 78 | 79 | ``` 80 | nginx version: nginx/1.11.1 81 | built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC) 82 | built with OpenSSL 1.0.1e-fips 11 Feb 2013 83 | TLS SNI support enabled 84 | ... 85 | ``` 86 | 87 | ## 重新编译安装 88 | 89 | 是指已经安装成功, 但想对 Nginx 添加个模块或者修改配置时需要重新编译。 90 | 91 | ### 1. 下载对应版本 (如果不想更新版本可以忽略) 92 | 93 | 可以使用 `nginx -V` 查看当前程序的编译参数 94 | 95 | ```bash 96 | # 下载 97 | 98 | wget http://nginx.org/download/nginx-1.11.13.tar.gz 99 | 100 | # 解压 101 | tar xzf nginx-1.11.13.tar.gz 102 | ``` 103 | 104 | ### 2. 重新编译 105 | 106 | ```bash 107 | # 配置 108 | ./configure --prefix=xxx 你的新参数 109 | 110 | # 编译 111 | [sudo] make 112 | 这时侯切记不要make install, 因为make install就会把文件复制到安装目录 113 | ``` 114 | 115 | ### 3. 复制程序 116 | 117 | ```bash 118 | # 新复制老版本, 以防出错 119 | cp /path/nginx /path/nginx.back 120 | 121 | # 停止服务 122 | /path/nginx -s stop 123 | 124 | # 复制新版本到安装目录 125 | cp objs/nginx /path/nginx 126 | 127 | # 查看新版本 128 | /path/nginx -t 129 | 130 | # 启动新版本 131 | /path/nginx 132 | ``` 133 | 134 | 注意:如果在运行中直接覆盖 Nginx 会报: `cp: 无法创建普通文件"nginx": 文本文件忙` 。 -------------------------------------------------------------------------------- /docs/guide/nginx-configure-descriptions.md: -------------------------------------------------------------------------------- 1 | # Nginx 编译参数 2 | 3 | ## 命令 4 | 5 | - `./sbin/nginx -V` - 查看当前已安装版本的编译参数 6 | - `src/nginx-x.x.x/configure --help` - 显示提示信息 7 | 8 | ## 参数详情 9 | 10 | 参数名称 | 备注 11 | --- | --- 12 | `--prefix=value` | 指向安装目录 13 | `--sbin-path` | 指向(执行)程序文件(`nginx`) 14 | `--conf-path=` | 指向配置文件(`nginx.conf`) 15 | `--error-log-path=` | 指向错误日志目录 16 | `--pid-path=` | 指向 `pid` 文件(`nginx.pid`) 17 | `--lock-path=` | 指向 `lock` 文件(`nginx.lock`)(安装文件锁定,防止安装文件被别人利用,或自己误操作。)进程ID文件 18 | `--user=` | 指定程序运行时的用户名 19 | `--group=` | 指定程序运行时的用户组名 20 | `--builddir=` | 指向编译目录 21 | `--with-rtsig_module` | 启用 rtsig 模块支持(实时信号) 22 | `--with-select_module` | 启用 select 模块支持(一种轮询模式,不推荐在高载环境下使用)禁用:`--without-select_module` 23 | `--with-poll_module` | 启用 poll 模块支持(功能与 select 相同,与 select 特性相同,为一种轮询模式,不推荐在高载环境下使用) 24 | `--with-file-aio` | 启用 file aio 支持(一种 APL 文件传输格式) 25 | `--with-ipv6` | 启用 ipv6 支持 26 | `--add-module=` | 启用外部模块支持 27 | `--with-cc=` | 指向 C 编译器路径 28 | `--with-cpp=` | 指向 C 预处理路径 29 | `--with-cc-opt=` | 设置 C 编译器参数 30 | `--with-ld-opt=` | 设置连接文件参数 31 | `--with-cpu-opt=` | 指定编译的 CPU,可用的值为:pentium, pentiumpro, pentium3, pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64 32 | `--without-pcre` | 禁用 PCRE 库 33 | `--with-pcre` | 启用 PCRE 库 34 | `--with-pcre=` | 指向 PCRE 库文件目录 35 | `--with-pcre-opt=` | 在编译时为 PCRE 库设置附加参数 36 | `--with-md5=` | 指向 MD5 库文件目录(消息摘要算法第五版,用以提供消息的完整性保护) 37 | `--with-md5-opt=` | 在编译时为 MD5 库设置附加参数 38 | `--with-md5-asm` | 使用 MD5 汇编源 39 | `--with-sha1=` | 指向 sha1 库目录(数字签名算法,主要用于数字签名) 40 | `--with-sha1-opt=` | 在编译时为 sha1 库设置附加参数 41 | `--with-sha1-asm` | 使用 sha1 汇编源 42 | `--with-perl=` | 设定 perl 库文件路径 43 | `--with-zlib=` | 指向 zlib 库目录 44 | `--with-zlib-opt=` | 在编译时为 zlib 设置附加参数 45 | `--with-zlib-asm=` | 为指定的 CPU 使用 zlib 汇编源进行优化,CPU 类型为 pentium, pentiumpro 46 | `--with-libatomic` | 为原子内存的更新操作的实现提供一个架构 47 | `--with-libatomic=` | 指向 libatomic_ops 安装目录 48 | `--with-openssl=` | 指向 openssl 安装目录 49 | `--with-openssl-opt` | 在编译时为 openssl 设置附加参数 50 | `--with-debug` | 启用 debug 日志 51 | `--with-http_ssl_module` | 启用 ngx_http_ssl_module 支持(使支持 HTTPS 请求,需已安装 openssl) 52 | `--with-http_realip_module` | 启用 ngx_http_realip_module 支持(这个模块允许从请求标头更改客户端的 IP 地址值,默认为关) 53 | `--with-http_addition_module` | 启用 ngx_http_addition_module 支持(作为一个输出过滤器,支持不完全缓冲,分部分响应请求) 54 | `--with-http_xslt_module` | 启用 ngx_http_xslt_module 支持(过滤转换 XML 请求) 55 | `--with-http_image_filter_module` | 启用 ngx_http_image_filter_module 支持(传输 JPEG/GIF/PNG 图片的一个过滤器)(默认为不启用。GD 库要用到) 56 | `--with-http_geoip_module` | 启用 ngx_http_geoip_module 支持(该模块创建基于与 MaxMind GeoIP 二进制文件相配的客户端 IP 地址的 ngx_http_geoip_module 变量) 57 | `--with-http_sub_module` | 启用 ngx_http_sub_module 支持(允许用一些其他文本替换 Nginx 响应中的一些文本) 58 | `--with-http_dav_module` | 启用 ngx_http_dav_module 支持(增加 PUT、DELETE、MKCOL 创建集合,COPY 和 MOVE 方法)默认情况下为关闭,需编译开启 59 | `--with-http_flv_module` | 启用 ngx_http_flv_module 支持(提供寻求内存使用基于时间的偏移量文件) 60 | `--with-http_gzip_static_module` | 启用 ngx_http_gzip_static_module 支持(在线实时压缩输出数据流) 61 | `--with-http_random_index_module` | 启用 `ngx_http_random_index_module` 支持(从目录中随机挑选一个目录索引) 62 | `--with-http_secure_link_module` | 启用 ngx_http_secure_link_module 支持(计算和检查要求所需的安全链接网址) 63 | `--with-http_degradation_module` | 启用 ngx_http_degradation_module 支持(允许在内存不足的情况下返回204或444码) 64 | `--with-http_stub_status_module` | 启用 ngx_http_stub_status_module 支持(获取 Nginx 自上次启动以来的工作状态) 65 | `--without-http_charset_module` | 禁用 ngx_http_charset_module 支持(重新编码 WEB 页面,但只能是一个方向--服务器端到客户端,并且只有一个字节的编码可以被重新编码) 66 | `--without-http_gzip_module` | 禁用 ngx_http_gzip_module 支持(该模块同 `--with-http_gzip_static_module` 功能一样) 67 | `--without-http_ssi_module` | 禁用 ngx_http_ssi_module 支持(该模块提供了一个在输入端处理处理服务器包含文件(SSI)的过滤器,目前支持 SSI 命令的列表是不完整的) 68 | `--without-http_userid_module` | 禁用 ngx_http_userid_module 支持(该模块用来处理用来确定客户端后续请求的 `cookie` ) 69 | `--without-http_access_module` | 禁用 ngx_http_access_module 支持(该模块提供了一个简单的基于主机的访问控制。允许/拒绝基于 IP 地址) 70 | `--without-http_auth_basic_module` | 禁用 ngx_http_auth_basic_module(该模块是可以使用用户名和密码基于 HTTP 基本认证方法来保护你的站点或其部分内容) 71 | `--without-http_autoindex_module` | 禁用 ngx_http_autoindex_module 支持(该模块用于自动生成目录列表,只在 `ngx_http_index_module` 模块未找到索引文件时发出请求。) 72 | `--without-http_geo_module` | 禁用 ngx_http_geo_module 支持(创建一些变量,其值依赖于客户端的IP地址) 73 | `--without-http_map_module` | 禁用 ngx_http_map_module 支持(使用任意的键/值对设置配置变量) 74 | `--without-http_split_clients_module` | 禁用 ngx_http_split_clients_module 支持(该模块用来基于某些条件划分用户。条件如:ip地址、报头、cookies等等) 75 | `--without-http_referer_module` | 禁用 ngx_http_referer_module支持(该模块用来过滤请求,拒绝报头中 Referer 值不正确的请求) 76 | `--without-http_rewrite_module` | 禁用 ngx_http_rewrite_module ,链接重写 77 | `--without-http_proxy_module` | 禁用 ngx_http_proxy_module 支持(有关代理服务器) 78 | `--without-http_fastcgi_module` | 禁用 ngx_http_fastcgi_module 支持(该模块允许 Nginx 与 FastCGI 进程交互,并通过传递参数来控制 FastCGI 进程工作。 )FastCGI 一个常驻型的公共网关接口。 79 | `--without-http_uwsgi_module` | 禁用 ngx_http_uwsgi_module 支持(该模块用来医用uwsgi协议,uWSGI服务器相关) 80 | `--without-http_scgi_module` | 禁用 ngx_http_scgi_module支持 81 | `--without-http_memcached_module` | 禁用 ngx_http_memcached_module 支持(该模块用来提供简单的缓存,以提高系统效率) 82 | `-without-http_limit_zone_module` | 禁用 ngx_http_limit_zone_module 支持(该模块可以针对条件,进行会话的并发连接数控制) 83 | `--without-http_limit_req_module` | 禁用 ngx_http_limit_req_module 支持(该模块允许你对于一个地址进行请求数量的限制用一个给定的session或一个特定的事件) 84 | `--without-http_empty_gif_module` | 禁用 `ngx_http_empty_gif_module` 支持(该模块在内存中常驻了一个1*1的透明GIF图像,可以被非常快速的调用) 85 | `--without-http_browser_module` | 禁用 ngx_http_browser_module 支持 86 | `--without-http_upstream_ip_hash_module` | 禁用 ngx_http_upstream_ip_hash_module 支持(该模块用于简单的负载均衡) 87 | `--with-http_perl_module` | 启用 ngx_http_perl_module 支持(该模块使nginx可以直接使用perl或通过ssi调用perl) 88 | `--with-perl_modules_path=` | 设定 perl 模块路径 89 | `--http-log-path=` | 设定 access log 路径 90 | `--http-client-body-temp-path=` | 设定 HTTP 客户端请求临时文件路径 91 | `--http-proxy-temp-path=` | 设定 HTTP 代理临时文件路径 92 | `--http-fastcgi-temp-path=` | 设定 HTTP Fastcgi 临时文件路径 93 | `--http-uwsgi-temp-path=` | 设定 HTTP uwsgi 临时文件路径 94 | `--http-scgi-temp-path=` | 设定 HTTP scgi 临时文件路径 95 | `--without-http` | 禁用 HTTP server 功能 96 | `--without-http-cache` | 禁用 HTTP Cache 功能 97 | `--with-mail` | 启用 POP3/IMAP4/SMTP 代理模块支持 98 | `--with-mail_ssl_module` | 启用 ngx_mail_ssl_module 支持 99 | `--without-mail_pop3_module` | 禁用 POP3 协议 100 | `--without-mail_imap_module` | 禁用 IMAP 协议 101 | `--without-mail_smtp_module` | 禁用 SMTP 协议 102 | `--with-google_perftools_module` | 启用 ngx_google_perftools_module 支持(调试用,剖析程序性能瓶颈) 103 | `--with-cpp_test_module` | 启用 ngx_cpp_test_module 支持 104 | 105 | > 部分摘自: -------------------------------------------------------------------------------- /docs/variable/README.md: -------------------------------------------------------------------------------- 1 | # 变量 2 | 3 | 这里列出常用的 Nginx 全局变量,也可以点击 [实时查看](https://echo.xuexb.com/api/dump/path?a=1&%E4%B8%AD%E6%96%87=%E5%A5%BD%E7%9A%84#123) 浏览。 -------------------------------------------------------------------------------- /docs/variable/client.md: -------------------------------------------------------------------------------- 1 | --- 2 | pageClass: page-main-max 3 | --- 4 | 5 | # 客户端相关 6 | 7 | 变量名 | 备注 | 示例 8 | --- | --- | --- 9 | `http_accept` | 浏览器支持的 MIME 类型 | `text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8` 10 | `http_accept_encoding` | 浏览器支持的压缩编码 | `gzip, deflate, br` 11 | `http_accept_language` | 浏览器支持的语言 | `zh-CN,zh;q=0.9,en;q=0.8` 12 | `http_cache_control` | 浏览器缓存 | `max-age=0` 13 | `http_connection` | 客户端与服务连接类型 | 14 | `http_cookie` | 浏览器请求 cookie | `a=1; b=2` 15 | `http_host` | 浏览器请求 host | echo.xuexb.com 16 | `http_referer` | 浏览器来源 | https://echo.xuexb.com/ 17 | `http_upgrade_insecure_requests` | 是一个请求首部,用来向服务器端发送信号,表示客户端优先选择加密及带有身份验证的响应,并且它可以成功处理 upgrade-insecure-requests CSP 指令 | 1 18 | `http_user_agent` | 用户设备标识 | `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36` 19 | `http_x_requested_with` | 异步请求标识 | true 20 | `http_x_forwarded_for` | 反向代理原 IP | 198.13.61.105 -------------------------------------------------------------------------------- /docs/variable/server.md: -------------------------------------------------------------------------------- 1 | --- 2 | pageClass: page-main-max 3 | --- 4 | 5 | # 服务器相关 6 | 7 | 变量名 | 备注 | 示例 8 | --- | --- | --- 9 | `nginx_version` | 当前运行的 Nginx 版本号 | 1.11.2 10 | `server_port` | 服务器端口 | 8080 11 | `server_addr` | 服务器端地址 | 127.0.0.1 12 | `server_name` | 服务器名称 | 127.0.0.1 13 | `server_protocol` | 服务器的HTTP版本 | HTTP/1.0 14 | `status` | HTTP 响应代码 | 200 15 | `time_iso8601` | 服务器时间的 ISO 8610 格式 | 2018-09-02T15:14:27+08:00 16 | `time_local` | 服务器时间(LOG Format 格式) | 02/Sep/2018:15:14:27 +0800 17 | `document_root` | 当前请求的文档根目录或别名 | `/home/xiaowu/github/echo.xuexb.com` 18 | `request_filename` | 当前连接请求的文件路径,由 `root` 或 `alias` 指令与 URI 请求生成 | `/home/xiaowu/github/echo.xuexb.com/api/dump/path` 19 | `request_completion` | 如果请求成功,值为”OK”,如果请求未完成或者请求不是一个范围请求的最后一部分,则为空 | 20 | `pid` | 工作进程的PID | 1234 21 | `msec` | 当前的Unix时间戳 | 1535872750.954 22 | `limit_rate` | 用于设置响应的速度限制 | 0 23 | `pipe` | 如果请求来自管道通信,值为“p”,否则为“.” | . 24 | `connection_requests` | TCP连接当前的请求数量 | 1 25 | `connection` | TCP 连接的序列号 | 363861 26 | `realpath_root` | 当前请求的文档根目录或别名的真实路径,会将所有符号连接转换为真实路径 | /home/xiaowu/github/echo.xuexb.com 27 | `hostname` | 主机名 | bj01 28 | -------------------------------------------------------------------------------- /docs/variable/url.md: -------------------------------------------------------------------------------- 1 | --- 2 | pageClass: page-main-max 3 | --- 4 | 5 | # 链接相关 6 | 7 | 变量名 | 备注 | 示例 8 | --- | --- | --- 9 | `scheme` | 请求使用的 WEB 协议 | http 10 | `uri` | 请求中的当前 URI(不带请求参数),可以不同于浏览器传递的 `$request_uri` 的值,它可以通过内部重定向,或者使用 `index` 指令进行修改 | `/api/dump/path` 11 | `document_uri` | 同 `$uri` | `/api/dump/path` 12 | `request_uri` | 这个变量等于包含一些客户端请求参数的原始 URI ,它无法修改 | `/api/dump/path?a=1&%E4%B8%AD%E6%96%87=%E5%A5%BD%E7%9A%84` 13 | `request_method` | HTTP 请求方法 | GET 14 | `request_time` | 处理客户端请求使用的时间,从读取客户端的第一个字节开始计时 | 0.000 15 | `request_length` | 请求的长度(包括请求地址、请求头和请求主体) | 678 16 | `args` | 请求参数 | `a=1&%E4%B8%AD%E6%96%87=%E5%A5%BD%E7%9A%84` 17 | `query_string` | 同 `$args` | 18 | `is_args` | 请求中是否有参数,有则为 `?` 否则为空 | `?` 19 | `arg_参数名` | 请求中具体的参数 | `$arg_a` => `1` 20 | `https` | 如果开启了 SSL 安全模式,则为 `on` 否则为空 | `on` -------------------------------------------------------------------------------- /netlify.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 主要处理 VuePress 生成的文件,移动到 learn-nginx 二级目录中 3 | 4 | mkdir netlify 5 | mv dist learn-nginx 6 | mv learn-nginx netlify 7 | echo '' > netlify/index.html -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "learn-nginx", 3 | "version": "2.0.0", 4 | "description": "学习 Nginx 配置,包括: 编译安装、反向代理、重定向、重写、缓存、跨域配置等", 5 | "main": "index.js", 6 | "scripts": { 7 | "dev": "vuepress dev docs", 8 | "build": "vuepress build docs", 9 | "netlify": "npm run build && sh netlify.sh" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "git+https://github.com/xuexb/learn-nginx.git" 14 | }, 15 | "author": "xuexb ", 16 | "license": "MIT", 17 | "bugs": { 18 | "url": "https://github.com/xuexb/learn-nginx/issues" 19 | }, 20 | "homepage": "https://github.com/xuexb/learn-nginx#readme", 21 | "devDependencies": { 22 | "vuepress": "^1.2.0" 23 | } 24 | } 25 | --------------------------------------------------------------------------------