一、安装nginx以及解决遇到的问题
1、安装
1)安装依赖包
sudo yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
2)进入nginx目录
cd /usr/local/nginx-1.18.0
3)执行命令
sudo ./configure
sudo make
sudo make install
4)ubantu 安装nginx
https://www.cnblogs.com/EasonJim/p/7806879.html
5)ubantu 下卸载nginx
https://www.cnblogs.com/zhaoyingjie/p/6840616.html
2、问题解决
1)报错:
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
解决方法
yum -y install pcre-devel
2)报错:
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
解决方法
yum install -y zlib-devel
二、nginx核心配置
1、配置文件结构
# main段配置信息
user nginx; # 运行用户,默认即是nginx,可以不进行设置
worker_processes auto; # Nginx 进程数,一般设置为和 CPU 核数一样
error_log /var/log/nginx/error.log warn; # Nginx 的错误日志存放目录
pid /var/run/nginx.pid; # Nginx 服务启动时的 pid 存放位置
# events段配置信息
events {
use epoll; # 使用epoll的I/O模型(如果你不知道Nginx该使用哪种轮询方法,会自动选择一个最适合你操作系统的)
worker_connections 1024; # 每个进程允许最大并发数
}
# http段配置信息
# 配置使用最频繁的部分,代理、缓存、日志定义等绝大多数功能和第三方模块的配置都在这里设置
http {
# 设置日志模式
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; # Nginx访问日志存放位置
sendfile on; # 开启高效传输模式
tcp_nopush on; # 减少网络报文段的数量
tcp_nodelay on;
keepalive_timeout 65; # 保持连接的时间,也叫超时时间,单位秒
types_hash_max_size 2048;
include /etc/nginx/mime.types; # 文件扩展名与类型映射表
default_type application/octet-stream; # 默认文件类型
include /etc/nginx/conf.d/*.conf; # 加载子配置项
# server段配置信息
server {
listen 80; # 配置监听的端口
server_name localhost; # 配置的域名
# location段配置信息
location / {
root /usr/share/nginx/html; # 网站根目录
index index.html index.htm; # 默认首页文件
deny 172.168.22.11; # 禁止访问的ip地址,可以为all
allow 172.168.33.44;# 允许访问的ip地址,可以为all
}
error_page 500 502 503 504 /50x.html; # 默认50x对应的访问页面
error_page 400 404 error.html; # 同上
}
}
1)main 全局配置,对全局生效;
2)events 配置影响 Nginx 服务器与用户的网络连接;
3)http 配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置;
4)server 配置虚拟主机的相关参数,一个 http 块中可以有多个 server 块;
5)location 用于配置匹配的 uri ;
6)upstream 配置后端服务器具体地址,负载均衡配置不可或缺的部分;
2、配置文件 main 段核心参数
1)指定运行 Nginx 的 woker 子进程的属主和属组,其中组可以不指定。
user USERNAME [GROUP]
user nginx lion; # 用户是nginx;组是lion
2)指定运行 Nginx master 主进程的 pid 文件存放路径
pid /opt/nginx/logs/nginx.pid # master主进程的的pid存放在nginx.pid的文件
3)指定 worker 子进程可以打开的最大文件句柄数。
worker_rlimit_nofile 20480; # 可以理解成每个worker子进程的最大连接数量。
4)指定 worker 子进程异常终止后的 core 文件,用于记录分析问题。
worker_rlimit_core 50M; # 存放大小限制
working_directory /opt/nginx/tmp; # 存放目录
5)指定 Nginx 启动的 worker 子进程数量。
worker_processes 4; # 指定具体子进程数量
worker_processes auto; # 与当前cpu物理核心数一致
6)指定 worker 子进程优雅退出时的超时时间。
worker_shutdown_timeout 5s;
7)指定 Nginx 的运行方式,前台还是后台,前台用于调试,后台用于生产。
daemon off; # 默认是on,后台运行模式
3、配置文件 events 段核心参数
1)Nginx 使用何种事件驱动模型。
use method; # 不推荐配置它,让nginx自己选择
method 可选值为:select、poll、kqueue、epoll、/dev/poll、eventport
2)worker 子进程能够处理的最大并发连接数。
worker_connections 1024 # 每个子进程的最大连接数为1024
3)是否打开负载均衡互斥锁。
accept_mutex on # 默认是off关闭的,这里推荐打开
4、location
1)location 中的反斜线
A : 不带 / 当访问 www.nginx-test.com/test 时, Nginx 先找是否有 test 目录,如果有则找 test 目录下的 index.html ;如果没有 test 目录, nginx 则会找是否有 A:test 文件。
B : 带 / 当访问 www.nginx-test.com/test 时, Nginx 先找是否有 test 目录,如果有则找 test 目录下的 index.html ,如果没有它也不会去找是否存在 test 文件。
5、nginx 四层代理
转发redis, mysql ,rabbitmq
stream {
upstream cloudsocket {
server 172.16.227.140:3306 ;
}
server {
listen 3306;#数据库服务器监听端口
proxy_connect_timeout 10s;
proxy_timeout 300s;#设置客户端和代理服务之间的超时时间,如果5分钟内没操作将自动断开。
proxy_pass cloudsocket;
}
}
6、设置超时
proxy_send_timeout 900s; # 设置发送超时时间,
proxy_read_timeout 900s; 、
三、nginx系统命令:
1、开机配置
systemctl enable nginx # 开机自动启动
systemctl disable nginx # 关闭开机自动启动
2、启动Nginx
systemctl start nginx # 启动Nginx成功后,可以直接访问主机IP,此时会展示Nginx默认页面
3、停止Nginx
systemctl stop nginx
4、重启Nginx
systemctl restart nginx
5、重新加载Nginx
systemctl reload nginx
6、查看 Nginx 运行状态
systemctl status nginx
7、查看Nginx进程
ps -ef | grep nginx
8、杀死Nginx进程
kill -9 pid # 根据上面查看到的Nginx进程号,杀死Nginx进程,-9 表示强制结束进程
四、nginx应用程序命令
nginx -s reload # 向主进程发送信号,重新加载配置文件,热重启
nginx -s reopen # 重启 Nginx
nginx -s stop # 快速关闭
nginx -s quit # 等待工作进程处理完成后关闭
nginx -T # 查看当前 Nginx 最终的配置
nginx -t # 检查配置是否有问题