- Nginx 版本类型
- Nginx yum 安装
- 了解 Nginx 配置文件
- Nginx 编译参数详解(源码编译安装时使用)
- Nginx 基本配置
- 创建Nginx 虚拟主机
1.进入nginx官网下载页面:http://nginx.org/en/download.html 提供了 3 种类型的 nginx 版本可下载分别是:
Nginx yum 安装
- Mainline version:
主线版本,即开发版本- Stable version:
最新稳定版,生产环境上建议使用版本- Legacy version:
遗留的老版本的稳定版
- 如果你想使用 nginx 源码编译安装可以直接在 nginx下载页面 下载 Stable version 版本源码包。
- 这次我们使用 yum 安装 nginx,操作系统选择 centos
- 进入 RHEL/CentOS 页面查看安装文档
- 首先安装 yum 工具集用来更新yum 仓库源
sudo yum install yum-utils- 创建nginx yum配置文件
vim /etc/yum.repos.d/nginx.repo
[nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true
配置意思:
[nginx-stable] #nginx 稳定最新版
name=nginx stable repo # 名称
baseurl=http://nginx.org/packages/centos/ r e l e a s e v e r / releasever/ releasever/basearch/ # 源地址
gpgcheck=1 # 需要校验
enabled=1 # 配置开启
gpgkey=https://nginx.org/keys/nginx_signing.key #校验地址
module_hotfixes=true #模块是否可用
- 如果现在直接使用 sudo yum install nginx
安装的将是稳定最新版本 - 使用 getenforce 查看 SELinux 是否关闭,如果没关闭可能没有权限读取文件导致403错误,关闭修改 vi /etc/selinux/config
# This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. #开启 #SELINUX=enforcing #关闭 SELINUX=disabled # SELINUXTYPE= can take one of three values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted
修改完需要重启系统才生效 reboot
6. 关闭防火墙
systemctl status firewalld #查看防火墙状态
systemctl stop firewalld # 关闭防火墙
- 启动 nginx :
systemctl start nginx
systemctl enable nginx
- 启动后可以直接访问 有个默认nginx主页
- 使用 rpm -ql nginx 查看nginx 所有配置文件
Nginx 编译参数详解(源码编译安装时使用)/etc/logrotate.d/nginx #nginx日志轮转文件
/etc/nginx/nginx.conf #主配置文件
/etc/nginx/conf.d #子配置文件
/etc/nginx/conf.d/default.conf # 默认的网站配置文件
/etc/nginx/fastcgi_params # 动态网站模块文件 --php所需相关变量
/etc/nginx/scgi_params #动态网站模块文件 — python所需相关变量
/etc/nginx/uwsgi_params #动态网站模块文件 — python所需相关变量
/etc/nginx/mime.types #文件关联程序 (网站文件类型和相关联的处理程序)
/usr/lib/systemd/system/nginx-debug.service #nginx 调试程序启动脚本
/usr/lib/systemd/system/nginx.service systemctl # 服务脚本
/usr/sbin/nginx #nginx主程序
/usr/sbin/nginx-debug #nginx 调试程序
#nginx 使用文档文件
/usr/share/doc/nginx-1.20.2 /usr/share/doc/nginx-1.20.2/COPYRIGHT /usr/share/man/man8/nginx.8.gz /usr/share/nginx /usr/share/nginx/html /usr/share/nginx/html/50x.html /usr/share/nginx/html/index.html
/var/cache/nginx # nginx 工作缓存文件夹
/var/log/nginx #nginx 日志文件夹(nginx 访问日志,错误日志)
/usr/lib64/nginx # nginx 系统库模块目录
- 使用 nginx -V 查看编译参数选项
Nginx 基本配置configure arguments # 配置参数 ./configure -help 查询帮助
--prefix=/etc/nginx #安装路径
--sbin-path=/usr/sbin/nginx # 程序文件
--modules-path=/usr/lib64/nginx/modules #模块路径
--conf-path=/etc/nginx/nginx.conf #主配置文件
--error-log-path=/var/log/nginx/error.log # 错误日志
--http-log-path=/var/log/nginx/access.log # 访问日志
--pid-path=/var/run/nginx.pid # 程序主进程id
--lock-path=/var/run/nginx.lock #锁路径,防止重复启动nginx
--http-client-body-temp-path=/var/cache/nginx/client_temp #缓存
--http-proxy-temp-path=/var/cache/nginx/proxy_temp #代理缓存
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp #php缓存
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp #python 缓存
--http-scgi-temp-path=/var/cache/nginx/scgi_temp
--user=nginx #nginx 启动用户
--group=nginx # nginx 用户组
--with-compat #启动动态模块兼容性(可以处理动态请求)
--with-file-aio #使用nginx的 aio特性会大大提高性能,比如图片站的特点是大量的读io操作,nginx aio 不用等待每次io 的结果,有助于并发处理大量 io 和提高nginx 处理效率。
aio的优点是能够同时提交多个io请求给内核,然后直接由内核的io调度算法去处理这些请求,内核就有可能执行一些合并,节约了读取文件的处理时间。
就是异步非阻塞
--with-threads # 多线程模块
--with-http_addition_module #响应之前或者之后追加文本内容,比如想在站点底部追加一个js广告或者新增的css样式
--with-http_auth_request_module # 认证模块
--with-http_dav_module # 增加支持上传下载put,delete,mkcol创建集合,copy和move方法默认情况下关闭
--with-http_flv_module # 支持添加mp4,FLV视频支持模块
--with-http_gunzip_module# 压缩模块
--with-http_gzip_static_module # 压缩模块
--with-http_mp4_module #多媒体模块
--with-http_random_index_module #随机主页模块(网站微更新机制)
--with-http_realip_module # 获取代理后真实ip模块
--with-http_secure_link_module # 安全链接模块(安全下载模块)
--with-http_slice_module # nginx 中文文档
--with-http_ssl_module # 支持https 安全模块
--with-http_stub_status_module # 分支状态模块(观察网站连接信息)
--with-http_sub_module # nginx 替换网站响应内容
--with-http_v2_module # web2.0
--with-mail # 邮件客户端
--with-mail_ssl_module # 邮件客户端
--with-stream # 负载均衡模块
--with-stream_realip_module # 负载均衡模块
--with-stream_ssl_module # 负载均衡模块
--with-stream_ssl_preread_module # 负载均衡模块
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC'
#cpu优化参数
--with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'#cpu优化参数
- 主配置文件/etc/nginx/nginx.conf由3部分组成
创建Nginx 虚拟主机
- CoreModule 核心模块(进程数等)
全局/核心块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker
- EventsModule 事件驱动模块(工作模式等)
events块:配置影响nginx服务器或与用户的网络连接。有每个进程最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网络连接,开启多个网络连接序列化等。
- HttpCoreModule http内核模块
http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
server块:配置虚拟主机的相关参数,一个http中可以有多个server。
location 块:配置请求路由,以及各种页面的处理情况。
1.建议在子配置目录/etc/nginx/conf.d下创建虚拟主机配置
server{
listen 80; # 监听端口
server_name dalei.com;#服务器域名
#访问位置
location / {
root /dalei; #指定网站根目录
index index.html; #默认访问主页
}
}
- 创建网站根目录 mkdir /dalei
- 写入一些数据到根目录下的index.html 文件 echo “大雷编程” > /dalei/index.html
- 创建本地解析地址 vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.227.128 dalei.com
- 使用ping 命令查看是否能ping 通
- 使用elinks http://dalei.com/ 访问, elinks 是一个纯字符浏览器,如果没安装elinks可以使用 yum install elinks 安装。



