Nginx
概述
简介
Nginx(Engine x)是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器,由伊戈尔·赛索耶夫开发。
其将源代码以类BSD许可证的形式发布,并且因其稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。其特点是占有内存少,并发能力强。
能够支持高达 50,000 个并发连接数的响应
作用
-
Http代理
-
正向代理
代理客户端
-
反向代理
代理服务器
-
-
负载均衡
负载均衡策略:
-
内置策略
- 轮询
依次循环访问
- 加权轮询
根据服务器权重值访问
- IPhash
对请求客户端的IP进行hash操作,根据hash结果将同一客户端的请求分发给同一台服务器进行处理,解决session不共享问题
-
扩展策略
-
-
动静分离
将网站中的动态资源(数据展示)与静态资源(css\html\image\js)分离,缓存静态资源,提高资源响应速度
安装
windows安装:
- 下载压缩包,nginx/Windows-1.20.1
- 解压到英文目录
- CMD窗口启动nginx.exe
- 默认监听80端口
Linux安装:
-
下载压缩包,nginx-1.20.1
-
解压
-
执行手动安装:
./configure make make install
安装目录:==usr/local/nginx==
配置文件:==usr/local/nginx/conf/nginx.conf==
-
nginx操作
cd /usr/local/nginx/sbin ./nginx # 启动nginx ./nginx -s stop # 停止nginx ./nginx -s quit # 安全退出 ./nginx -s reload # 重新加载配置文件 ps aux|grep nginx # 查看nginx进程
实际使用
nginx.conf
## 全局配置
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
# 最大连接数
events {
worker_connections 1024;
}
# HTTP配置
http {
# HTTP中的全局配置
include mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
# 负载均衡配置
upstream test{
# 服务器资源 weight:权重值
127.0.0.1:8081 weight=1;
127.0.0.1:8082 weight=2;
}
# 每个服务
server {
listen 80; # 监听端口
server_name localhost;
# 默认地址代理
location / {
root html;
index index.html index.htm;
proxy_pass http://test;
}
# 代理 localhost/admin
location /admin {
root html;
index index.html index.htm;
proxy_pass http://test;
}
# redirect server error pages to the static page /50x.html
# 将服务器错误页面重定向到静态页面/50x.html
error_page 500 502 503 504 /50x.html;
# 代理
location = /50x.html {
root html;
}
# HTTPS server
# 代理HTTPS服务
#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;
# }
#}
}
}