目录
一、Nginx简介
1、nginx特点
Nginx 是一个很强大的高性能Web和反向代理服务,它具有很多非常优越的特性:
在连接高并发的情况下,Nginx是Apache服务不错的替代品:Nginx在美国是做虚拟主机生意的老板们经常选择的软件平台之一。能够支持高达 50,000 个并发连接数。
Nginx作为负载均衡服务:Nginx 既可以在内部直接支持 Rails 和 PHP 程序对外进行服务,也可以支持作为 HTTP代理服务对外进行服务。Nginx采用C进行编写,不论是系统资源开销还是CPU使用效率都比 Perlbal 要好很多。
处理静态文件,索引文件以及自动索引;打开文件描述符缓冲。
无缓存的反向代理加速,简单的负载均衡和容错。
FastCGI,简单的负载均衡和容错。
模块化的结构。包括 gzipping, byte ranges, chunked responses,以及 SSI-filter 等 filter。如果由 FastCG或其它代理服务器处理单页中存在的多个 SSI,则这项处理可以并行运行,而不需要相互等待。
Nginx 是一个安装非常的简单、配置文件非常简洁(还能够支持perl语法)、Bug非常少的服务。Nginx 启动特别容易,并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动。你还能够不间断服务的情况下进行软件版本的升级
2、应用场景
二、Nginx安装
安装方式
- epol源安装
- 官方源安装
- 源码包安装
1、epol源安装
[root@web03 ~] # yum install -y nginx
2、官方源安装
1.配置官方源
[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx-stable]name = nginx stable repobaseurl = http://nginx.org/packages/centos/7/ $basearch /gpgcheck = 1enabled = 1gpgkey = https://nginx.org/keys/nginx_signing.keymodule_
2.安装依赖
[root@web01 ~] # yum install -y gcc gcc-c ++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree
3.安装nginx[root@web01 ~] # yum install -y nginx
如果在生产环境中有同一用户需求需要修改nginx配置文件4.配置nginx[root@web01 ~] # vim /etc/nginx/nginx.confuser: 统一用户名;
5.启动服务
①方法一:
[root@web01 ~]# systemctl start nginx
②方法二:
[root@web01 ~]# nginx
6.检查启动
①方式一:
[root@web01 ~]# systemctl starts nginx
②方式二:
[root@web01 ~]# ps -ef | grep nginx
③方式三:
[root@web01 ~]# netstat -lntp | grep 80 #nginx服务默认端口80
④方式四:
[root@web01 ~]# nginx -v
三、nginx常用命令
1.nginx启动
①:方法一:
[root@web01 ~]# systemctl start nginx
②:方法二:
[root@web01 ~]# nginx
#注意:使用什么方法启动的,就使用什么方法关闭
2.nginx停止
①:方法一:
[root@web01 ~]# systemctl stop nginx
②:方式二:
[root@web01 ~]# nginx -s reload
3.nginx重启
①:方法一:
[root@web01 ~]# systemctl restart nginx
4.nginx重载,重新加载配置文件
①:方法一:
[root@web01 ~]# systemctl reload nginx
②:方法二:
[root@web01 ~]# nginx -s reload
5.加入开机自启
①:方法一:
[root@web01 ~]# systemctl enable nginx
四、源码包安装
1.下载安装包
[root@web01 ~]# wget http://nginx.org/download/nginx-1.16.1.tar.gz
2.解压
[root@web01 ~]# tar xf nginx-1.16.1.tar.gz
3.创建用户 #在生产环境中公司服务器可能需要统一用户,便于更好管理服务器
[root@web01 ~] # groupadd www -g 666[root@web01 ~] # useradd www -u 666 -g 666
4.生成
[root@web01 ~]# cd nginx-1.16.1
[root@web01 nginx-1.16.1]# ./configure --prefix=/usr/local/nginx-1.16.1 --user=www -- group=www --without-http_gzip_module
5.编译安装
[root@web01 nginx-1.16.1]# make && make install
6.做软链接
[root@web01 nginx-1.16.1]# ln -s /usr/local/nginx-1.16.1 /usr/local/nginx
7.配置环境变量
[root@web01 ~]# vim /etc/profile.d/nginx.sh
export PATH=$PATH:/usr/local/nginx/sbin[root@web01~]# source /etc/profile
8.启动nginx
#启动时没有办法使用system管理,需要自行手动配置文件
[root@web01 ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target#启动
[root@web01~]# systemctl start nginx
五、nginx添加模块
1.安装依赖
[root@web01 nginx-1.16.1]# yum install -y openssl openssl-devel
2.再生成一次
[root@web01 nginx-1.16.1]# ./configure --prefix=/usr/local/nginx-1.16.1-new --user=www --group=www --without-http_gzip_module --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --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' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
3.安装
[root@web01 nginx-1.16.1]# make && make install
4.重做软链接
[root@web01 nginx-1.16.1]# rm -rf /usr/local/nginx && ln -s /usr/local/nginx-1.16.0-new /usr/local/nginx
5.重启服务
[root@web01 nginx-1.16.1]# systemctl restart nginx
六、nginx相关文件
为了让大家更清晰的了解nginx的全貌,可使用rpm -ql nginx查看整体的目录结构及应对的功能,,如下表整理了nginx比较重要的配置文件及路径
1.Nginx主配置文件
路径 类型 作用 /etc/nginx/nginx.conf 配置文件 nginx主配置文件 /etc/nginx/conf.d/default 配置文件 默认网站配置文件
2.Nginx代理相关参数文件
路径 类型 作用 /etc/nginx/fastcgi_params 配置文件 Fastcgi代理配置文件 /etc/nginx/scgi_params 配置文件 scgi代理配置文件 /etc/nginx/uwsgi_params 配置文件 uwsgi代理配置文件
3.Nginx编码相关配置文件
路径 类型 作用 /etc/nginx/win_utf 配置文件 Nginx编码转换映射文件 /etc/nginx/koi-utf 配置文件 Nginx编码转换映射文件 /etc/nginx/koi-win 配置文件 Nginx编码转换映射文件 /etc/nginx/mime.types 配置文件 Content-Type与拓展名
4.Nginx管理相关命令
路径 类型 作用 /usr/sbin/nginx 命令 Nginx命令行管理终端工具 /usr/sbin/nginx-debug 命令 Nginx命令行与终端调试工具
5.Nginx日志相关目录与文件
路径 类型 作用 /var/log/nginx 目录 Nginx默认存放日志目录 /var/logrotate.d/nginx 配置文件 Nginx默认日志切割
五、nginx配置文件详解
Nginx主配置文件/etc/nginx/nginx.conf是一个纯文本类型的文件,整个配置文件是以区块的形似组织的。一般,每个区块以一对大括号{}来表示开始于结束。
Nginx主配置文件整体分为三块,分别是CoreModule(核心模块)、EventModule(事件驱动模块)、HttpCoreModule(http内核模块)。
[root@web01 ~]# cat /etc/nginx/nginx.conf
#########################核心模块####################
#指定启动的用户
user www;
#nginx的worker进程的数量
worker_processes 1;
#指定错误日志存放的路径以及记录的级别 debug/info/notice/warn/error/emerg
error_log /var/log/nginx/error.log warn;
#指定pid文件
pid /var/run/nginx.pid;########################事件驱动模块#################
events {
#每个worker工作进程的最大连接数
worker_connections 1024;
}######################http内核模块###################
http {
#包含,nginx可识别的文件类型
include /etc/nginx/mime.types;
#当nginx不识别文件类型的时候,默认下载
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"';
#指定访问日志存储路径与格式
access_log /var/log/nginx/access.log main;
#高效传输
sendfile on;
#高效传输
#tcp_nopush on;
#开启长连接
keepalive_timeout 65;
#开启压缩
#gzip on;
#包含网站的配置文件
include /etc/nginx/conf.d/*.conf;
#一个server表示一个网站
server {
#监听端口
listen 80;
#网站提供的域名
server_name localhost;
#字符集
charset utf8;
#匹配、控制访问的网站站点
location / {
#指定站点目录
root /usr/share/nginx/html;
#指定默认访问的页面
index index.html index.htm;
}
}
}