一、Nginx概述
Nginx是一款轻量级的Web服务器、反向代理服务器,由于它的内存占用少,启动极快,高并发能力强,在互联网项目中广泛应用
二、Nginx安装
- 环境准备
- gcc yun install gcc-c++
- zlib yum install -y zlib zlib-devel
- prce yum install -y prce prce-devel
- opsnssl yum install -y openssl openssl-devel
- 下载nginx解压
- wget https://nginx.org/download/nginx-1.20.2.tar.gz
- tar -zxvf nginx-1.20.2.tar.gz
- 进入目录执行命令
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
自定义配置信息,非必要,可以直接执行 ./configure
- 执行make命令编译 执行make install安装
- 安装完成后到nginx目录启动,默认访问的端口是80端口
- 启动 ./nginx
- 指定配置文件启动 ./nginx -c nginx.conf
- 强制关闭 ./nginx -s stop
- 退出 ./nginx -s quit
- 重新加载配置文件,先关闭后启动 ./nginx -s reload
- 检查配置文件 ./nginx -c nginx.conf -t
三、Nginx配置文件
# 配置worker进程运行用户,nobody也是一个linux用户,一般用于启动程序,没有密码
#user nobody;
# 配置工作进程数量,根据硬件调整,通常等于CPU数量或者2倍CPU数量
worker_processes 1;
# 配置全局错误日志以及类型 默认级别是error. debug| info | notice | warn | error | crit
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
# 配置进程pid文件
#pid logs/nginx.pid;
# 配置工作模式和链接数
events {
# 配置每个worker进程链接数上限,nginx支持的总连接数就等于worker_connections * worker_processes,上限是65535
worker_connections 1024;
}
# 配置http服务器,利用它的反向代理功能实现负载均衡
http {
# 配置nginx支持哪些多媒体类型,具体类型查看conf/mime.types
include 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 日志及存放路径,并使用上面定义的main日志格式
#access_log logs/access.log main;
# 开启高效文件传输模式
sendfile on;
# 防止网络阻塞
#tcp_nopush on;
#keepalive_timeout 0;
# 长连接超时时间
keepalive_timeout 65;
# 开启gzip压缩输出
#gzip on;
# 配置虚拟主机
server {
listen 80; # 配置监听端口号
server_name localhost; # 配置服务名
#charset koi8-r; # 配置字符集
#access_log logs/host.access.log main; # 配置本虚拟主机的访问日志
# 默认的匹配斜杠的请求,当访问路径中有斜杠,会被该location匹配到并进行处理
location / {
# root是配置服务器的默认网站根目录位置,默认位nginx安装目录下面的html目录
root html;
# 配置首页文件的名称
index index.html index.htm;
}
# 配置404页面
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; # 配置50x错误页面
# 精确匹配
location = /50x.html {
root html;
}
# PHP脚本请求全部转发到Apache处理
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# PHP脚本请求全部转发到FastCGI处理
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
四、配置反向代理
五、nginx的负载均衡策略
- 轮询:每个请求会按时间顺序逐一分配到不同的后端服务器。
- 权重:权重方式,在轮询策略的基础上指定轮询的几率
- ip_hash:ip绑定,每个请求按照访问ip的hash分配,这个方法确保了相同的客户端请求一直发送到相同的服务器,以保证session会话。这样每个用户都固定访问一个后端服务器,可以解决session不能跨服务器的问
- least_conn:把请求转发给链接数量较少的后端服务器。
- fair(第三方策略):按照服务器端的响应时间来分配请求,响应时间短的优先分配
- url_hash(第三方策略):按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,要配合缓存命中来使用。