Nginx最全操作总结

本文详细介绍了Nginx的安装、配置、常用命令及各项功能,包括反向代理、负载均衡、访问控制、gzip压缩、HTTP服务器、动静分离、请求限制等,并提供了配置开机自启和全局可用的方法,以及如何处理常见的问题和技巧,帮助读者全面掌握Nginx的使用。
摘要由CSDN通过智能技术生成
本文将会从:安装 -> 全局配置 -> 常用的各种配置 来书写,其中常用配置写的炒鸡详细,需要的童鞋可以直接滑倒相应的位置查看。

安装 nginx

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

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

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

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

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

./configure

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

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

再次进行检查操作 ./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

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

查看默认安装的模块支持

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

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

通过命令:cat nginx-1.17.2/auto/options | grep YES就可以查看

编译并安装

make && make install

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

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

whereis nginx
$nginx: /usr/local/nginx

启动 nginx 服务

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

服务启动的时候报错了: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

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

./nginx

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

nginx 配置

基本结构

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

主要配置含义

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

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

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

资料领取直通车:大厂面试题锦集+视频教程icon-default.png?t=M85Bhttps://docs.qq.com/doc/DTlhVekRrZUdDUEpy

Linux服务器学习网站:C/C++Linux服务器开发/后台架构师icon-default.png?t=M85Bhttps://ke.qq.com/course/417774?flowToken=1028592

内置变量

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

TCP UDP

常用命令

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

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

以上命令通过 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 运行状态

配置 nginx 开机自启

利用 systemctl 命令

如果用 yum install 命令安装的 nginx,yum 命令会自动创建 nginx.service 文件,直接用命令:

systemctl enable nginx   # 设置开机启动 Nginx
systemctl disable nginx  # 关闭开机启动 Nginx

就可以设置开机自启,否则需要在系统服务目录里创建 nginx.service 文件。

创建并打开 nginx.service 文件:

vi /lib/systemd/system/nginx.service

内容如下:

[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

:wq 保存退出,运行 systemctl daemon-reload 使文件生效。

这样便可以通过以下命令操作 nginx 了:

systemctl start nginx.service # 启动nginx服务
systemctl enable nginx.service # 设置开机启动
systemctl disable nginx.service # 停止开机自启动
systemctl status nginx.service # 查看服务当前状态
systemctl restart nginx.service # 重新启动服务
systemctl is-enabled nginx.service #查询服务是否开机启动

通过开机启动命令脚本实现开机自启

创建开机启动命令脚本文件:

vi /etc/init.d/nginx

在这个 nginx 文件中插入一下启动脚本代码,启动脚本代码来源网络复制,实测有效:

#! /bin/bash
# chkconfig: - 85 15
PATH=/usr/local/nginx
DESC="nginx daemon"
NAME=nginx
DAEMON=$PATH/sbin/$NAME
CONFIGFILE&#
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值