一、简述nginx:
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。
其特点是占有内存少,并发能力强,
Nginx作为负载均衡服务:既可以在内部直接支持 Rails 和 PHP 程序对外进行服务,也可以支持作为 HTTP代理服务对外进行服务。
Nginx采用C进行编写,不论是系统资源开销还是CPU使用效率都比 Perlbal 要好很多。
处理静态文件,索引文件以及自动索引;打开文件描述符缓冲。无缓存的反向代理加速,简单的负载均衡和容错。
1.nginx的基础配置
1.下载nginx并解压
安装包:
nginx-1.17.1.tar.gz
#1.在官网上下载nginx
[root@server1 ~]# ls
nginx-1.17.1.tar.gz
#2.解压
[root@server1 ~]# tar zxf nginx-1.17.1.tar.gz
[root@server1 ~]# ls
nginx-1.17.1 nginx-1.17.1.tar.gz
2.隐藏版本号
隐藏版本号:是为了防止被黑客攻击
[root@server mnt]# cd nginx-1.17.1/
[root@server nginx-1.17.1]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README
[root@server nginx-1.17.1]# cd src/
[root@server src]# ls
core event http mail misc os stream
[root@server src]# cd core/
[root@server core]# ls
[root@server core]# vim nginx.h
3.关闭debug日志
关闭debug日志:因为日志量太多了,过于繁琐
[root@server core]# cd ..
[root@server src]# cd ..
[root@server nginx-1.17.1]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README
[root@server nginx-1.17.1]# cd auto/
[root@server auto]# ls
cc feature headers install module options stubs types
define have include lib modules os summary unix
endianness have_headers init make nohave sources threads
[root@server auto]# cd cc
[root@server cc]# ls
acc bcc ccc clang conf gcc icc msvc name owc sunc
[root@server cc]# vim gcc
4.源码编译
(1)configure编译
#没有eorror即可; --prefix:安装目录 threads:激活线程池 file-aio:文件io http_ssl_module:ssl模块 http_stub_status_module:状态监控模块
[root@server nginx-1.17.1]# ./configure --prefix=/usr/local/nginx --with-threads --with-file-aio --with-http_ssl_module --with-http_stub_status_module
(2)编译与安装
[root@server1 nginx-1.15.7]# make && make install
5.开启nginx
[root@server nginx-1.17.1]# cd /usr/local
[root@server local]# ls
bin etc games include lib lib64 libexec nginx sbin share src
[root@server local]# cd nginx/
[root@server nginx]# ls
client_body_temp fastcgi_temp logs(日志) sbin(启动脚本) uwsgi_temp
conf(配置文件) html(默认发布目录) proxy_temp scgi_temp
[root@server nginx]# cd sbin/
[root@server sbin]# ls
nginx
[root@server sbin]# ./nginx
[root@server sbin]# netstat -tnlp
6.网页测试
2.nginx的配置文件部分参数详解
#1.更改配置文件:添加nginx用户和组
[root@server sbin]# cd /usr/local/nginx/conf
[root@server conf]# ls
fastcgi.conf koi-utf nginx.conf uwsgi_params
fastcgi.conf.default koi-win nginx.conf.default uwsgi_params.default
fastcgi_params mime.types scgi_params win-utf
fastcgi_params.default mime.types.default scgi_params.default
[root@server conf]# vim nginx.conf
#2.创建nginx用户
#-s指定shell,-M不创建加目录,-d指定
[root@server1 conf]# useradd -s /sbin/nologin -M -d /usr/local/nginx nginx
[root@server1 conf]# id nginx
uid=1002(nginx) gid=1002(nginx) groups=1002(nginx)
#3.重新加载
[root@server nginx]# cd sbin/
[root@server sbin]# ls
nginx
[root@server sbin]# pwd
/usr/local/nginx/sbin (必须在该目录下重新加载)
[root@server sbin]# ./nginx -s reload
测试:
#查看进程:发现nginx进程的用户身份为nginx
[root@server sbin]# ps aux
3.nginx反向代理实现负载均衡
(1)配置nginx服务端
#1.更改配置文件
[root@server1 sbin]# cd /usr/local/nginx/conf
[root@server1 conf]# vim nginx.conf