nginx概要

2023-07-29,

新机(CentOS7)配置nginx:

一. 更新yum源为阿里云镜像

ping mirrors.aliyun.com
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# 清除yum缓存
yum clean all
# 缓存阿里云源
yum makecache
# 测试阿里云源
yum list

二. 下载安装nginx

#查看yum的nginx信息
yum info nginx
#安装
yum install nginx
#启动nginx
systemctl start nginx.service
#重启nginx
nginx -s reload
#(可选)检查nginx配置文件是否正确
nginx -t

三. 配置文件要点

默认安装配置文件位于/etc/nginx/nginx.conf,默认站点目录位于/usr/share/nginx/html/ 。

 入门配置:

优化配置:

几个关键配置:worker_processes,events配置

events {
use epoll; #注意epoll
worker_connections 50000;
multi_accept on;
}

keepalive_timeout  65;

buffer模块:client_max_body_size

gzip压缩

XSS相关:

主要是配置相关响应头:

X-Frame-Options
X-Content-Type-Options

...

负载均衡:

     # xxx环境
#默认普通轮询,请求平均分发到各服务器
upstream uat.test.com{
#server 10.210.0.8:80;
server 10.210.0.9:80;
}

nginx内置负载均衡策略有三种:

轮询(普通轮询,权重轮询);

最少连接(把请求分发给连接最少的服务器);

ip_hash 每个请求固定访问一个后端服务器。

官方参考移步

**nginx用作web服务器,生产环境中一般会直接使用Tengine,淘宝的nginx项目。

nginx概要的相关教程结束。

《nginx概要.doc》

下载本文的Word格式文档,以方便收藏与打印。