天天看点

Nginx 操作总结

安装 nginx

下载 nginx 的压缩包文件到根目录,官网下载地址:nginx.org/download/nginx-x.xx.xx.tar.gz

yum update #更新系统软件  
cd /  
wget nginx.org/download/nginx-1.17.2.tar.gz             

复制

Bash

Copy

解压 tar.gz 压缩包文件,进去 nginx-1.17.2

tar -xzvf nginx-1.17.2.tar.gz  
cd nginx-1.17.2             

复制

Bash

Copy

进入文件夹后进行配置检查

./configure             

复制

Bash

Copy

通过安装前的配置检查,发现有报错。检查中发现一些依赖库没有找到,这时候需要先安装 nginx 的一些依赖库

yum -y install pcre* #安装使nginx支持rewrite  
yum -y install gcc-c++  
yum -y install zlib*  
yum -y install openssl openssl-devel             

复制

Bash

Copy

再次进行检查操作

./configure

没发现报错显示,接下来进行编译并安装的操作

// 检查模块支持
  ./configure  --prefix=/usr/local/nginx  --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-mail --with-mail_ssl_module --with-stream --with-stream_ssl_module --with-stream_realip_module --with-stream_ssl_preread_module --with-threads --user=www --group=www            

复制

Bash

Copy

这里得特别注意下,你以后需要用到的功能模块是否存在,不然以后添加新的包会比较麻烦。

查看默认安装的模块支持

ls nginx-1.17.2

查看 nginx 的文件列表,可以发现里面有一个 auto 的目录。

在这个 auto 目录中有一个 options 文件,这个文件里面保存的就是 nginx 编译过程中的所有选项配置。

通过命令:

cat nginx-1.17.2/auto/options | grep YES

就可以查看

nginx 编译安装时,怎么查看安装模块

编译并安装

make && make install             

复制

Bash

Copy

这里需要注意,模块的支持跟后续的 nginx 配置有关,比如 SSL,gzip 压缩等等,编译安装前最好检查需要配置的模块存不存在。

查看 nginx 安装后在的目录,可以看到已经安装到 /usr/local/nginx 目录了

whereis nginx  
$nginx: /usr/local/nginx             

复制

Bash

Copy

启动 nginx 服务

cd /usr/local/nginx/sbin/  
./nginx             

复制

Bash

Copy

服务启动的时候报错了:

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

,通过命令查看本机网络地址和端口等一些信息,找到被占用的 80 端口

netstat \-ntpl

的 tcp 连接,并杀死进程(kill 进程 pid)

netstat -ntpl  
kill 进程PID             

复制

Bash

Copy

继续启动 nginx 服务,启动成功

./nginx             

复制

Bash

Copy

在浏览器直接访问 ip 地址,页面出现 Welcome to Nginx! 则安装成功。

nginx 配置

基本结构

main        # 全局配置,对全局生效  
├── events  # 配置影响 nginx 服务器或与用户的网络连接  
├── http    # 配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置  
│   ├── upstream # 配置后端服务器具体地址,负载均衡配置不可或缺的部分  
│   ├── server   # 配置虚拟主机的相关参数,一个 http 块中可以有多个 server 块  
│   ├── server  
│   │   ├── location  # server 块可以包含多个 location 块,location 指令用于匹配 uri  
│   │   ├── location  
│   │   └── ...  
│   └── ...  
└── ...             

复制

Bash

Copy

主要配置含义

  • main:nginx 的全局配置,对全局生效。
  • events:配置影响 nginx 服务器或与用户的网络连接。
  • http:可以嵌套多个 server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。
  • server:配置虚拟主机的相关参数,一个 http 中可以有多个 server。
  • location:配置请求的路由,以及各种页面的处理情况。
  • upstream:配置后端服务器具体地址,负载均衡配置不可或缺的部分。

nginx.conf 配置文件的语法规则

  1. 配置文件由指令与指令块构成
  2. 每条指令以 “;” 分号结尾,指令与参数间以空格符号分隔
  3. 指令块以 {} 大括号将多条指令组织在一起
  4. include 语句允许组合多个配置文件以提升可维护性
  5. 通过 # 符号添加注释,提高可读性
  6. 通过 \$ 符号使用变量
  7. 部分指令的参数支持正则表达式,例如常用的 location 指令

内置变量

nginx 常用的内置全局变量,你可以在配置中随意使用:

常用命令

这里列举几个常用的命令:

nginx -s reload  # 向主进程发送信号,重新加载配置文件,热重启  
nginx -s reopen  # 重启 Nginx  
nginx -s stop    # 快速关闭  
nginx -s quit    # 等待工作进程处理完成后关闭  
nginx -T         # 查看当前 Nginx 最终的配置  
nginx -t -c <配置路径>  # 检查配置是否有问题,如果已经在配置目录,则不需要 -c             

复制

Bash

Copy

以上命令通过

nginx \-h

就可以查看到,还有其它不常用这里未列出。

Linux 系统应用管理工具 systemd 关于 nginx 的常用命令:

systemctl start nginx    # 启动 Nginx  
systemctl stop nginx     # 停止 Nginx  
systemctl restart nginx  # 重启 Nginx  
systemctl reload nginx   # 重新加载 Nginx,用于修改配置后  
systemctl enable nginx   # 设置开机启动 Nginx  
systemctl disable nginx  # 关闭开机启动 Nginx  
systemctl status nginx   # 查看 Nginx 运行状态             

复制

Bash

Copy

配置 nginx 开机自启