├── .github ├── FUNDING.yml └── workflows │ └── ci.yml ├── .gitignore ├── LICENSE ├── README.md ├── favicon.svg ├── idoc.yml ├── nginx.svg ├── package.json └── renovate.json /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | ko_fi: jaywcjlove 2 | buy_me_a_coffee: jaywcjlove 3 | custom: ["https://www.paypal.me/kennyiseeyou", "https://jaywcjlove.github.io/#/sponsor"] 4 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | push: 4 | branches: 5 | - master 6 | 7 | jobs: 8 | build: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - uses: actions/checkout@v4 12 | - uses: actions/setup-node@v4 13 | with: 14 | node-version: 20 15 | registry-url: 'https://registry.npmjs.org' 16 | 17 | - run: npm install 18 | - run: npm run build 19 | 20 | - name: Generate Contributors Images 21 | uses: jaywcjlove/github-action-contributors@main 22 | with: 23 | filter-author: (renovate\[bot\]|renovate-bot|dependabot\[bot\]) 24 | output: dist/CONTRIBUTORS.svg 25 | avatarSize: 42 26 | 27 | - name: Create Tag 28 | id: create_tag 29 | uses: jaywcjlove/create-tag-action@main 30 | with: 31 | package-path: ./package.json 32 | 33 | - name: Deploy Website 34 | uses: peaceiris/actions-gh-pages@v4 35 | with: 36 | user_name: 'github-actions[bot]' 37 | user_email: 'github-actions[bot]@users.noreply.github.com' 38 | commit_message: ${{ github.event.head_commit.message }} 39 | github_token: ${{ secrets.GITHUB_TOKEN }} 40 | publish_dir: ./dist 41 | 42 | - name: Generate Changelog 43 | id: changelog 44 | uses: jaywcjlove/changelog-generator@main 45 | with: 46 | token: ${{ secrets.GITHUB_TOKEN }} 47 | filter-author: (jaywcjlove|小弟调调™|dependabot\[bot\]|Renovate Bot) 48 | filter: (^[\s]+?[R|r]elease)|(^[R|r]elease) 49 | 50 | - name: Create Release 51 | uses: ncipollo/release-action@v1 52 | if: steps.create_tag.outputs.successful 53 | with: 54 | allowUpdates: true 55 | token: ${{ secrets.GITHUB_TOKEN }} 56 | name: ${{ steps.create_tag.outputs.version }} 57 | tag: ${{ steps.create_tag.outputs.version }} 58 | body: | 59 | [![Buy me a coffee](https://img.shields.io/badge/Buy%20me%20a%20coffee-048754?logo=buymeacoffee)](https://jaywcjlove.github.io/#/sponsor) 60 | 61 | Documentation ${{ steps.changelog.outputs.tag }}: https://raw.githack.com/jaywcjlove/vim-web/${{ steps.changelog.outputs.gh-pages-short-hash }}/index.html 62 | Comparing Changes: ${{ steps.changelog.outputs.compareurl }} 63 | 64 | ${{ steps.changelog.outputs.changelog }} -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | lib 2 | dist 3 | node_modules 4 | coverage 5 | test/out 6 | 7 | npm-debug.log* 8 | lerna-debug.log 9 | yarn-error.log 10 | package-lock.json 11 | 12 | .DS_Store 13 | .cache 14 | .vscode 15 | .idea 16 | 17 | *.bak 18 | *.tem 19 | *.temp 20 | #.swp 21 | *.*~ 22 | ~*.* 23 | 24 | # IDEA 25 | *.iml 26 | *.ipr 27 | *.iws 28 | .idea/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 小弟调调™ 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 |

2 | 3 | 4 | 5 | 6 |

7 | 8 | 9 |

Nginx 入门学习笔记

10 | 11 | 12 | [![Buy me a coffee](https://img.shields.io/badge/Buy%20me%20a%20coffee-048754?logo=buymeacoffee)](https://jaywcjlove.github.io/#/sponsor) 13 | 14 | [Nginx](https://nginx.org/en/) 是一款面向性能设计的 HTTP 服务器,能反向代理 HTTP、HTTPS 和邮件相关(SMTP、POP3、IMAP)的协议链接,并且提供了负载均衡以及 HTTP 缓存。它的设计充分使用异步事件模型,削减上下文调度的开销,提高服务器并发能力。采用了模块化设计,提供了丰富的第三方模块。 15 | 16 | 关于 Nginx,有这些标签:「异步」「事件」「模块化」「高性能」「高并发」「反向代理」「负载均衡」 17 | 18 | Linux系统:`Centos 7 x64` 19 | Nginx版本:`1.11.5` 20 | 21 | 22 | 23 | ## 目录 24 | 25 | - [安装](#安装) 26 | - [安装依赖](#安装依赖) 27 | - [下载](#下载) 28 | - [编译安装](#编译安装) 29 | - [Nginx 测试](#nginx-测试) 30 | - [设置全局 nginx 命令](#设置全局-nginx-命令) 31 | - [Mac 安装](#mac-安装) 32 | - [安装nginx](#安装nginx) 33 | - [启动服务](#启动服务) 34 | - [开机自启动](#开机自启动) 35 | - [运维](#运维) 36 | - [服务管理](#服务管理) 37 | - [重启服务防火墙报错解决](#重启服务防火墙报错解决) 38 | - [Nginx 卸载](#nginx-卸载) 39 | - [参数说明](#参数说明) 40 | - [配置](#配置) 41 | - [常用正则](#常用正则) 42 | - [全局变量](#全局变量) 43 | - [符号参考](#符号参考) 44 | - [配置文件](#配置文件) 45 | - [内置预定义变量](#内置预定义变量) 46 | - [反向代理](#反向代理) 47 | - [负载均衡](#负载均衡) 48 | - [RR](#rr) 49 | - [权重](#权重) 50 | - [ip_hash](#ip_hash) 51 | - [fair](#fair) 52 | - [url_hash](#url_hash) 53 | - [屏蔽ip](#屏蔽ip) 54 | - [第三方模块安装方法](#第三方模块安装方法) 55 | - [重定向](#重定向) 56 | - [重定向整个网站](#重定向整个网站) 57 | - [重定向单页](#重定向单页) 58 | - [重定向整个子路径](#重定向整个子路径) 59 | - [性能](#性能) 60 | - [内容缓存](#内容缓存) 61 | - [Gzip压缩](#gzip压缩) 62 | - [打开文件缓存](#打开文件缓存) 63 | - [SSL缓存](#ssl缓存) 64 | - [上游Keepalive](#上游keepalive) 65 | - [监控](#监控) 66 | - [常见使用场景](#常见使用场景) 67 | - [跨域问题](#跨域问题) 68 | - [跳转到带www的域上面](#跳转到带www的域上面) 69 | - [代理转发](#代理转发) 70 | - [监控状态信息](#监控状态信息) 71 | - [代理转发连接替换](#代理转发连接替换) 72 | - [SSL 配置](#ssl-配置) 73 | - [强制将http重定向到https](#强制将http重定向到https) 74 | - [两个虚拟主机](#两个虚拟主机) 75 | - [虚拟主机标准配置](#虚拟主机标准配置) 76 | - [爬虫过滤](#爬虫过滤) 77 | - [防盗链](#防盗链) 78 | - [虚拟目录配置](#虚拟目录配置) 79 | - [防盗图配置](#防盗图配置) 80 | - [屏蔽.git等文件](#屏蔽git等文件) 81 | - [域名路径加不加需要都能正常访问](#域名路径加不加需要都能正常访问) 82 | - [cockpit](#cockpit) 83 | - [错误问题](#错误问题) 84 | - [精品文章参考](#精品文章参考) 85 | 86 | 87 | 88 | ## 安装 89 | 90 | ### 安装依赖 91 | 92 | > prce(重定向支持)和 openssl(https 支持,如果不需要 https 可以不安装)。 93 | 94 | ```bash 95 | yum install -y pcre-devel 96 | yum -y install gcc make gcc-c++ wget 97 | yum -y install openssl openssl-devel 98 | ``` 99 | 100 | 在 CentOS 6.5 上安装时,我选择了“基本服务器”,默认情况下这两个包都没有安装全,所以需要运行上述命令进行安装。 101 | 102 | ### 下载 103 | 104 | [nginx 的所有版本在这里](http://nginx.org/download/) 105 | 106 | ```bash 107 | wget http://nginx.org/download/nginx-1.13.3.tar.gz 108 | wget http://nginx.org/download/nginx-1.13.7.tar.gz 109 | 110 | # 如果没有安装 wget 111 | $ yum install wget 112 | 113 | # 解压压缩包 114 | tar zxf nginx-1.13.3.tar.gz 115 | ``` 116 | 117 | ### 编译安装 118 | 119 | 然后进入目录编译安装,[configure参数说明](#参数说明) 120 | 121 | ```bash 122 | cd nginx-1.11.5 123 | ./configure 124 | 125 | .... 126 | Configuration summary 127 | + using system PCRE library 128 | + OpenSSL library is not used 129 | + using system zlib library 130 | 131 | nginx path prefix: "/usr/local/nginx" 132 | nginx binary file: "/usr/local/nginx/sbin/nginx" 133 | nginx modules path: "/usr/local/nginx/modules" 134 | nginx configuration prefix: "/usr/local/nginx/conf" 135 | nginx configuration file: "/usr/local/nginx/conf/nginx.conf" 136 | nginx pid file: "/usr/local/nginx/logs/nginx.pid" 137 | nginx error log file: "/usr/local/nginx/logs/error.log" 138 | nginx http access log file: "/usr/local/nginx/logs/access.log" 139 | nginx http client request body temporary files: "client_body_temp" 140 | nginx http proxy temporary files: "proxy_temp" 141 | nginx http fastcgi temporary files: "fastcgi_temp" 142 | nginx http uwsgi temporary files: "uwsgi_temp" 143 | nginx http scgi temporary files: "scgi_temp" 144 | ``` 145 | 146 | 安装报错误的话比如:“C compiler cc is not found”,这个就是缺少编译环境,安装一下就可以了 **yum -y install gcc make gcc-c++ openssl-devel** 147 | 148 | 如果没有error信息,就可以执行下边的安装了: 149 | 150 | ```bash 151 | make 152 | make install 153 | ``` 154 | 155 | ### Nginx 测试 156 | 157 | 运行下面命令会出现两个结果,一般情况nginx会安装在`/usr/local/nginx`目录中 158 | 159 | ```bash 160 | cd /usr/local/nginx/sbin/ 161 | ./nginx -t 162 | 163 | # nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok 164 | # nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful 165 | ``` 166 | 167 | ### 设置全局 Nginx 命令 168 | 169 | ```bash 170 | vi ~/.bash_profile 171 | ``` 172 | 173 | 将下面内容添加到 `~/.bash_profile` 文件中 174 | 175 | ```bash 176 | PATH=$PATH:$HOME/bin:/usr/local/nginx/sbin/ 177 | export PATH 178 | ``` 179 | 180 | 运行命令 **`source ~/.bash_profile`** 让配置立即生效。你就可以全局运行 `nginx` 命令了。 181 | 182 | ## Mac 安装 183 | 184 | Mac OSX 安装特别简单,首先你需要安装 [Brew](https://brew.sh/), 通过 `brew` 快速安装 `nginx`。 185 | 186 | ### 安装nginx 187 | 188 | ```bash 189 | brew install nginx 190 | # Updating Homebrew... 191 | # ==> Auto-updated Homebrew! 192 | # Updated 2 taps (homebrew/core, homebrew/cask). 193 | # ==> Updated Formulae 194 | # ==> Installing dependencies for nginx: openssl, pcre 195 | # ==> Installing nginx dependency: openssl 196 | # ==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2o_1.high_sierra.bottle.tar.gz 197 | # ######################################################################## 100.0% 198 | # ==> Pouring openssl-1.0.2o_1.high_sierra.bottle.tar.gz 199 | # ==> Caveats 200 | # A CA file has been bootstrapped using certificates from the SystemRoots 201 | # keychain. To add additional certificates (e.g. the certificates added in 202 | # the System keychain), place .pem files in 203 | # /usr/local/etc/openssl/certs 204 | # 205 | # and run 206 | # /usr/local/opt/openssl/bin/c_rehash 207 | # 208 | # This formula is keg-only, which means it was not symlinked into /usr/local, 209 | # because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries. 210 | # 211 | # If you need to have this software first in your PATH run: 212 | # echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.zshrc 213 | # 214 | # For compilers to find this software you may need to set: 215 | # LDFLAGS: -L/usr/local/opt/openssl/lib 216 | # CPPFLAGS: -I/usr/local/opt/openssl/include 217 | # For pkg-config to find this software you may need to set: 218 | # PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig 219 | # 220 | # ==> Summary 221 | # 🍺 /usr/local/Cellar/openssl/1.0.2o_1: 1,791 files, 12.3MB 222 | # ==> Installing nginx dependency: pcre 223 | # ==> Downloading https://homebrew.bintray.com/bottles/pcre-8.42.high_sierra.bottle.tar.gz 224 | # ######################################################################## 100.0% 225 | # ==> Pouring pcre-8.42.high_sierra.bottle.tar.gz 226 | # 🍺 /usr/local/Cellar/pcre/8.42: 204 files, 5.3MB 227 | # ==> Installing nginx 228 | # ==> Downloading https://homebrew.bintray.com/bottles/nginx-1.13.12.high_sierra.bottle.tar.gz 229 | # ######################################################################## 100.0% 230 | # ==> Pouring nginx-1.13.12.high_sierra.bottle.tar.gz 231 | # ==> Caveats 232 | # Docroot is: /usr/local/var/www 233 | # 234 | # The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that 235 | # nginx can run without sudo. 236 | # 237 | # nginx will load all files in /usr/local/etc/nginx/servers/. 238 | # 239 | # To have launchd start nginx now and restart at login: 240 | # brew services start nginx 241 | # Or, if you don't wacd /usr/local/Cellar/nginx/1.13.12/n just run: 242 | # cd /usr/local/Cellar/nginx/1.13.12/ 243 | ``` 244 | 245 | ### 启动服务 246 | 247 | 注意默认端口不是 `8080` 查看确认端口是否被占用。 248 | 249 | ```bash 250 | brew services start nginx 251 | # http://localhost:8080/ 252 | ``` 253 | 254 | ## 开机自启动 255 | 256 | **开机自启动方法一:** 257 | 258 | 编辑 **vi /lib/systemd/system/nginx.service** 文件,没有创建一个 **touch nginx.service** 然后将如下内容根据具体情况进行修改后,添加到nginx.service文件中: 259 | 260 | ```bash 261 | [Unit] 262 | Description=nginx 263 | After=network.target remote-fs.target nss-lookup.target 264 | 265 | [Service] 266 | 267 | Type=forking 268 | PIDFile=/var/run/nginx.pid 269 | ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf 270 | ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 271 | ExecReload=/bin/kill -s HUP $MAINPID 272 | ExecStop=/bin/kill -s QUIT $MAINPID 273 | PrivateTmp=true 274 | 275 | [Install] 276 | WantedBy=multi-user.target 277 | ``` 278 | 279 | - `[Unit]`:服务的说明 280 | - `Description`:描述服务 281 | - `After`:描述服务类别 282 | - `[Service]`服务运行参数的设置 283 | - `Type=forking`是后台运行的形式 284 | - `ExecStart`为服务的具体运行命令 285 | - `ExecReload`为重启命令 286 | - `ExecStop`为停止命令 287 | - `PrivateTmp=True`表示给服务分配独立的临时空间 288 | 289 | 注意:`[Service]`的启动、重启、停止命令全部要求使用绝对路径。 290 | 291 | `[Install]`运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为`3`。 292 | 293 | 保存退出。 294 | 295 | 设置开机启动,使配置生效: 296 | 297 | ```bash 298 | # 启动nginx服务 299 | systemctl start nginx.service 300 | # 停止开机自启动 301 | systemctl disable nginx.service 302 | # 查看服务当前状态 303 | systemctl status nginx.service 304 | # 查看所有已启动的服务 305 | systemctl list-units --type=service 306 | # 重新启动服务 307 | systemctl restart nginx.service 308 | # 设置开机自启动 309 | systemctl enable nginx.service 310 | # 输出下面内容表示成功了 311 | Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service. 312 | ``` 313 | 314 | ```bash 315 | systemctl is-enabled servicename.service # 查询服务是否开机启动 316 | systemctl enable *.service # 开机运行服务 317 | systemctl disable *.service # 取消开机运行 318 | systemctl start *.service # 启动服务 319 | systemctl stop *.service # 停止服务 320 | systemctl restart *.service # 重启服务 321 | systemctl reload *.service # 重新加载服务配置文件 322 | systemctl status *.service # 查询服务运行状态 323 | systemctl --failed # 显示启动失败的服务 324 | ``` 325 | 326 | 注:*代表某个服务的名字,如http的服务名为httpd 327 | 328 | **开机自启动方法二:** 329 | 330 | ```bash 331 | vi /etc/rc.local 332 | 333 | # 在 rc.local 文件中,添加下面这条命令 334 | /usr/local/nginx/sbin/nginx start 335 | ``` 336 | 337 | 如果开机后发现自启动脚本没有执行,你要去确认一下rc.local这个文件的访问权限是否是可执行的,因为rc.local默认是不可执行的。修改rc.local访问权限,增加可执行权限: 338 | 339 | ```bash 340 | # /etc/rc.local是/etc/rc.d/rc.local的软连接, 341 | chmod +x /etc/rc.d/rc.local 342 | ``` 343 | 344 | 官方脚本 [ed Hat NGINX Init Script](https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/)。 345 | 346 | ## 运维 347 | 348 | ### 服务管理 349 | 350 | ```bash 351 | # 启动 352 | /usr/local/nginx/sbin/nginx 353 | # 重启 354 | /usr/local/nginx/sbin/nginx -s reload 355 | # 关闭进程 356 | /usr/local/nginx/sbin/nginx -s stop 357 | # 平滑关闭nginx 358 | /usr/local/nginx/sbin/nginx -s quit 359 | # 查看nginx的安装状态, 360 | /usr/local/nginx/sbin/nginx -V 361 | ``` 362 | 363 | **关闭防火墙,或者添加防火墙规则就可以测试了** 364 | 365 | ```bash 366 | service iptables stop 367 | ``` 368 | 369 | 或者编辑配置文件: 370 | 371 | ```bash 372 | vi /etc/sysconfig/iptables 373 | ``` 374 | 375 | 添加这样一条开放80端口的规则后保存: 376 | 377 | ```bash 378 | -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT 379 | ``` 380 | 381 | 重启服务即可: 382 | 383 | ```bash 384 | service iptables restart 385 | # 命令进行查看目前nat 386 | iptables -t nat -L 387 | ``` 388 | 389 | ### 重启服务防火墙报错解决 390 | 391 | 392 | ```bash 393 | service iptables restart 394 | # Redirecting to /bin/systemctl restart iptables.service 395 | # Failed to restart iptables.service: Unit iptables.service failed to load: No such file or directory. 396 | ``` 397 | 398 | 在CentOS 7或RHEL 7或Fedora中防火墙由 **firewalld** 来管理,当然你可以还原传统的管理方式。或则使用新的命令进行管理。 399 | 假如采用传统请执行一下命令: 400 | 401 | ```bash 402 | # 传统命令 403 | systemctl stop firewalld 404 | systemctl mask firewalld 405 | ``` 406 | 407 | ```bash 408 | # 安装命令 409 | yum install iptables-services 410 | 411 | systemctl enable iptables 412 | service iptables restart 413 | ``` 414 | 415 | 416 | ## Nginx 卸载 417 | 418 | 如果通过yum安装,使用下面命令安装。 419 | 420 | ```bash 421 | yum remove nginx 422 | ``` 423 | 424 | 编译安装,删除/usr/local/nginx目录即可 425 | 如果配置了自启动脚本,也需要删除。 426 | 427 | 428 | ## 参数说明 429 | 430 | | 参数 | 说明 | 431 | | ---- | ---- | 432 | | --prefix=`` | Nginx安装路径。如果没有指定,默认为 /usr/local/nginx。 | 433 | | --sbin-path=`` | Nginx可执行文件安装路径。只能安装时指定,如果没有指定,默认为``/sbin/nginx。 | 434 | | --conf-path=`` | 在没有给定-c选项下默认的nginx.conf的路径。如果没有指定,默认为``/conf/nginx.conf。 | 435 | | --pid-path=`` | 在nginx.conf中没有指定pid指令的情况下,默认的nginx.pid的路径。如果没有指定,默认为 ``/logs/nginx.pid。 | 436 | | --lock-path=`` | nginx.lock文件的路径。 | 437 | | --error-log-path=`` | 在nginx.conf中没有指定error_log指令的情况下,默认的错误日志的路径。如果没有指定,默认为 ``/- logs/error.log。 | 438 | | --http-log-path=`` | 在nginx.conf中没有指定access_log指令的情况下,默认的访问日志的路径。如果没有指定,默认为 ``/- logs/access.log。 | 439 | | --user=`` | 在nginx.conf中没有指定user指令的情况下,默认的nginx使用的用户。如果没有指定,默认为 nobody。 | 440 | | --group=`` | 在nginx.conf中没有指定user指令的情况下,默认的nginx使用的组。如果没有指定,默认为 nobody。 | 441 | | --builddir=DIR | 指定编译的目录 | 442 | | --with-rtsig_module | 启用 rtsig 模块 | 443 | | --with-select_module --without-select_module | 允许或不允许开启SELECT模式,如果 configure 没有找到更合适的模式,比如:kqueue(sun os),epoll (linux kenel 2.6+), rtsig(- 实时信号)或者/dev/poll(一种类似select的模式,底层实现与SELECT基本相 同,都是采用轮训方法) SELECT模式将是默认安装模式| 444 | | --with-poll_module --without-poll_module | Whether or not to enable the poll module. This module is enabled by, default if a more suitable method such as kqueue, epoll, rtsig or /dev/poll is not discovered by configure. | 445 | | --with-http_ssl_module | Enable ngx_http_ssl_module. Enables SSL support and the ability to handle HTTPS requests. Requires OpenSSL. On Debian, this is libssl-dev. 开启HTTP SSL模块,使NGINX可以支持HTTPS请求。这个模块需要已经安装了OPENSSL,在DEBIAN上是libssl | 446 | | --with-http_realip_module | 启用 ngx_http_realip_module | 447 | | --with-http_addition_module | 启用 ngx_http_addition_module | 448 | | --with-http_sub_module | 启用 ngx_http_sub_module | 449 | | --with-http_dav_module | 启用 ngx_http_dav_module | 450 | | --with-http_flv_module | 启用 ngx_http_flv_module | 451 | | --with-http_stub_status_module | 启用 "server status" 页 | 452 | | --without-http_charset_module | 禁用 ngx_http_charset_module | 453 | | --without-http_gzip_module | 禁用 ngx_http_gzip_module. 如果启用,需要 zlib 。 | 454 | | --without-http_ssi_module | 禁用 ngx_http_ssi_module | 455 | | --without-http_userid_module | 禁用 ngx_http_userid_module | 456 | | --without-http_access_module | 禁用 ngx_http_access_module | 457 | | --without-http_auth_basic_module | 禁用 ngx_http_auth_basic_module | 458 | | --without-http_autoindex_module | 禁用 ngx_http_autoindex_module | 459 | | --without-http_geo_module | 禁用 ngx_http_geo_module | 460 | | --without-http_map_module | 禁用 ngx_http_map_module | 461 | | --without-http_referer_module | 禁用 ngx_http_referer_module | 462 | | --without-http_rewrite_module | 禁用 ngx_http_rewrite_module. 如果启用需要 PCRE 。 | 463 | | --without-http_proxy_module | 禁用 ngx_http_proxy_module | 464 | | --without-http_fastcgi_module | 禁用 ngx_http_fastcgi_module | 465 | | --without-http_memcached_module | 禁用 ngx_http_memcached_module | 466 | | --without-http_limit_zone_module | 禁用 ngx_http_limit_zone_module | 467 | | --without-http_empty_gif_module | 禁用 ngx_http_empty_gif_module | 468 | | --without-http_browser_module | 禁用 ngx_http_browser_module | 469 | | --without-http_upstream_ip_hash_module | 禁用 ngx_http_upstream_ip_hash_module | 470 | | --with-http_perl_module | 启用 ngx_http_perl_module | 471 | | --with-perl_modules_path=PATH | 指定 perl 模块的路径 | 472 | | --with-perl=PATH | 指定 perl 执行文件的路径 | 473 | | --http-log-path=PATH | Set path to the http access log | 474 | | --http-client-body-temp-path=PATH | Set path to the http client request body temporary files | 475 | | --http-proxy-temp-path=PATH | Set path to the http proxy temporary files | 476 | | --http-fastcgi-temp-path=PATH | Set path to the http fastcgi temporary files | 477 | | --without-http | 禁用 HTTP server | 478 | | --with-mail | 启用 IMAP4/POP3/SMTP 代理模块 | 479 | | --with-mail_ssl_module | 启用 ngx_mail_ssl_module | 480 | | --with-cc=PATH | 指定 C 编译器的路径 | 481 | | --with-cpp=PATH | 指定 C 预处理器的路径 | 482 | | --with-cc-opt=OPTIONS | Additional parameters which will be added to the variable CFLAGS. With the use of the system library PCRE in FreeBSD, it is necessary to indicate --with-cc-opt="-I /usr/local/include". If we are using select() and it is necessary to increase the number of file descriptors, then this also can be assigned here: --with-cc-opt="-D FD_SETSIZE=2048". | 483 | | --with-ld-opt=OPTIONS | Additional parameters passed to the linker. With the use of the system library PCRE in - FreeBSD, it is necessary to indicate --with-ld-opt="-L /usr/local/lib". | 484 | | --with-cpu-opt=CPU | 为特定的 CPU 编译,有效的值包括:pentium, pentiumpro, pentium3, pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64 | 485 | | --without-pcre | 禁止 PCRE 库的使用。同时也会禁止 HTTP rewrite 模块。在 "location" 配置指令中的正则表达式也需要 PCRE 。 | 486 | | --with-pcre=DIR | 指定 PCRE 库的源代码的路径。 | 487 | | --with-pcre-opt=OPTIONS | Set additional options for PCRE building. | 488 | | --with-md5=DIR | Set path to md5 library sources. | 489 | | --with-md5-opt=OPTIONS | Set additional options for md5 building. | 490 | | --with-md5-asm | Use md5 assembler sources. | 491 | | --with-sha1=DIR | Set path to sha1 library sources. | 492 | | --with-sha1-opt=OPTIONS | Set additional options for sha1 building. | 493 | | --with-sha1-asm | Use sha1 assembler sources. | 494 | | --with-zlib=DIR | Set path to zlib library sources. | 495 | | --with-zlib-opt=OPTIONS | Set additional options for zlib building. | 496 | | --with-zlib-asm=CPU | Use zlib assembler sources optimized for specified CPU, valid values are: pentium, pentiumpro | 497 | | --with-openssl=DIR | Set path to OpenSSL library sources | 498 | | --with-openssl-opt=OPTIONS | Set additional options for OpenSSL building | 499 | | --with-debug | 启用调试日志 | 500 | | --add-module=PATH | Add in a third-party module found in directory PATH | 501 | 502 | 503 | ## 配置 504 | 505 | 在Centos 默认配置文件在 **/usr/local/nginx-1.5.1/conf/nginx.conf** 我们要在这里配置一些文件。nginx.conf是主配置文件,由若干个部分组成,每个大括号`{}`表示一个部分。每一行指令都由分号结束`;`,标志着一行的结束。 506 | 507 | ### 常用正则 508 | 509 | | 正则 | 说明 | 正则 | 说明 | 510 | | ---- | ---- | ---- | ---- | 511 | | `. ` | 匹配除换行符以外的任意字符 | `$ ` | 匹配字符串的结束 | 512 | | `? ` | 重复0次或1次 | `{n} ` | 重复n次 | 513 | | `+ ` | 重复1次或更多次 | `{n,} ` | 重复n次或更多次 | 514 | | `*` | 重复0次或更多次 | `[c] ` | 匹配单个字符c | 515 | | `\d ` |匹配数字 | `[a-z]` | 匹配a-z小写字母的任意一个 | 516 | | `^ ` | 匹配字符串的开始 | - | - | 517 | 518 | ### 全局变量 519 | 520 | | 变量 | 说明 | 变量 | 说明 | 521 | | ---- | ---- | ---- | ---- | 522 | | $args | 这个变量等于请求行中的参数,同$query_string | $remote_port | 客户端的端口。 | 523 | | $content_length | 请求头中的Content-length字段。 | $remote_user | 已经经过Auth Basic Module验证的用户名。 | 524 | | $content_type | 请求头中的Content-Type字段。 | $request_filename | 当前请求的文件路径,由root或alias指令与URI请求生成。 | 525 | | $document_root | 当前请求在root指令中指定的值。 | $scheme | HTTP方法(如http,https)。 | 526 | | $host | 请求主机头字段,否则为服务器名称。 | $server_protocol | 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。 | 527 | | $http_user_agent | 客户端agent信息 | $server_addr | 服务器地址,在完成一次系统调用后可以确定这个值。 | 528 | | $http_cookie | 客户端cookie信息 | $server_name | 服务器名称。 | 529 | | $limit_rate | 这个变量可以限制连接速率。 | $server_port | 请求到达服务器的端口号。 | 530 | | $request_method | 客户端请求的动作,通常为GET或POST。 | $request_uri | 包含请求参数的原始URI,不包含主机名,如:/foo/bar.php?arg=baz。 | 531 | | $remote_addr | 客户端的IP地址。 | $uri | 不带请求参数的当前URI,$uri不包含主机名,如/foo/bar.html。 | 532 | | $document_uri | 与$uri相同。 | - | - | 533 | 534 | 例如请求:`http://localhost:3000/test1/test2/test.php` 535 | 536 | ``` 537 | $host:localhost 538 | $server_port:3000 539 | $request_uri:/test1/test2/test.php 540 | $document_uri:/test1/test2/test.php 541 | $document_root:/var/www/html 542 | $request_filename:/var/www/html/test1/test2/test.php 543 | ``` 544 | 545 | ### 符号参考 546 | 547 | | 符号 | 说明 | 符号 | 说明 | 符号 | 说明 | 548 | | ---- | ---- | ---- | ---- | ---- | ---- | 549 | | k,K | 千字节 | m,M | 兆字节 | ms | 毫秒 | 550 | | s | 秒 | m | 分钟 | h | 小时 | 551 | | d | 日 | w | 周 | M | 一个月, 30天 | 552 | 553 | 例如,"8k","1m" 代表字节数计量。 554 | 例如,"1h 30m","1y 6M"。代表 "1小时 30分","1年零6个月"。 555 | 556 | ### 配置文件 557 | 558 | nginx 的配置系统由一个主配置文件和其他一些辅助的配置文件构成。这些配置文件均是纯文本文件,全部位于 nginx 安装目录下的 conf 目录下。 559 | 560 | 指令由 nginx 的各个模块提供,不同的模块会提供不同的指令来实现配置。 561 | 指令除了 Key-Value 的形式,还有作用域指令。 562 | 563 | nginx.conf 中的配置信息,根据其逻辑上的意义,对它们进行了分类,也就是分成了多个作用域,或者称之为配置指令上下文。不同的作用域含有一个或者多个配置项。 564 | 565 | 下面的这些上下文指令是用的比较多: 566 | 567 | | Directive | Description | Contains Directive | 568 | | ---- | ---- | ---- | 569 | | main | nginx 在运行时与具体业务功能(比如 http 服务或者 email 服务代理)无关的一些参数,比如工作进程数,运行的身份等。 | user, worker_processes, error_log, events, http, mail | 570 | | http | 与提供 http 服务相关的一些配置参数。例如:是否使用 keepalive 啊,是否使用 gzip 进行压缩等。 | server | 571 | | server | http 服务上支持若干虚拟主机。每个虚拟主机一个对应的 server 配置项,配置项里面包含该虚拟主机相关的配置。在提供 mail 服务的代理时,也可以建立若干 server. 每个 server 通过监听的地址来区分。| listen, server_name, access_log, location, protocol, proxy, smtp_auth, xclient | 572 | | location | http 服务中,某些特定的 URL 对应的一系列配置项。 | index, root | 573 | | mail | 实现 email 相关的 SMTP/IMAP/POP3 代理时,共享的一些配置项(因为可能实现多个代理,工作在多个监听地址上)。 | server, http, imap_capabilities | 574 | | include | 以便增强配置文件的可读性,使得部分配置文件可以重新使用。 | - | 575 | | valid_referers | 用来校验Http请求头Referer是否有效。 | - | 576 | | try_files | 用在server部分,不过最常见的还是用在location部分,它会按照给定的参数顺序进行尝试,第一个被匹配到的将会被使用。 | - | 577 | | if | 当在location块中使用if指令,在某些情况下它并不按照预期运行,一般来说避免使用if指令。 | - | 578 | 579 | 580 | 例如我们再 **nginx.conf** 里面引用两个配置 vhost/example.com.conf 和 vhost/gitlab.com.conf 它们都被放在一个我自己新建的目录 vhost 下面。nginx.conf 配置如下: 581 | 582 | ```nginx 583 | worker_processes 1; 584 | events { 585 | worker_connections 1024; 586 | } 587 | 588 | http { 589 | include mime.types; 590 | default_type application/octet-stream; 591 | 592 | #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 593 | # '$status $body_bytes_sent "$http_referer" ' 594 | # '"$http_user_agent" "$http_x_forwarded_for"'; 595 | 596 | #access_log logs/access.log main; 597 | 598 | sendfile on; 599 | #tcp_nopush on; 600 | 601 | #keepalive_timeout 0; 602 | keepalive_timeout 65; 603 | 604 | #gzip on; 605 | server { 606 | listen 80; 607 | server_name localhost; 608 | location / { 609 | root html; 610 | index index.html index.htm; 611 | } 612 | error_page 500 502 503 504 /50x.html; 613 | location = /50x.html { 614 | root html; 615 | } 616 | } 617 | include vhost/example.com.conf; 618 | include vhost/gitlab.com.conf; 619 | } 620 | ``` 621 | 622 | 623 | 简单的配置: example.com.conf 624 | 625 | ```nginx 626 | server { 627 | #侦听的80端口 628 | listen 80; 629 | server_name baidu.com app.baidu.com; # 这里指定域名 630 | index index.html index.htm; # 这里指定默认入口页面 631 | root /home/www/app.baidu.com; # 这里指定目录 632 | } 633 | ``` 634 | 635 | ### 内置预定义变量 636 | 637 | Nginx提供了许多预定义的变量,也可以通过使用set来设置变量。你可以在if中使用预定义变量,也可以将它们传递给代理服务器。以下是一些常见的预定义变量,[更多详见](http://nginx.org/en/docs/varindex.html) 638 | 639 | | 变量名称 | 值 | 640 | | ---- | ---- | 641 | | $args_name | 在请求中的name参数 | 642 | | $args | 所有请求参数 | 643 | | $query_string | $args的别名 | 644 | | $content_length | 请求头Content-Length的值 | 645 | | $content_type | 请求头Content-Type的值 | 646 | | $host | 如果当前有Host,则为请求头Host的值;如果没有这个头,那么该值等于匹配该请求的server_name的值 | 647 | | $remote_addr | 客户端的IP地址 | 648 | | $request | 完整的请求,从客户端收到,包括Http请求方法、URI、Http协议、头、请求体 | 649 | | $request_uri | 完整请求的URI,从客户端来的请求,包括参数 | 650 | | $scheme | 当前请求的协议 | 651 | | $uri | 当前请求的标准化URI | 652 | 653 | ### 反向代理 654 | 655 | 反向代理是一个Web服务器,它接受客户端的连接请求,然后将请求转发给上游服务器,并将从服务器得到的结果返回给连接的客户端。下面简单的反向代理的例子: 656 | 657 | ```nginx 658 | server { 659 | listen 80; 660 | server_name localhost; 661 | client_max_body_size 1024M; # 允许客户端请求的最大单文件字节数 662 | 663 | location / { 664 | proxy_pass http://localhost:8080; 665 | proxy_set_header Host $host:$server_port; 666 | proxy_set_header X-Forwarded-For $remote_addr; # HTTP的请求端真实的IP 667 | proxy_set_header X-Forwarded-Proto $scheme; # 为了正确地识别实际用户发出的协议是 http 还是 https 668 | } 669 | } 670 | ``` 671 | 672 | 复杂的配置: gitlab.com.conf。 673 | 674 | ```nginx 675 | server { 676 | #侦听的80端口 677 | listen 80; 678 | server_name git.example.cn; 679 | location / { 680 | proxy_pass http://localhost:3000; 681 | #以下是一些反向代理的配置可删除 682 | proxy_redirect off; 683 | #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP 684 | proxy_set_header Host $host; 685 | client_max_body_size 10m; #允许客户端请求的最大单文件字节数 686 | client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数 687 | proxy_connect_timeout 300; #nginx跟后端服务器连接超时时间(代理连接超时) 688 | proxy_send_timeout 300; #后端服务器数据回传时间(代理发送超时) 689 | proxy_read_timeout 300; #连接成功后,后端服务器响应时间(代理接收超时) 690 | proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小 691 | proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置 692 | proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2) 693 | } 694 | } 695 | ``` 696 | 697 | 代理到上游服务器的配置中,最重要的是proxy_pass指令。以下是代理模块中的一些常用指令: 698 | 699 | | 指令 | 说明 | 700 | | ---- | ---- | 701 | | proxy_connect_timeout | Nginx从接受请求至连接到上游服务器的最长等待时间 | 702 | | proxy_send_timeout | 后端服务器数据回传时间(代理发送超时) | 703 | | proxy_read_timeout | 连接成功后,后端服务器响应时间(代理接收超时) | 704 | | proxy_cookie_domain | 替代从上游服务器来的Set-Cookie头的domain属性 | 705 | | proxy_cookie_path | 替代从上游服务器来的Set-Cookie头的path属性 | 706 | | proxy_buffer_size | 设置代理服务器(nginx)保存用户头信息的缓冲区大小 | 707 | | proxy_buffers | proxy_buffers缓冲区,网页平均在多少k以下 | 708 | | proxy_set_header | 重写发送到上游服务器头的内容,也可以通过将某个头部的值设置为空字符串,而不发送某个头部的方法实现 | 709 | | proxy_ignore_headers | 这个指令禁止处理来自代理服务器的应答。 | 710 | | proxy_intercept_errors | 使nginx阻止HTTP应答代码为400或者更高的应答。 | 711 | 712 | ### 负载均衡 713 | 714 | upstream指令启用一个新的配置区段,在该区段定义一组上游服务器。这些服务器可能被设置不同的权重,也可能出于对服务器进行维护,标记为down。 715 | 716 | ```nginx 717 | upstream gitlab { 718 | ip_hash; 719 | # upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。 720 | server 192.168.122.11:8081 ; 721 | server 127.0.0.1:82 weight=3; 722 | server 127.0.0.1:83 weight=3 down; 723 | server 127.0.0.1:84 weight=3; max_fails=3 fail_timeout=20s; 724 | server 127.0.0.1:85 weight=4;; 725 | keepalive 32; 726 | } 727 | server { 728 | #侦听的80端口 729 | listen 80; 730 | server_name git.example.cn; 731 | location / { 732 | proxy_pass http://gitlab; #在这里设置一个代理,和upstream的名字一样 733 | #以下是一些反向代理的配置可删除 734 | proxy_redirect off; 735 | #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP 736 | proxy_set_header Host $host; 737 | proxy_set_header X-Real-IP $remote_addr; 738 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 739 | client_max_body_size 10m; #允许客户端请求的最大单文件字节数 740 | client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数 741 | proxy_connect_timeout 300; #nginx跟后端服务器连接超时时间(代理连接超时) 742 | proxy_send_timeout 300; #后端服务器数据回传时间(代理发送超时) 743 | proxy_read_timeout 300; #连接成功后,后端服务器响应时间(代理接收超时) 744 | proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小 745 | proxy_buffers 4 32k;# 缓冲区,网页平均在32k以下的话,这样设置 746 | proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2) 747 | proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传 748 | } 749 | } 750 | ``` 751 | 752 | 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。 753 | 754 | **负载均衡:** 755 | 756 | upstream模块能够使用3种负载均衡算法:轮询、IP哈希、最少连接数。 757 | 758 | **轮询:** 默认情况下使用轮询算法,不需要配置指令来激活它,它是基于在队列中谁是下一个的原理确保访问均匀地分布到每个上游服务器; 759 | **IP哈希:** 通过ip_hash指令来激活,Nginx通过IPv4地址的前3个字节或者整个IPv6地址作为哈希键来实现,同一个IP地址总是能被映射到同一个上游服务器; 760 | **最少连接数:** 通过least_conn指令来激活,该算法通过选择一个活跃数最少的上游服务器进行连接。如果上游服务器处理能力不同,可以通过给server配置weight权重来说明,该算法将考虑到不同服务器的加权最少连接数。 761 | 762 | #### RR 763 | 764 | **简单配置** ,这里我配置了2台服务器,当然实际上是一台,只是端口不一样而已,而8081的服务器是不存在的,也就是说访问不到,但是我们访问 `http://localhost` 的时候,也不会有问题,会默认跳转到`http://localhost:8080`具体是因为Nginx会自动判断服务器的状态,如果服务器处于不能访问(服务器挂了),就不会跳转到这台服务器,所以也避免了一台服务器挂了影响使用的情况,由于Nginx默认是RR策略,所以我们不需要其他更多的设置 765 | 766 | ```nginx 767 | upstream test { 768 | server localhost:8080; 769 | server localhost:8081; 770 | } 771 | server { 772 | listen 81; 773 | server_name localhost; 774 | client_max_body_size 1024M; 775 | 776 | location / { 777 | proxy_pass http://test; 778 | proxy_set_header Host $host:$server_port; 779 | } 780 | } 781 | ``` 782 | 783 | **负载均衡的核心代码为** 784 | 785 | ```nginx 786 | upstream test { 787 | server localhost:8080; 788 | server localhost:8081; 789 | } 790 | ``` 791 | 792 | #### 权重 793 | 794 | 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。 例如 795 | 796 | ```nginx 797 | upstream test { 798 | server localhost:8080 weight=9; 799 | server localhost:8081 weight=1; 800 | } 801 | ``` 802 | 803 | 那么10次一般只会有1次会访问到8081,而有9次会访问到8080 804 | 805 | #### ip_hash 806 | 807 | 上面的2种方式都有一个问题,那就是下一个请求来的时候请求可能分发到另外一个服务器,当我们的程序不是无状态的时候(采用了session保存数据),这时候就有一个很大的很问题了,比如把登录信息保存到了session中,那么跳转到另外一台服务器的时候就需要重新登录了,所以很多时候我们需要一个客户只访问一个服务器,那么就需要用iphash了,iphash的每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。 808 | 809 | ```nginx 810 | upstream test { 811 | ip_hash; 812 | server localhost:8080; 813 | server localhost:8081; 814 | } 815 | ``` 816 | 817 | #### fair 818 | 819 | 这是个第三方模块,按后端服务器的响应时间来分配请求,响应时间短的优先分配。 820 | 821 | ```nginx 822 | upstream backend { 823 | fair; 824 | server localhost:8080; 825 | server localhost:8081; 826 | } 827 | ``` 828 | 829 | #### url_hash 830 | 831 | 这是个第三方模块,按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。 在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法 832 | 833 | ```nginx 834 | upstream backend { 835 | hash $request_uri; 836 | hash_method crc32; 837 | server localhost:8080; 838 | server localhost:8081; 839 | } 840 | ``` 841 | 842 | 以上5种负载均衡各自适用不同情况下使用,所以可以根据实际情况选择使用哪种策略模式,不过fair和url_hash需要安装第三方模块才能使用 843 | 844 | **server指令可选参数:** 845 | 846 | 1. weight:设置一个服务器的访问权重,数值越高,收到的请求也越多; 847 | 2. fail_timeout:在这个指定的时间内服务器必须提供响应,如果在这个时间内没有收到响应,那么服务器将会被标记为down状态; 848 | 3. max_fails:设置在fail_timeout时间之内尝试对一个服务器连接的最大次数,如果超过这个次数,那么服务器将会被标记为down; 849 | 4. down:标记一个服务器不再接受任何请求; 850 | 5. backup:一旦其他服务器宕机,那么有该标记的机器将会接收请求。 851 | 852 | **keepalive指令:** 853 | 854 | Nginx服务器将会为每一个worker进行保持同上游服务器的连接。 855 | 856 | ### 屏蔽ip 857 | 858 | 在nginx的配置文件`nginx.conf`中加入如下配置,可以放到http, server, location, limit_except语句块,需要注意相对路径,本例当中`nginx.conf`,`blocksip.conf`在同一个目录中。 859 | 860 | ```nginx 861 | include blockip.conf; 862 | ``` 863 | 864 | 在blockip.conf里面输入内容,如: 865 | 866 | ```nginx 867 | deny 165.91.122.67; 868 | 869 | deny IP; # 屏蔽单个ip访问 870 | allow IP; # 允许单个ip访问 871 | deny all; # 屏蔽所有ip访问 872 | allow all; # 允许所有ip访问 873 | deny 123.0.0.0/8 # 屏蔽整个段即从123.0.0.1到123.255.255.254访问的命令 874 | deny 124.45.0.0/16 # 屏蔽IP段即从123.45.0.1到123.45.255.254访问的命令 875 | deny 123.45.6.0/24 # 屏蔽IP段即从123.45.6.1到123.45.6.254访问的命令 876 | 877 | # 如果你想实现这样的应用,除了几个IP外,其他全部拒绝 878 | allow 1.1.1.1; 879 | allow 1.1.1.2; 880 | deny all; 881 | ``` 882 | 883 | ## 第三方模块安装方法 884 | 885 | ``` 886 | ./configure --prefix=/你的安装目录 --add-module=/第三方模块目录 887 | ``` 888 | 889 | ## 重定向 890 | 891 | - `permanent` 永久性重定向。请求日志中的状态码为301 892 | - `redirect` 临时重定向。请求日志中的状态码为302 893 | 894 | ### 重定向整个网站 895 | 896 | ```nginx 897 | server { 898 | server_name old-site.com 899 | return 301 $scheme://new-site.com$request_uri; 900 | } 901 | ``` 902 | 903 | ### 重定向单页 904 | 905 | ```nginx 906 | server { 907 | location = /oldpage.html { 908 | return 301 http://example.org/newpage.html; 909 | } 910 | } 911 | ``` 912 | 913 | ### 重定向整个子路径 914 | 915 | ```nginx 916 | location /old-site { 917 | rewrite ^/old-site/(.*) http://example.org/new-site/$1 permanent; 918 | } 919 | ``` 920 | 921 | ## 性能 922 | 923 | ### 内容缓存 924 | 925 | 允许浏览器基本上永久地缓存静态内容。 Nginx将为您设置Expires和Cache-Control头信息。 926 | 927 | ```nginx 928 | location /static { 929 | root /data; 930 | expires max; 931 | } 932 | ``` 933 | 934 | 如果要求浏览器永远不会缓存响应(例如用于跟踪请求),请使用-1。 935 | 936 | ```nginx 937 | location = /empty.gif { 938 | empty_gif; 939 | expires -1; 940 | } 941 | ``` 942 | 943 | ### Gzip压缩 944 | 945 | ```nginx 946 | gzip on; 947 | gzip_buffers 16 8k; 948 | gzip_comp_level 6; 949 | gzip_http_version 1.1; 950 | gzip_min_length 256; 951 | gzip_proxied any; 952 | gzip_vary on; 953 | gzip_types 954 | text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml 955 | text/javascript application/javascript application/x-javascript 956 | text/x-json application/json application/x-web-app-manifest+json 957 | text/css text/plain text/x-component 958 | font/opentype application/x-font-ttf application/vnd.ms-fontobject 959 | image/x-icon; 960 | gzip_disable "msie6"; 961 | ``` 962 | 963 | ### 打开文件缓存 964 | 965 | ```nginx 966 | open_file_cache max=1000 inactive=20s; 967 | open_file_cache_valid 30s; 968 | open_file_cache_min_uses 2; 969 | open_file_cache_errors on; 970 | ``` 971 | 972 | ### SSL缓存 973 | 974 | ```nginx 975 | ssl_session_cache shared:SSL:10m; 976 | ssl_session_timeout 10m; 977 | ``` 978 | 979 | ### 上游Keepalive 980 | 981 | ```nginx 982 | upstream backend { 983 | server 127.0.0.1:8080; 984 | keepalive 32; 985 | } 986 | server { 987 | ... 988 | location /api/ { 989 | proxy_pass http://backend; 990 | proxy_http_version 1.1; 991 | proxy_set_header Connection ""; 992 | } 993 | } 994 | ``` 995 | 996 | ### 监控 997 | 998 | 使用`ngxtop`实时解析nginx访问日志,并且将处理结果输出到终端,功能类似于系统命令top。所有示例都读取nginx配置文件的访问日志位置和格式。如果要指定访问日志文件和/或日志格式,请使用-f和-a选项。 999 | 1000 | 注意:在nginx配置中`/usr/local/nginx/conf/nginx.conf`日志文件必须是绝对路径。 1001 | 1002 | ```bash 1003 | # 安装 ngxtop 1004 | pip install ngxtop 1005 | 1006 | # 实时状态 1007 | ngxtop 1008 | # 状态为404的前10个请求的路径: 1009 | ngxtop top request_path --filter 'status == 404' 1010 | 1011 | # 发送总字节数最多的前10个请求 1012 | ngxtop --order-by 'avg(bytes_sent) * count' 1013 | 1014 | # 排名前十位的IP,例如,谁攻击你最多 1015 | ngxtop --group-by remote_addr 1016 | 1017 | # 打印具有4xx或5xx状态的请求,以及status和http referer 1018 | ngxtop -i 'status >= 400' print request status http_referer 1019 | 1020 | # 由200个请求路径响应发送的平均正文字节以'foo'开始: 1021 | ngxtop avg bytes_sent --filter 'status == 200 and request_path.startswith("foo")' 1022 | 1023 | # 使用“common”日志格式从远程机器分析apache访问日志 1024 | ssh remote tail -f /var/log/apache2/access.log | ngxtop -f common 1025 | ``` 1026 | 1027 | ## 常见使用场景 1028 | 1029 | ### 跨域问题 1030 | 1031 | 在工作中,有时候会遇到一些接口不支持跨域,这时候可以简单的添加add_headers来支持cors跨域。配置如下: 1032 | 1033 | ```nginx 1034 | server { 1035 | listen 80; 1036 | server_name api.xxx.com; 1037 | 1038 | add_header 'Access-Control-Allow-Origin' '*'; 1039 | add_header 'Access-Control-Allow-Credentials' 'true'; 1040 | add_header 'Access-Control-Allow-Methods' 'GET,POST,HEAD'; 1041 | 1042 | location / { 1043 | proxy_pass http://127.0.0.1:3000; 1044 | proxy_set_header X-Real-IP $remote_addr; 1045 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 1046 | proxy_set_header Host $http_host; 1047 | } 1048 | } 1049 | ``` 1050 | 1051 | 上面更改头信息,还有一种,使用 [rewrite](http://nginx.org/en/docs/http/ngx_http_rewrite_module.html) 指令重定向URI来解决跨域问题。 1052 | 1053 | ```nginx 1054 | upstream test { 1055 | server 127.0.0.1:8080; 1056 | server localhost:8081; 1057 | } 1058 | server { 1059 | listen 80; 1060 | server_name api.xxx.com; 1061 | location / { 1062 | root html; #去请求../html文件夹里的文件 1063 | index index.html index.htm; #首页响应地址 1064 | } 1065 | # 用于拦截请求,匹配任何以 /api/开头的地址, 1066 | # 匹配符合以后,停止往下搜索正则。 1067 | location ^~/api/{ 1068 | # 代表重写拦截进来的请求,并且只能对域名后边的除去传递的参数外的字符串起作用, 1069 | # 例如www.a.com/proxy/api/msg?meth=1&par=2重写,只对/proxy/api/msg重写。 1070 | # rewrite后面的参数是一个简单的正则 ^/api/(.*)$, 1071 | # $1代表正则中的第一个(),$2代表第二个()的值,以此类推。 1072 | rewrite ^/api/(.*)$ /$1 break; 1073 | 1074 | # 把请求代理到其他主机 1075 | # 其中 http://www.b.com/ 写法和 http://www.b.com写法的区别如下 1076 | # 如果你的请求地址是他 http://server/html/test.jsp 1077 | # 配置一: http://www.b.com/ 后面有“/” 1078 | # 将反向代理成 http://www.b.com/html/test.jsp 访问 1079 | # 配置一: http://www.b.com 后面没有有“/” 1080 | # 将反向代理成 http://www.b.com/test.jsp 访问 1081 | proxy_pass http://test; 1082 | 1083 | # 如果 proxy_pass URL 是 http://a.xx.com/platform/ 这种情况 1084 | # proxy_cookie_path应该设置成 /platform/ / (注意两个斜杠之间有空格)。 1085 | proxy_cookie_path /platfrom/ /; 1086 | 1087 | # http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass_header 1088 | # 设置 Cookie 头通过 1089 | proxy_pass_header Set-Cookie; 1090 | } 1091 | } 1092 | ``` 1093 | 1094 | ### 跳转到带 www 的域上面 1095 | 1096 | ```nginx 1097 | server { 1098 | listen 80; 1099 | # 配置正常的带www的域名 1100 | server_name www.wangchujiang.com; 1101 | root /home/www/wabg/download; 1102 | location / { 1103 | try_files $uri $uri/ /index.html =404; 1104 | } 1105 | } 1106 | server { 1107 | # 这个要放到下面, 1108 | # 将不带www的 wangchujiang.com 永久性重定向到 https://www.wangchujiang.com 1109 | server_name wangchujiang.com; 1110 | rewrite ^(.*) https://www.wangchujiang.com$1 permanent; 1111 | } 1112 | ``` 1113 | 1114 | ### 代理转发 1115 | 1116 | ```nginx 1117 | upstream server-api{ 1118 | # api 代理服务地址 1119 | server 127.0.0.1:3110; 1120 | } 1121 | upstream server-resource{ 1122 | # 静态资源 代理服务地址 1123 | server 127.0.0.1:3120; 1124 | } 1125 | server { 1126 | listen 3111; 1127 | server_name localhost; # 这里指定域名 1128 | root /home/www/server-statics; 1129 | # 匹配 api 路由的反向代理到API服务 1130 | location ^~/api/ { 1131 | rewrite ^/(.*)$ /$1 break; 1132 | proxy_pass http://server-api; 1133 | } 1134 | # 假设这里验证码也在API服务中 1135 | location ^~/captcha { 1136 | rewrite ^/(.*)$ /$1 break; 1137 | proxy_pass http://server-api; 1138 | } 1139 | # 假设你的图片资源全部在另外一个服务上面 1140 | location ^~/img/ { 1141 | rewrite ^/(.*)$ /$1 break; 1142 | proxy_pass http://server-resource; 1143 | } 1144 | # 路由在前端,后端没有真实路由,在路由不存在的 404状态的页面返回 /index.html 1145 | # 这个方式使用场景,你在写React或者Vue项目的时候,没有真实路由 1146 | location / { 1147 | try_files $uri $uri/ /index.html =404; 1148 |        #                               ^ 空格很重要 1149 |    } 1150 | } 1151 | ``` 1152 | 1153 | ### 监控状态信息 1154 | 1155 | 通过 `nginx -V` 来查看是否有 `with-http_stub_status_module` 该模块。 1156 | 1157 | > `nginx -V` 这里 `V` 是大写的,如果是小写的 `v` 即 `nginx -v`,则不会出现有哪些模块,只会出现 `nginx` 的版本 1158 | 1159 | ```nginx 1160 | location /nginx_status { 1161 | stub_status on; 1162 | access_log off; 1163 | } 1164 | ``` 1165 | 1166 | 通过 http://127.0.0.1/nginx_status 访问出现下面结果。 1167 | 1168 | ```bash 1169 | Active connections: 3 1170 | server accepts handled requests 1171 | 7 7 5 1172 | Reading: 0 Writing: 1 Waiting: 2 1173 | ``` 1174 | 1175 | 1. 主动连接(第 1 行) 1176 | 1177 | 当前与http建立的连接数,包括等待的客户端连接:3 1178 | 1179 | 2. 服务器接受处理的请求(第 2~3 行) 1180 | 1181 | 接受的客户端连接总数目:7 1182 | 处理的客户端连接总数目:7 1183 | 客户端总的请求数目:5 1184 | 1185 | 3. 读取其它信(第 4 行) 1186 | 1187 | 当前,nginx读请求连接 1188 | 当前,nginx写响应返回给客户端 1189 | 目前有多少空闲客户端请求连接 1190 | 1191 | ### 代理转发连接替换 1192 | 1193 | ```nginx 1194 | location ^~/api/upload { 1195 | rewrite ^/(.*)$ /wfs/v1/upload break; 1196 | proxy_pass http://wfs-api; 1197 | } 1198 | ``` 1199 | 1200 | ### SSL 配置 1201 | 1202 | 超文本传输安全协议(缩写:HTTPS,英语:Hypertext Transfer Protocol Secure)是超文本传输协议和SSL/TLS的组合,用以提供加密通讯及对网络服务器身份的鉴定。HTTPS连接经常被用于万维网上的交易支付和企业信息系统中敏感信息的传输。HTTPS不应与在RFC 2660中定义的安全超文本传输协议(S-HTTP)相混。HTTPS 目前已经是所有注重隐私和安全的网站的首选,随着技术的不断发展,HTTPS 网站已不再是大型网站的专利,所有普通的个人站长和博客均可以自己动手搭建一个安全的加密的网站。 1203 | 1204 | 1205 | 创建SSL证书,如果你购买的证书,就可以直接下载 1206 | 1207 | ```bash 1208 | sudo mkdir /etc/nginx/ssl 1209 | # 创建了有效期100年,加密强度为RSA2048的SSL密钥key和X509证书文件。 1210 | sudo openssl req -x509 -nodes -days 36500 -newkey rsa:2048 -keyout /etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.crt 1211 | # 上面命令,会有下面需要填写内容 1212 | Country Name (2 letter code) [AU]:US 1213 | State or Province Name (full name) [Some-State]:New York 1214 | Locality Name (eg, city) []:New York City 1215 | Organization Name (eg, company) [Internet Widgits Pty Ltd]:Bouncy Castles, Inc. 1216 | Organizational Unit Name (eg, section) []:Ministry of Water Slides 1217 | Common Name (e.g. server FQDN or YOUR name) []:your_domain.com 1218 | Email Address []:admin@your_domain.com 1219 | ``` 1220 | 1221 | 创建自签证书 1222 | 1223 | ```bash 1224 | 首先,创建证书和私钥的目录 1225 | # mkdir -p /etc/nginx/cert 1226 | # cd /etc/nginx/cert 1227 | 创建服务器私钥,命令会让你输入一个口令: 1228 | # openssl genrsa -des3 -out nginx.key 2048 1229 | 创建签名请求的证书(CSR): 1230 | # openssl req -new -key nginx.key -out nginx.csr 1231 | 在加载SSL支持的Nginx并使用上述私钥时除去必须的口令: 1232 | # cp nginx.key nginx.key.org 1233 | # openssl rsa -in nginx.key.org -out nginx.key 1234 | 最后标记证书使用上述私钥和CSR: 1235 | # openssl x509 -req -days 365 -in nginx.csr -signkey nginx.key -out nginx.crt 1236 | ``` 1237 | 1238 | 查看目前nginx编译选项 1239 | 1240 | ``` 1241 | sbin/nginx -V 1242 | ``` 1243 | 1244 | 输出下面内容 1245 | 1246 | ``` 1247 | nginx version: nginx/1.7.8 1248 | built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC) 1249 | TLS SNI support enabled 1250 | configure arguments: --prefix=/usr/local/nginx-1.7.8 --with-http_ssl_module --with-http_spdy_module --with-http_stub_status_module --with-pcre 1251 | ``` 1252 | 1253 | 如果依赖的模块不存在,可以进入安装目录,输入下面命令重新编译安装。 1254 | 1255 | ```bash 1256 | ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module 1257 | ``` 1258 | 1259 | 运行完成之后还需要`make` (不用make install) 1260 | 1261 | ```bash 1262 | # 备份nginx的二进制文件 1263 | cp -rf /usr/local/nginx/sbin/nginx  /usr/local/nginx/sbin/nginx.bak 1264 | # 覆盖nginx的二进制文件 1265 | cp -rf objs/nginx /usr/local/nginx/sbin/ 1266 | ``` 1267 | 1268 | HTTPS server 1269 | 1270 | ```nginx 1271 | server { 1272 | listen 443 ssl; 1273 | server_name localhost; 1274 | 1275 | ssl_certificate /etc/nginx/ssl/nginx.crt; 1276 | ssl_certificate_key /etc/nginx/ssl/nginx.key; 1277 | # 禁止在header中出现服务器版本,防止黑客利用版本漏洞攻击 1278 | server_tokens off; 1279 | # 设置ssl/tls会话缓存的类型和大小。如果设置了这个参数一般是shared,buildin可能会参数内存碎片,默认是none,和off差不多,停用缓存。如shared:SSL:10m表示我所有的nginx工作进程共享ssl会话缓存,官网介绍说1M可以存放约4000个sessions。 1280 | ssl_session_cache shared:SSL:1m; 1281 | 1282 | # 客户端可以重用会话缓存中ssl参数的过期时间,内网系统默认5分钟太短了,可以设成30m即30分钟甚至4h。 1283 | ssl_session_timeout 5m; 1284 | 1285 | # 选择加密套件,不同的浏览器所支持的套件(和顺序)可能会不同。 1286 | # 这里指定的是OpenSSL库能够识别的写法,你可以通过 openssl -v cipher 'RC4:HIGH:!aNULL:!MD5'(后面是你所指定的套件加密算法) 来看所支持算法。 1287 | ssl_ciphers HIGH:!aNULL:!MD5; 1288 | 1289 | # 设置协商加密算法时,优先使用我们服务端的加密套件,而不是客户端浏览器的加密套件。 1290 | ssl_prefer_server_ciphers on; 1291 | 1292 | location / { 1293 | root html; 1294 | index index.html index.htm; 1295 | } 1296 | } 1297 | ``` 1298 | 1299 | ### 强制将http重定向到https 1300 | 1301 | ```nginx 1302 | server { 1303 | listen 80; 1304 | server_name example.com; 1305 | rewrite ^ https://$http_host$request_uri? permanent; # 强制将http重定向到https 1306 | # 在错误页面和“服务器”响应头字段中启用或禁用发射nginx版本。 防止黑客利用版本漏洞攻击 1307 | server_tokens off; 1308 | } 1309 | ``` 1310 | 1311 | ### 两个虚拟主机 1312 | 1313 | 纯静态-html 支持 1314 | 1315 | ```nginx 1316 | http { 1317 | server { 1318 | listen 80; 1319 | server_name www.domain1.com; 1320 | access_log logs/domain1.access.log main; 1321 | location / { 1322 | index index.html; 1323 | root /var/www/domain1.com/htdocs; 1324 | } 1325 | } 1326 | server { 1327 | listen 80; 1328 | server_name www.domain2.com; 1329 | access_log logs/domain2.access.log main; 1330 | location / { 1331 | index index.html; 1332 | root /var/www/domain2.com/htdocs; 1333 | } 1334 | } 1335 | } 1336 | ``` 1337 | 1338 | ### 虚拟主机标准配置 1339 | 1340 | ```nginx 1341 | http { 1342 | server { 1343 | listen 80 default; 1344 | server_name _ *; 1345 | access_log logs/default.access.log main; 1346 | location / { 1347 | index index.html; 1348 | root /var/www/default/htdocs; 1349 | } 1350 | } 1351 | } 1352 | ``` 1353 | 1354 | ### 爬虫过滤 1355 | 1356 | 根据 `User-Agent` 过滤请求,通过一个简单的正则表达式,就可以过滤不符合要求的爬虫请求(初级爬虫)。 1357 | 1358 | > `~*` 表示不区分大小写的正则匹配 1359 | 1360 | ```nginx 1361 | location / { 1362 | if ($http_user_agent ~* "python|curl|java|wget|httpclient|okhttp") { 1363 | return 503; 1364 | } 1365 | # 正常处理 1366 | # ... 1367 | } 1368 | ``` 1369 | 1370 | ### 防盗链 1371 | 1372 | ```nginx 1373 | location ~* \.(gif|jpg|png|swf|flv)$ { 1374 | root html 1375 | valid_referers none blocked *.nginxcn.com; 1376 | if ($invalid_referer) { 1377 | rewrite ^/ www.nginx.cn 1378 | #return 404; 1379 | } 1380 | } 1381 | ``` 1382 | 1383 | ### 虚拟目录配置 1384 | 1385 | alias指定的目录是准确的,root是指定目录的上级目录,并且该上级目录要含有location指定名称的同名目录。 1386 | 1387 | ```nginx 1388 | location /img/ { 1389 | alias /var/www/image/; 1390 | } 1391 | # 访问/img/目录里面的文件时,ningx会自动去/var/www/image/目录找文件 1392 | location /img/ { 1393 | root /var/www/image; 1394 | } 1395 | # 访问/img/目录下的文件时,nginx会去/var/www/image/img/目录下找文件。] 1396 | ``` 1397 | 1398 | ### 防盗图配置 1399 | 1400 | ```nginx 1401 | location ~ \/public\/(css|js|img)\/.*\.(js|css|gif|jpg|jpeg|png|bmp|swf) { 1402 | valid_referers none blocked *.jslite.io; 1403 | if ($invalid_referer) { 1404 | rewrite ^/ http://wangchujiang.com/piratesp.png; 1405 | } 1406 | } 1407 | ``` 1408 | 1409 | ### 屏蔽.git等文件 1410 | 1411 | ```nginx 1412 | location ~ (.git|.gitattributes|.gitignore|.svn) { 1413 | deny all; 1414 | } 1415 | ``` 1416 | 1417 | ### 域名路径加不加需要都能正常访问 1418 | 1419 | ```bash 1420 | http://wangchujiang.com/api/index.php?a=1&name=wcj 1421 | ^ 有后缀 1422 | 1423 | http://wangchujiang.com/api/index?a=1&name=wcj 1424 | ^ 没有后缀 1425 | ``` 1426 | 1427 | nginx rewrite规则如下: 1428 | 1429 | ```nginx 1430 | rewrite ^/(.*)/$ /index.php?/$1 permanent; 1431 | if (!-d $request_filename){ 1432 | set $rule_1 1$rule_1; 1433 | } 1434 | if (!-f $request_filename){ 1435 | set $rule_1 2$rule_1; 1436 | } 1437 | if ($rule_1 = "21"){ 1438 | rewrite ^/ /index.php last; 1439 | } 1440 | ``` 1441 | 1442 | ### cockpit 1443 | 1444 | https://github.com/cockpit-project/cockpit 1445 | 1446 | ```nginx 1447 | server{ 1448 |     listen 80; 1449 |     server_name cockpit.xxxxxxx.com; 1450 |     return 301 https://$server_name$request_uri; 1451 | } 1452 | 1453 | server { 1454 |     listen 443 ssl; 1455 |     server_name cockpit.xxxxxxx.com; 1456 | 1457 |     #ssl on; 1458 |     ssl_certificate /etc/nginx/cert/cockpit.xxxxxxx.com.pem; 1459 |     ssl_certificate_key /etc/nginx/cert/cockpit.xxxxxxx.com.key; 1460 | 1461 |     location / { 1462 |         root /; 1463 |         index index.html; 1464 |         proxy_redirect off; 1465 |         proxy_pass http://websocket; 1466 |         proxy_http_version 1.1; 1467 |         proxy_set_header Upgrade $http_upgrade; 1468 |         proxy_set_header Connection "upgrade"; 1469 |         proxy_set_header Host $http_host; 1470 |     } 1471 | } 1472 | ``` 1473 | 1474 | 这时输入域名,能看到登录页面,但登录后,显示不出内容,页面全白。这里要对 `cockpit.conf` 进行设置修改。 1475 | 1476 | ```bash 1477 | sudo vim /etc/cockpit/cockpit.conf 1478 | ``` 1479 | 1480 | 参照如下配置修改,注意域名替换为 `your_domain_host`: 1481 | 1482 | ```toml 1483 | [WebService] 1484 | Origins = https://cockpit.xxxxxxx.com https://127.0.0.1:9090 1485 | ProtocolHeader = X-Forwarded-Proto 1486 | AllowUnencrypted = true 1487 | ``` 1488 | 1489 | ## 错误问题 1490 | 1491 | ```bash 1492 | The plain HTTP request was sent to HTTPS port 1493 | ``` 1494 | 1495 | 解决办法,`fastcgi_param HTTPS $https if_not_empty` 添加这条规则, 1496 | 1497 | ```nginx 1498 | server { 1499 | listen 443 ssl; # 注意这条规则 1500 | server_name my.domain.com; 1501 | 1502 | fastcgi_param HTTPS $https if_not_empty; 1503 | fastcgi_param HTTPS on; 1504 | 1505 | ssl_certificate /etc/ssl/certs/your.pem; 1506 | ssl_certificate_key /etc/ssl/private/your.key; 1507 | 1508 | location / { 1509 | # Your config here... 1510 | } 1511 | } 1512 | ``` 1513 | 1514 | ## Nginx 模块 1515 | 1516 | - [Nginx Office Hours](https://gitlab.com/rbdr/ngx_http_office_hours_filter_module) 一个 nginx 模块,允许您仅在办公时间内提供访问访问网站。 1517 | 1518 | ## 精品文章参考 1519 | 1520 | - [负载均衡原理的解析](https://my.oschina.net/u/3341316/blog/877206) 1521 | - [Nginx泛域名解析,实现多个二级域名 ](http://blog.githuber.cn/posts/73) 1522 | - [深入 NGINX: 我们如何设计性能和扩展](https://www.nginx.com/blog/inside-nginx-how-we-designed-for-performance-scale/) 1523 | - [Inside NGINX: How We Designed for Performance & Scale](https://www.nginx.com/blog/inside-nginx-how-we-designed-for-performance-scale/) 1524 | - [Nginx开发从入门到精通](http://tengine.taobao.org/book/index.html) 1525 | - [Nginx的优化与防盗链](http://os.51cto.com/art/201703/535326.htm#topx) 1526 | - [实战开发一个Nginx扩展 (Nginx Module)](https://segmentfault.com/a/1190000009769143) 1527 | - [Nginx+Keepalived(双机热备)搭建高可用负载均衡环境(HA)](https://my.oschina.net/xshuai/blog/917097) 1528 | - [Nginx 平滑升级](http://www.huxd.org/articles/2017/07/24/1500890692329.html) 1529 | - [Nginx最新模块—ngx_http_mirror_module分析可以做版本发布前的预先验证,进行流量放大后的压测等等](https://mp.weixin.qq.com/s?__biz=MzIxNzg5ODE0OA==&mid=2247483708&idx=1&sn=90b0b1dccd9c337922a0588245277666&chksm=97f38cf7a08405e1928e0b46d923d630e529e7db8ac7ca2a91310a075986f8bcb2cee5b4953d#rd) 1530 | 1531 | ## Contributors 1532 | 1533 | As always, thanks to our amazing contributors! 1534 | 1535 | 1536 | 1537 | 1538 | 1539 | Made with [action-contributors](https://github.com/jaywcjlove/github-action-contributors). 1540 | 1541 | ## License 1542 | 1543 | Licensed under the MIT License. 1544 | -------------------------------------------------------------------------------- /favicon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /idoc.yml: -------------------------------------------------------------------------------- 1 | site: "Nginx 入门学习笔记" 2 | logo: "./favicon.svg" 3 | favicon: "./favicon.svg" 4 | 5 | homepage: https://wangchujiang.com/nginx-tutorial/ 6 | 7 | menus: 8 | 首页: index.html 9 | 捐赠: 10 | url: https://wangchujiang.com/#/sponsor 11 | target: __blank 12 | 应用: 13 | url: https://wangchujiang.com/#/app 14 | target: __blank 15 | 16 | editButton: 17 | label: 在 GitHub 上编辑此页面 18 | url: https://github.com/jaywcjlove/nginx-tutorial/tree/master/ 19 | footer: | 20 | App • 21 | Projects • 22 | Sponsor • 23 | More Apps

24 | Released under the MIT License. Copyright © 2024 Kenny Wong
25 | Generated by idoc v{{idocVersion}} -------------------------------------------------------------------------------- /nginx.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://json.schemastore.org/package.json", 3 | "private": true, 4 | "name": "nginx-tutorial", 5 | "description": "Nginx 是一款面向性能设计的 HTTP 服务器,能反向代理 HTTP,HTTPS 和邮件相关(SMTP,POP3,IMAP)的协议链接。并且提供了负载均衡以及 HTTP 缓存。它的设计充分使用异步事件模型,削减上下文调度的开销,提高服务器并发能力。采用了模块化设计,提供了丰富模块的第三方模块。", 6 | "homepage": "https://jaywcjlove.github.io/nginx-tutorial", 7 | "version": "1.0.0", 8 | "scripts": { 9 | "start": "idoc --watch", 10 | "build": "idoc" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/jaywcjlove/nginx-tutorial" 15 | }, 16 | "keywords": [ 17 | "nginx", 18 | "tutorial", 19 | "centos", 20 | "Nginx 入门学习笔记" 21 | ], 22 | "dependencies": { 23 | "idoc": "^1" 24 | } 25 | } -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "config:base" 4 | ], 5 | "packageRules": [ 6 | { 7 | "matchPackagePatterns": ["*"], 8 | "rangeStrategy": "replace" 9 | } 10 | ] 11 | } 12 | --------------------------------------------------------------------------------