nginx相关安装
相关命令
nginx -v ##查看版本
nginx -V ##查看所以文件路径和已安装模块
nginx -t ##检查conf文件语法
nginx -c /data/nginx.conf ##指定conf文件启动
nginx -s start|stop|reload|restart ##执行指令
yum安装
yum -y install nginx
相关路径
nginx -V ##查看路径及安装的模块
编译安装
编译环境安装
yum -y install gcc gcc-c++ pcre pcre-devel openssl openssl-devel zlib zlib-devel gd gd-devel
下载tar包,创建用户
useradd -s /sbin/nologin nginx
wget http://nginx.org/download/nginx-1.16.0.tar.gz
tar -zxvf nginx-1.16.0.tar.gz
cd nginx-1.16.0
编译配置
./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-pcre \
--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_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-http_image_filter_module \
--with-http_slice_module \
--with-mail \
--with-threads \
--with-file-aio \
--with-stream \
--with-mail_ssl_module \
--with-stream_ssl_module
make && make install
cd /usr/local/nginx/sbin
./nginx # 启动Nginx
./nginx -t # 验证配置文件是正确
./nginx -s reload # 重启Nginx
./nginx -v # 查看是否安装成功
配置systemctl服务管理
vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -S reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
给执行权限
chmod +x /usr/lib/systemd/system/nginx.service
systemctl daemon-reload
systemctl start nginx.service
systemctl status nginx
systemctl enable nginx.service
相关路径
可在编译配置时设定相关路径
--prefix=/usr/share/nginx # 指向安装目录
--conf-path=/etc/nginx/nginx.conf # 指定配置文件
--http-log-path=/var/log/nginx/access.log # 指定访问日志
--error-log-path=/var/log/nginx/error.log # 指定错误日志
--lock-path=/var/lock/nginx.lock # 指定lock文件
--pid-path=/run/nginx.pid # 指定pid文件
所有模块详解
# 模块参数具体功能
--with-cc-opt='-g -O2 -fPIE -fstack-protector' # 设置额外的参数将被添加到CFLAGS变量。(FreeBSD或者ubuntu使用)
--param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2'
--with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now'
--prefix=/usr/share/nginx # 指向安装目录
--conf-path=/etc/nginx/nginx.conf # 指定配置文件
--http-log-path=/var/log/nginx/access.log # 指定访问日志
--error-log-path=/var/log/nginx/error.log # 指定错误日志
--lock-path=/var/lock/nginx.lock # 指定lock文件
--pid-path=/run/nginx.pid # 指定pid文件
--http-client-body-temp-path=/var/lib/nginx/body # 设定http客户端请求临时文件路径
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi # 设定http fastcgi临时文件路径
--http-proxy-temp-path=/var/lib/nginx/proxy # 设定http代理临时文件路径
--http-scgi-temp-path=/var/lib/nginx/scgi # 设定http scgi临时文件路径
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi # 设定http uwsgi临时文件路径
--with-debug # 启用debug日志
--with-pcre-jit # 编译PCRE包含“just-in-time compilation”
--with-ipv6 # 启用ipv6支持
--with-http_ssl_module # 启用ssl支持
--with-http_stub_status_module # 获取nginx自上次启动以来的状态
--with-http_realip_module # 允许从请求标头更改客户端的IP地址值,默认为关
--with-http_auth_request_module # 实现基于一个子请求的结果的客户端授权。如果该子请求返回的2xx响应代码,所述接入是允许的。如果它返回401或403中,访问被拒绝与相应的错误代码。由子请求返回的任何其他响应代码被认为是一个错误。
--with-http_addition_module # 作为一个输出过滤器,支持不完全缓冲,分部分响应请求
--with-http_dav_module # 增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法 默认关闭,需编译开启
--with-http_geoip_module # 使用预编译的MaxMind数据库解析客户端IP地址,得到变量值
--with-http_gunzip_module # 它为不支持“gzip”编码方法的客户端解压具有“Content-Encoding: gzip”头的响应。
--with-http_gzip_static_module # 在线实时压缩输出数据流
--with-http_image_filter_module # 传输JPEG/GIF/PNG 图片的一个过滤器)(默认为不启用。gd库要用到)
--with-http_spdy_module # SPDY可以缩短网页的加载时间
--with-http_sub_module # 允许用一些其他文本替换nginx响应中的一些文本
--with-http_xslt_module # 过滤转换XML请求
--with-mail # 启用POP3/IMAP4/SMTP代理模块支持
--with-mail_ssl_module # 启用ngx_mail_ssl_module支持启用外部模块支持
nginx.conf详解
# 全局参数设置
user nginx/www; # 运行进程的用户
worker_processes 1; # 设置nginx启动进程的数量,一般设置成与逻辑cpu数量相同
error_log /var/log/nginx/error.log warn; # 指定错误日志(建议绝对路劲)
worker_rlimit_nofile 102400; # 设置一个nginx进程能打开的最大文件数
pid /var/run/nginx.pid;
events { # 事件配置
worker_connections 10240; # 设置一个进程的最大并发连接数
use epoll; # 事件驱动类型
}
# 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"';
log_format test
access_log /var/log/nginx/access.log test; #设置访问日志的位置和格式
sendfile on; # 用于开启文件高效传输模式,一般设置为on,若nginx是用来进行磁盘IO负载应用时,可以设置为off,降低系统负载
tcp_nopush on; # 减少网络报文段数量,当有数据时,先别着急发送, 确保数据包已经装满数据, 避免了网络拥塞
tcp_nodelay on; # 提高I/O性能,确保数据尽快发送, 提高可数据传输效率
gzip on; # 是否开启 gzip 压缩
keepalive_timeout 65; # 设置长连接的超时时间,请求完成之后还要保持连接多久,不是请求时间多久,目的是保持长连接,减少创建连接过程给系统带来的性能损耗,类似于线程池,数据库连接池
types_hash_max_size 2048; # 影响散列表的冲突率。越大,就会消耗更多的内存,但散列key的冲突率会降低,检索速度就更快。越小,消耗的内存就越小,但散列key的冲突率可能上升
include /etc/nginx/mime.types; # 关联mime类型,关联资源的媒体类型(不同的媒体类型的打开方式)
default_type application/octet-stream; # 根据文件的后缀来匹配相应的MIME类型,并写入Response header,导致浏览器播放文件而不是下载
# 虚拟服务器的相关设置
server {
listen 80; # 设置监听的端口
server_name localhost; # 设置绑定的主机名、域名或ip地址
charset utf8; # 设置编码字符
location / {
root /var/www/nginx; # 设置服务器默认网站的根目录位置
index index.html index.htm index.php; # 设置默认打开的文档
}
error_page 500 502 503 504 /50x.html; # 设置错误信息返回页面
location = /50x.html {
root html; # 这里的绝对位置是/var/www/nginx/html
}
}
}
负载均衡相关
upstream mysvr {
server http://192.168.152.100:8000; #代理的web1
server http://192.168.152.101:8000; #代理的web2
}
server {
listen 81;
server_name web;
charset utf-8;
location / {
proxy_pass http://mysvr; #请求转向 mysvr 定义的服务器列表
proxy_set_header Host $http_host; ##相信真实请求信息发给代理机器
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
nginx-porxy详细配置
proxy_pass :真实web/app服务器
proxy_redirect : 如果真实服务器使用的是的真是IP:非默认端口。则改成IP:默认端口。
proxy_set_header:重新定义或者添加发往后端服务器的请求头
proxy_set_header X-Real-IP :启用客户端真实地址(否则日志中显示的是代理在访问网站)
proxy_set_header X-Forwarded-For:记录代理地址
proxy_connect_timeout::后端服务器连接的超时时间发起三次握手等候响应超时时间
proxy_send_timeout:后端服务器数据回传时间就是在规定时间之内后端服务器必须传完所有的数据
proxy_read_timeout :nginx接收upstream(上游/真实) server数据超时, 默认60s, 如果连续的60s内没有收到1个字节, 连接关闭。像长连接
proxy_buffering on; 开启缓存
proxy_buffer_size 32k; 只是响应头的缓冲区
proxy_buffers 4 128k; 内容缓冲区域大小
proxy_busy_buffers_size 256k; 从proxy_buffers划出一部分缓冲区来专门向客户端传送数据的地方
proxy_max_temp_file_size 256k; 超大的响应头存储成文件。
upstream 算法
upstream myweb {
server 192.168.152.100:8080;
server 192.168.152.101:8080 backup; #热备
}
upstream myweb {
server 192.168.152.100:8080;
server 192.168.152.101:8080; #无规则正常轮询
}
upstream myweb {
server 192.168.152.100:8080 weight=1;
server 192.168.152.101:8080 weight=2;
} #注意 值越大分配的请求越多
upstream myweb {
server 192.168.152.100:8080;
server 192.168.152.101:8080;
ip_hash; #ip哈希相同的客户端ip请求相同的服务器
}
upstream myweb {
server 192.168.152.100:8080 weight=2 max_fails=2 fail_timeout=2;
server 192.168.152.101:8080 weight=1 max_fails=2 fail_timeout=1;
}
- down,表示当前的server暂时不参与负载均衡。
- backup,预留的备份机器。当其他所有的非backup机器出现故障或者忙的时候,才会请求backup机器,因此这台机器的压力最轻。
- max_fails,允许请求失败的次数,默认为1。当超过最大次数时,返回 proxy_next_upstream 模块定义的错误。
- fail_timeout,在经历了max_fails次失败后,暂停服务的时间。max_fails 可以和 fail_timeout一起使用。