Nginx是一款高性能、轻量级的web服务软件
优点:
高性能:对http并发连接的处理能很高,单台物理服务器可支持30000-50000个并发请求(在实际操作中,为了维持服务器的稳定,一般设置在20000个左右)
轻量级:nginx软件很小,安装所需的空间也很小
稳定性强:对系统资源消耗低
1、处理静态网页:html、htm图像
2、支持反向代理(负载均衡),负载均衡靠算法实现
3、处理动态内容:但是处理能力较差,有专门处理动态内容的程序,例如comcat或srpingclound
4、虚拟主机:nginx可以配置多个虚拟主机,每个虚拟主机都可以作为一个域名和站点,每个虚拟主机都可以拥有独立的配置和资源
5、URL重定向:可以对URL的请求进行修改和重定向
6、缓存机制,可以缓存静态文件和动态内容
7、日志记录:服务日志,访问日志和报错日志:/usr/local/nginx/logs(控制日志还是在/var/log/messgaes)中
8、可以做为代理服务器,通过代理可以访问其他的后端服务器(淘宝使用的就是基于nginx二次开发的Tengine天极)
- conf:保存nginx的所有配置文件,其中nginx.conf是nginx的主配置文件
- html:保存nginx的web文件,html结尾的文件,图片
- 50x:nginx默认的报错提示页面
- logs:保存日志的目录,路径可以改
- access.log:记录访问日志记录
- error.log:记录报错日志
- sbin:nginx的二进制启动脚本
- -t:检测nginx配置文件的配置语法是否正确
- -v:只查看版本
- -V:查看版本和bginx支持的配置模块
- -s:给nginx主程序发送信号
- -s stop:停止nginx服务器,即停止监听连接的处理
- -s restart:重启nginx服务器,停止并重新启动监听连接的处理
- -s reload:重新加载nginx配置文件,实现平滑地重新加载配置文件,对新的配置进行生效,而无需停止正在处理的连接
主模块:
包含一些全局的指令,如工作进程数、日志文件路径等
events模块:
控制并发数
httpd模块:
配置代理、缓存、日志、虚拟主机和第三方模块
server模块:
配置虚拟主机的设备,在http的模块中可以有多个server
location模块:
location模块只能配置在server模块当中,匹配uri
(location模块中root指定和alias指定的区别:root是拼接,alias是完整路径匹配)
注:一个http中可以有多个server,一个server可以有多个location
proxy模块
代理功能,核心功能模块之一,配置反向的功能
proxy_pass:指定,定义在location当中
headers模块
处理请求头部的响应信息,获取客户端真实ip
upstream模块
七层反向代理模块,只能配置在http模块当中,或者stream模块中
stream模块
四层反向代理模块,只能写在配置当中
关闭防火墙及安全机制
- systemctl stop firewalld
- setenforce 0
安装依赖环境
yum -y install gcc pcre-devel openssl-devel zlib-devel openssl openssl-devel
创建运行用户,指定nginx为程序用户不能登录
useradd -M -s /sbin/nologin nginx
编译安装
- cd /opt #然后将nginx压缩包传入系统
- tar -xf nginx-1.22.o.tar.gz
- cd nginx-1.22.0/
- ./configure --prefix=/usr/local/nginx \
- --user=nginx \
- --group=nginx \
- --with-http_ssl_module \
- --with-http_v2_module \
- --with-http_realip_module \
- --with-http_stub_status_module \
- --with-http_gzip_static_module \
- --with-pcre \
- --with-stream \
- --with-stream_ssl_module \
- --with-stream_realip_module
-
- make -j 4 && make install
-
- 注释:
- ./configure --prefix=/usr/local/nginx \ #指定安装目录
- --user=nginx \ #指定运行用户
- --group=nginx \ #指定运行组
- --with-http_ssl_module \ #开启对http的ssl加密支持
- --with-http_v2_module \ #支持http2.0协议
- --with-http_realip_module \ #允许nginx获取客户端的真实ip
- --with-http_stub_status_module \ #启动了stud_status模块,获取nginx的访问和状态信息的方法
- --with-http_gzip_static_module \ #支持压缩文件内容图片
- --with-pcre \ #动态库
- --with-stream \ #开启tcp/udp代理模块支持 支持四层转发
- --with-stream_ssl_module \ #支持四层转发的ssl加密
- --with-stream_realip_module #nginx可以从四层转发的头部信息中获取客户端的真实ip
修改目录权限
将/usr/local/nginx目录下的所有文件和子目录的所有者和所属组改为 nginx
chown -R nginx.nginx /usr/local/nginx/
创建软连接,让系统能够识别二进制
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
添加 Nginx 系统服务
- vim /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=/usr/local/nginx/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
指定存储Nginx进程ID的文件路径
- cd /usr/local/nginx/
- mkdir run
- chown -R nginx.nginx /usr/local/nginx
- cd conf
- vim nginx.conf
-
- pid /usr/local/nginx/run/nginx.pid;
- #把pid这一行的注释删掉,把路径改为设置好的路径
重新加载配置并重启nginx服务
- nginx -t
- systemctl daemon-reload
- systemctl restart nginx
- [root@localhost conf]# vim nginx.conf
-
- location /status {
- stub_status on;
- access_log off;
- }
-
- [root@localhost conf]# nginx -t
- [root@localhost conf]# systemctl restart nginx.service
使用浏览器访问20.0.0.61/status
Active connections:表示当前活动连接数
server accepts:已经处理的连接数
handled:成功的tcp握手次数
requests:已经处理的请求数
Reading:0:服务端正在从客户端读取数据,正在读取客户端请求的连接数
Writing:1:服务器正在将响应数据发送给客户端,正在向客户端写入响应的连接数
Wating:1:表示有连接处于空闲状态,当前等待客户端请求的连接数,即time_wait
[root@pup1 conf]# vim nginx.conf
添加新的server模块
- [root@localhost conf]# cd /var/www/html
- [root@localhost html]# mkdir pup accp
- [root@localhost html]# cd pup
- [root@localhost pup]# vim index.html
- this is pup!
-
- [root@localhost pup]# cd ..
- [root@localhost html]# cd accp
- [root@localhost accp]# vim index.html
- this is accp!
-
- [root@localhost accp]# vim /etc/hosts
- 20.0.0.61 www.pup.com www.accp.com
-
- [root@localhost conf]# nginx -t
- [root@localhost conf]# systemctl restart nginx.service
浏览器访问www.pup.com
浏览器访问www.accp.com
- [root@localhost conf]# ifconfig ens33:0 20.0.0.100/24
- [root@pup1 conf]# vim nginx.conf
- [root@localhost conf]# nginx -t
- [root@localhost conf]# systemctl restart nginx.service
浏览器访问20.0.0.61
浏览器访问20.0.0.100
- [root@localhost accp]# cd /usr/local/nginx/conf/
- [root@localhost conf]# vim nginx.conf
- [root@localhost conf]# nginx -t
- [root@localhost conf]# systemctl restart nginx
浏览器访问20.0.0.61:8080
浏览器访问20.0.0.100:8888
- [root@localhost ~}# yum -y install httpd-tools
- [root@localhost ~]# chown nginx /usr/local/nginx/passwd.db
- [root@localhost ~]# chmod 400 /usr/local/nginx/passwd.db
- [root@localhost ~]# cd /usr/local/nginx/conf/
- [root@localhost conf]# vim nginx.conf
- [root@localhost conf]# nginx -t
- [root@localhost conf]# systemctl restart nginx
浏览器访问20.0.0.61:8080
[root@localhost conf]# vim nginx.conf
- [root@localhost conf]# nginx -t
- [root@localhost conf]# systemctl restart nginx
20.0.0.10主机无法访问
20.0.0.20主机可以访问