1、nginx简介
-
什么是nginx
nginx是高性能HTTP和反向代理的服务器,处理高并发能力十分强大,能经受高负载的考验,有报告表明能支持高达 50,000 个并发连接数。 -
正向代理
需要在客户端配置代理服务器进行指定网站访问
-
反向代理
暴露的是代理服务器地址,隐藏了真实服务器 IP 地址。
-
负载均衡
增加服务器的数量,然后将请求分发到各个服务器上,将原先请求集中到单个服务器上的情况改为将请求分发到多个服务器上,将负载分发到不同的服务器,也就是我们所说的负载均衡
-
动静分离
2、nginx安装
- 安装需要的pcre、openssl、zlib、gcc依赖
- 安装nginx
解压安装命令:
tar -xvf xxx
./configure
make && make install
linux需要开放访问端口
3、nginx常用命令
- 查看nginx版本
./nginx -v
- 启动nginx
./nginx
- 停止nginx
./nginx -s stop
- 重新加载配置
./nginx -s reload
4、nginx的配置文件
- nginx配置文件位置
/usr/local/nginx/conf/nginx.conf
- 内容:
- 全局块:配置服务器整体运行的配置指令
比如 worker_processes 1;处理并发数的配置 - events 块:影响 Nginx 服务器与用户的网络连接
比如 worker_connections 1024; 支持的最大连接数为 1024 - http 块
还包含两部分:
http 全局块
server 块
5、配置实例
- 反向代理
server {
listen 80;
server_name localhost;
location / {
root html;
proxy_pass http://127.0.0.1:8080;
}
}
即使用localhost:80访问 / 时会被转发到http://127.0.0.1:8080。
- 负载均衡
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream myserver {
server 127.0.0.1:8080;
server 127.0.0.1:8081;
ip_hash;
}
server {
listen 80;
server_name localhost;
location / {
root html;
proxy_pass http://myserver;
}
}
}
即使用localhost:80访问 / 的请求会被平均分配到127.0.0.1:8080和127.0.0.1:8081。