# Nginx学习篇|博客系列

> 本文详细介绍了Nginx的安装、配置、核心特性（如高性能、反向代理、负载均衡、模块化等），并提供实用的操作步骤和常见问题解决方法，适合初学者和有经验的开发者快速掌握Nginx的使用与优化。

- 原文：https://feinterview.poetries.top/docs/fe-blog-docs/blog-docs/nginx/-Nginx%E5%AD%A6%E4%B9%A0%E7%AF%87
- 来源：前端进阶之旅（feinterview.poetries.top）
- 更新：2025.10.25
- 可见范围：免费部分

---

> Nginx 配置文件在线生成: https://nginxconfig.io/

> `Nginx` 是一款面向性能设计的 `HTTP` 服务器，能反向代理 `HTTP`，`HTTPS` 和邮件相关(`SMTP`，`POP3`，`IMAP`)的协议链接。并且提供了负载均衡以及 `HTTP` 缓存。它的设计充分使用异步事件模型，削减上下文调度的开销，提高服务器并发能力。采用了模块化设计，提供了丰富模块的第三方模块。

- 所以关于 `Nginx，有这些标签：「异步」「事件」「模块化」「高性能」「高并发」「反向代理」「负载均衡」

## 一、安装

### 1.1 安装依赖

> `prce`(重定向支持)和`openssl`(`https`支持，如果不需要`https`可以不安装)

```bash
yum install -y pcre-devel 
yum -y install gcc make gcc-c++ wget
yum -y install openssl openssl-devel 
```

`CentOS 6.5` 我安装的时候是选择的“基本服务器”，默认这两个包都没安装全，所以这两个都运行安装即可

### 1.2 下载

[nginx的所有版本在这里](http://nginx.org/download/)

```bash
wget http://nginx.org/download/nginx-1.13.3.tar.gz
wget http://nginx.org/download/nginx-1.13.7.tar.gz

# 如果没有安装wget
# 下载已编译版本
$ yum install wget

# 解压压缩包
tar zxf nginx-1.13.3.tar.gz
```

### 1.3 编译安装

然后进入目录编译安装，[configure参数说明](#configure参数说明)

```bash
cd nginx-1.11.5
./configure

....
Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"
```

> 安装报错误的话比如：`“C compiler cc is not found”`，这个就是缺少编译环境，安装一下就可以了 `yum -y install gcc make gcc-c++ openssl-devel`

如果没有`error`信息，就可以执行下边的安装了：

```bash
make
make install
```

### 1.4 nginx测试

- 运行下面命令会出现两个结果，一般情况`nginx`会安装在`/usr/local/nginx`目录中

```bash
cd /usr/local/nginx/sbin/
./nginx -t

# nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
# nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
```

### 1.5 设置全局nginx命令

```bash
vi ~/.bash_profile
```

将下面内容添加到 `~/.bash_profile` 文件中

```bash
PATH=$PATH:$HOME/bin:/usr/local/nginx/sbin/
export PATH
```

运行命令 **`source ~/.bash_profile`** 让配置立即生效。你就可以全局运行 `nginx` 命令了。

## 二、开机自启动

**开机自启动方法一**

- 编辑 **`vi /lib/systemd/system/nginx.service`** 文件，没有创建一个 **`touch nginx.service`** - 然后将如下内容根据具体情况进行修改后，添加到`nginx.service`文件中：

```bash
[Unit]
Description=nginx
After=network.target remote-fs.target nss-lookup.target

[Service]

Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
```

```
[Unit]:服务的说明  
Description:描述服务  
After:描述服务类别  
[Service]服务运行参数的设置  
Type=forking是后台运行的形式  
ExecStart为服务的具体运行命令  
ExecReload为重启命令  
ExecStop为停止命令  
PrivateTmp=True表示给服务分配独立的临时空间  
注意：[Service]的启动、重启、停止命令全部要求使用绝对路径  
[Install]运行级别下服务安装的相关设置，可设置为多用户，即系统运行级别为3  
```

保存退出。

设置开机启动，使配置生效：

```bash
systemctl enable nginx.service
# 输出下面内容表示成功了
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
```

**开机自启动方法二**

```bash
vi /etc/rc.local

# 在 rc.local 文件中，添加下面这条命令
/usr/local/nginx/sbin/nginx start
```

- 如果开机后发现自启动脚本没有执行，你要去确认一下`rc.local`这个文件的访问权限是否是可执行的，因为`rc.local`默认是不可执行的。修改`rc.local`访问权限，增加可执行权限：

```bash
chmod +x /etc/rc.d/rc.local
```

## 三、运维

### 3.1 服务管理

```bash
# 启动
/usr/local/nginx/sbin/nginx

# 重启
/usr/local/nginx/sbin/nginx -s reload

# 关闭进程
/usr/local/nginx/sbin/nginx -s stop

# 平滑关闭nginx
/usr/local/nginx/sbin/nginx -s quit

# 查看nginx的安装状态，
/usr/local/nginx/sbin/nginx -V 
```

**关闭防火墙，或者添加防火墙规则就可以测试了**

```bash
service iptables stop
```

或者编辑配置文件：

```bash
vi /etc/sysconfig/iptables
```

添加这样一条开放80端口的规则后保存：

```bash
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
```

重启服务即可:

```bash
service iptables restart
# 命令进行查看目前nat
iptables -t nat -L
```

### 3.2 重启服务防火墙报错解决

```bash
service iptables restart
# Redirecting to /bin/systemctl restart  iptables.service
# Failed to restart iptables.service: Unit iptables.service failed to load: No such file or directory.
```

- 在`CentOS 7`或`RHEL 7`或`Fedora`中防火墙由 **`firewalld`** 来管理，当然你可以还原传统的管理方式。或则使用新的命令进行管理。
假如采用传统请执行一下命令

```bash
# 传统命令
systemctl stop firewalld
systemctl mask firewalld
```

```bash
# 安装命令
yum install iptables-services

systemctl enable iptables 
service iptables restart
```

## 四、nginx卸载

- 如果通过`yum`安装，使用下面命令安装。

```bash
yum remove nginx
```

- 编译安装，删除`/usr/local/nginx`目录即可
- 如果配置了自启动脚本，也需要删除。

## 五、参数说明

---

> 以上是免费部分。完整内容需要「前端进阶之旅」会员，原文：https://feinterview.poetries.top/docs/fe-blog-docs/blog-docs/nginx/-Nginx%E5%AD%A6%E4%B9%A0%E7%AF%87
