先看2个实际需求,引出Nginx
需求1:访问不同微服务
需求2:轮询访问服务
解决方案:Nginx
反向代理
负载均衡
动静分离
高可用集群
Nginx 在分布式 微服务 架构的位置
基本介绍
Nginx是什么?能干什么
Nginx 下载&安装&启动
下载
安装
安装Linux,登录Linux
ping www.baidu.com
具体安装步骤
1.搭建gcc环境
yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
2.将nginx-1.20.2.tar.gz 上传到Linux /opt目录
3.切换到/opt目录,解压nginx-1.20.2.tar.gz
tar -zxvf nginx-1.20.2.tar.gz
3.1将解压后的文件放到指定位置
mv nginx-1.20.2 /usr/local/nginx
4.进入文件目录
cd /usr/local/nginx
5.配置nginx路径
./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 --conf-path=/usr/local/nginx/nginx.conf
6.补全nginx配置目录
mkdir /var/temp/nginx -p
7.编译并安装
make && make install
8.测试配置与nginx是否正常,当出现successful即可
./sbin/nginx -t
9.启动nginx
./sbin/nginx -c nginx.conf
10.查看进程/或端口
ps -ef | grep nginx
验证是否安装成功
设置防火墙,让Windows访问Nginx
具体配置
-
查看开放的端口号
firewall-cmd --list-all -
设置开放的端口号
firewall-cmd --add-service=http --permanent
firewall-cmd --add-service=80/tcp --permanent -
重启防火墙
firewall-cmd --reload
Nginx 命令行参数
指令说明
使用演示
- 启动 /usr/local/nginx/sbin/nginx -c nginx.conf
- 停止 /usr/local/nginx/sbin/nginx -s stop
- 重新加载(不需要重启) /usr/local/nginx/sbin/nginx -s reload
- 查看版本 /usr/local/nginx/sbin/nginx -v
- 查看版本,配置参数 /usr/local/nginx/sbin/nginx -V
nginx.conf 配置文件
基本说明
Nginx的配置文件位置
多说一句:使用 /usr/local/nginx/sbin/nginx 启动Nginx,默认用的是安装目录\nginx.conf 配置文件
作用:完成对Nginx的各种配置,包括端口,并发数,重写规则等
nginx.conf组成
全局块
events 块
http 块
nginx.conf 详细文档
详细内容
#Nginx用户及组:用户 组。window下不指定
#user nobody;
#工作进程:数目。根据硬件调整,通常等于CPU数量或者2倍于CPU。
worker_processes 1;
#错误日志:存放路径。
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid(进程标识符):存放路径
pid /usr/local/nginx/logs/nginx.pid;
#一个进程能打开的文件描述符最大值,理论上该值因该是最多能打开的文件数除以进程数。
#但是由于nginx负载并不是完全均衡的,所以这个值最好等于最多能打开的文件数。
#LINUX系统可以执行 sysctl -a | grep fs.file 可以看到linux文件描述符。
worker_rlimit_nofile 65535;
events {
#使用epoll的I/O 模型。linux建议epoll,FreeBSD建议采用kqueue,window下不指定。
use epoll;
#单个进程最大连接数(最大连接数=连接数*进程数)
worker_connections 1024;
#客户端请求头部的缓冲区大小。这个可以根据你的系统分页大小来设置,
#一般一个请求头的大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。
#client_header_buffer_size 4k;
}
http {
#设定mime类型,类型由mime.type文件定义
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"';
#用了log_format指令设置了日志格式之后,需要用access_log指令指定日志文件的存放路径
#记