Nginx最全操作总结

阅读: 评论:0

Nginx最全操作总结
本⽂将会从:安装 -> 全局配置 -> 常⽤的各种配置 来书写,其中常⽤配置写的炒鸡详细,需要的童鞋可以直接滑倒相应的位置查看。
安装 nginx
yum update #更新系统软件
cd /
/download/nginx-1.17.
解压 压缩包⽂件,进去 nginx-1.17.2
tar -xzvf nginx-1.17.
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 就可以查看
nginx 编译安装时,怎么查看安装模块
编译并安装
make && make install
这⾥需要注意,模块的⽀持跟后续的 nginx 配置有关,⽐如 SSL,gzip 压缩等等,编译安装前最好检查需要配置的模块存不存在。
查看 nginx 安装后在的⽬录,可以看到已经安装到 /usr/local/nginx ⽬录了
whereis nginx
$nginx: /usr/local/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:配置后端服务器具体地址,负载均衡配置不可或缺的部分。
1. 配置⽂件由指令与指令块构成
2. 每条指令以 “;” 分号结尾,指令与参数间以空格符号分隔
3. 指令块以 {} ⼤括号将多条指令组织在⼀起
4. include 语句允许组合多个配置⽂件以提升可维护性
5. 通过 # 符号添加注释,提⾼可读性
6. 通过 $ 符号使⽤变量
7. 部分指令的参数⽀持正则表达式,例如常⽤的 location 指令
内置变量
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=$PATH/conf/$f
PIDFILE=$PATH/logs/$NAME.pid
scriptNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}webmax
do_stop() {
$DAEMON -s stop || echo -n "nginx not running"
}
do_reload() {
$DAEMON -s reload || echo -n "nginx can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $"
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $scriptNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0
设置所有⼈都有对这个启动脚本 nginx ⽂件的执⾏权限:
chmod a+x /etc/init.d/nginx
把 nginx 加⼊系统服务中:
chkconfig --add nginx
把服务设置为开机启动:
chkconfig nginx on
reboot 重启系统⽣效,可以使⽤上⾯ systemctl ⽅法相同的命令:
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 #查询服务是否开机启动
如果服务启动的时候出现 Restarting nginx daemon: nginxnginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory) nginx not running 的错误,通过 nginx -c 参数指定配置⽂件即可解决
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/f
如果服务启动中出现 nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 的错误,
可以先通过 service nginx stop 停⽌服务,再启动就好。
配置 nginx 全局可⽤
当你每次改了 f 配置⽂件的内容都需要重新到 nginx 启动⽬录去执⾏命令,或者通过 -p 参数指向特定⽬录,会不会感觉很⿇烦?
例如: 直接执⾏ nginx -s reload 会报错 -bash: nginx: command not found ,需要到 /usr/local/nginx/sbin ⽬录下⾯去执⾏,并且是执⾏ ./nginx -s reload 。
这⾥有两种⽅式可以解决,⼀种是通过脚本对 nginx 命令包装,这⾥介绍另外⼀种⽐较简单:通过把 nginx 配置到环境变量⾥,⽤ nginx 执⾏指令即可。步骤如下:
1、编辑 /etc/profile
vi /etc/profile
2、在最后⼀⾏添加配置,:wq 保存

本文发布于:2023-06-27 20:47:34,感谢您对本站的认可!

本文链接:https://patent.en369.cn/xueshu/138427.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:配置   启动   开机   服务   安装
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 369专利查询检索平台 豫ICP备2021025688号-20 网站地图