https://www.digitalocean.com/community/tools/nginx?global.app.lang=zhCN
可以自由选择所需的应用,生成nginx配置作为参考。
根据你的业务需求,自动生成复杂的nginx配置文件,提供你作为参考,非常好用
1.提供静态页面展示,网页服务
2.提供多个网站、多个域名的网页服务
====================================================
3.提供反向代理服务(结合动态应用程序)
4.提供简单资源下载服务(密码认证) ftp服务
5.用户行为分析(日志功能)
nginx运行后,有多少个干活的工人,多进程,调用多个cpu去解析用户的请求
在linux中进行多进程开发,开辟多个进程,调用多个cpu,当然也会消耗更多的机器资源,内存,cpu资源,给服务器带来更大的压力
- 不是说进程越多,干活越快,合理的分配,才能达到最高效的处理效率
=====
有一个屋子要装修,请1个工人干活,还是请5个工人干活,请500个工人干活
哪一个效率是最高的呢? 最合适的?
===
关于nginx的优化设置,nginx默认应该启动多少个进程去工作呢?
默认就是根据cpu的核数去设置进程数即可。
=======
包工头进程,管理nginx的数据,创建worker工作进程。
1.启动时检查nginx.conf是否正确,语法错误;
2.根据配置文件的参数创建、且监控worker进程的数量和状态;
3.监听socket,接收client发起的请求,然后worker竞争抢夺链接,获胜的可以处理且响应请求。
4.接收运维超哥发送的管理nginx进程的信号,并且将信号通知到worker进程。
5.如果运维超哥发送了reload命令,则读取新配置文件,创建新的worker进程,结束旧的worker进程。
待会再来揭晓。。
---
利用nginx -V 命令可以看到当前操作的这个命令二进制命令,详细的信息,包括支持了哪些模块
官网yum仓库
源代码编译
离线的rpm安装(yum 自建仓库)
留作作业
通过linux命令,去解决这些问题
作业1 : 部署两个版本的nginx
1.编译安装nginx最新版,安装到 /opt/ngx/
2. yum官网仓库也安装一个nginx
3. 你要学会如何管理这多个版本的nginx
4.(各自版本的nginx配置文件路径,PATH设置,数据目录)
5. 当前机器运行的是哪个安装形式的nginx
1. 配置官网yum源,一键安装即可
cat > /etc/yum.repos.d/nginx.repo << 'EOF'
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
EOF
2.清空yum源,安装最新版nginx
[root@web-8 /etc/yum.repos.d]#yum clean all
[root@web-8 /etc/yum.repos.d]#yum install nginx -y
3.查看PATH变量
[root@web-8 /etc/yum.repos.d]#which nginx
/usr/sbin/nginx
[root@web-8 /etc/yum.repos.d]#ll /usr/sbin/nginx
-rwxr-xr-x 1 root root 1377720 Nov 16 2021 /usr/sbin/nginx
[root@web-8 /etc/yum.repos.d]#nginx -V
nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
nginx -t # 检测nginx.conf语法
nginx -s reload # 重新读取nginx.conf
nginx -s stop # 停止nginx kill -15 nginx
nginx # 默认是直接运行,前提是当前机器没运行nginx
#你通过yum安装的nginx请你用systemctl去管理
# 不能多次执行nginx二进制命令
[root@web-8 ~]#
[root@web-8 ~]#nginx
[root@web-8 ~]#nginx # 会报错
# nginx -s reload ,会发生什么
nginx -s reload是给master进程发信号,重新读取配置信息,导致worker重新生成,因此worker-pid发生了变化
但是master进程id不带变化的(包工头,一直没变,更换了手底下的干活的工人)
================================
配置文件变化,就好比 合同变化了(包工头还是他,但是工人更换了一批)
===========
只有你restart的时候,包工头,也会被更换
你只能先停止, 再重启
nginx -s stop # 不会报错的
==============================
如果出现如下错误,如何解决,其实是通过pid,管理nginx的进程
~]#nginx -s stop
nginx: [error] invalid PID number "" in "/var/run/nginx.pid"
[root@web-8 ~]#
[root@web-8 ~]#
[root@web-8 ~]#cat /var/run/nginx.pid
[root@web-8 ~]#
[root@web-8 ~]#
[root@web-8 ~]#
[root@web-8 ~]#ps -ef |grep nginx
root 3599 1 0 16:10 ? 00:00:00 nginx: master process nginx
nginx 3628 3599 0 16:12 ? 00:00:00 nginx: worker process
root 3677 3434 0 16:19 pts/0 00:00:00 grep --color=auto nginx
[root@web-8 ~]#
[root@web-8 ~]#
[root@web-8 ~]#echo 3599 > /var/run/nginx.pid
[root@web-8 ~]#
[root@web-8 ~]#
[root@web-8 ~]#nginx -s stop
[root@web-8 ~]#
[root@web-8 ~]#
[root@web-8 ~]#!ps
ps -ef |grep nginx
root 3686 3434 0 16:19 pts/0 00:00:00 grep --color=auto nginx
[root@web-8 ~]#
[root@web-8 ~]#
[root@web-8 ~]## 看懂了扣 6 不懂7
[root@web-8 ~]#
# 明确,现在用systemctl去管理nginx了
[root@web-8 ~]#systemctl start nginx
查看状态,reload, restart nginx,查看进程id号
[root@web-8 ~]#systemctl status nginx
[root@web-8 ~]#systemctl reload nginx # worker变化,master不变
[root@web-8 ~]#systemctl restart nginx # 整个nginx进程变化
# 用什么命令启动的,就用什么方式去管理该进程
安装完了之后,后续nginx的所有功能,都是围绕着修改nginx配置文件生效了
看懂配置文件,运维来说,达到手写nginx配置文件,才是合格的。
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
~