Skip to content

Commit d4c29fb

Browse files
committed
feat: 添加文档
1 parent 89dd480 commit d4c29fb

File tree

18 files changed

+552
-4
lines changed

18 files changed

+552
-4
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Nginx 入门教程
2+
3+
学习 Nginx 配置, 包括: 编译安装、反向代理、重定向、重写、缓存、跨域配置等。
4+
5+
## 起因
6+
7+
起源是我的博客,博客从 IIS 伪静态起,我就一直需要接触一些配置,后来遇到了 Nginx ,被她『美丽动人的简约』打动,至些博客开始了各种折腾之路,比如:负载均衡、反向代理、前置缓存等,我把这些操作都记录到了 GitHub ,只是闲时写点笔记,也不成体系。
8+
9+
直到 [VuePress](https://vuepress.vuejs.org) 这么简约的文档起步,我意识到可以重新构建下站点了,起名叫:Nginx 入门教程。
10+
11+
基于 VuePress 构建在 GitHub Pages 中,PR 时基于 [Netlify](https://www.netlify.com) 自动部署预览测试环境方便回归。
12+
13+
## 欢迎你的加入
14+
15+
但我的个人能力有限,水平也有限,只能大概的构建出我的思路,希望有想法的朋友提交代码,一起完善她!
16+
17+
## LICENSE
18+
19+
MIT

docs/.vuepress/config.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
}
42+
},
43+
}
44+
}
45+
}
46+
47+
function genSidebarConfigGuide (title) {
48+
return [
49+
{
50+
title,
51+
collapsable: false,
52+
children: [
53+
'',
54+
'dir',
55+
'linux-install',
56+
'nginx-configure-descriptions',
57+
'error',
58+
]
59+
}
60+
]
61+
}
62+
63+
function genSidebarConfigVariable (title) {
64+
return [
65+
{
66+
title,
67+
collapsable: false,
68+
children: [
69+
'',
70+
'server',
71+
'client',
72+
'url',
73+
]
74+
}
75+
]
76+
}

docs/.vuepress/override.styl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.theme-container.page-main-max
2+
.content
3+
max-width 80%

docs/.vuepress/public/logo.jpg

11 KB
Loading

docs/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
---

docs/config/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# 配置

docs/example/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# 示例

docs/guide/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 ,通常作为负载均衡器。<sup>[[1]][wiki]</sup>
4+
5+
## Nginx的特点 <sup>[[2]][jianshu]</sup>
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+
本项目部分内容是来自网络资源,如有侵权请联系 `[email protected]`
34+
35+
## 相关链接
36+
37+
- [Nginx 官网](http://nginx.org/)

docs/guide/dir.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 主程序版本升级,还是修改某个站点配置,还是快速查找某个站点日志都是得心应手~

docs/guide/error.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# 常见错误
2+
3+
### nginx: [emerg] getpwnam("nginx") failed
4+
5+
表示该用户 `nginx` 不存在, 解决方法:
6+
7+
1.`nginx.conf` 里添加 `user nobody;`
8+
2. 创建用户和用户对应的分组
9+
10+
### nginx: [emerg] getgrnam("xiaowu") failed
11+
12+
表示用户分组不存在,解决方法:
13+
14+
1.`nginx.conf` 里添加 `user nobody;`
15+
2. 创建用户对应的分组
16+
17+
### nginx: [alert] could not open error log file: open() "/logs/error.log" failed (13: Permission denied)
18+
19+
启动 Nginx 的用户权限不够导致无法写入日志文件,常见于非 `root` 用户启动报错。
20+
21+
### nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
22+
23+
80端口被占用启动失败,修改端口或者杀死占用者再启动即可。
24+
25+
### nginx: [error] open() "nginx.pid" failed (2: No such file or directory)
26+
27+
pid 进程 id 文件不存在,可能文件被删除或者已经停止,在停止 Nginx 时会使用该进程id,如果不存在将失败,可以手动 kill 掉。
28+
29+
### nginx: [emerg] unknown "realpath_roots_xxx" variable
30+
31+
变量 `$realpath_roots_xxx` 不存在
32+
33+
### nginx: [emerg] "add_header" directive is not allowed here in xx
34+
35+
`add_header` 指令不能直接在 `if` 判断内,可以在 `http``server``server.location``server.location.if` 下。

docs/guide/linux-install.md

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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": 文本文件忙`

0 commit comments

Comments
 (0)