01 Nginx部署安装
一、先决条件
1. 通过下载阿里云YUM源(centos、epel)
https://opsx.alibaba.com/mirror
2. 安装依赖包(pcre、openssl)
yum install pcre pcre-devel openssl openssl-devel -y
二、部署安装
1. 通过nginx官方网站下载稳定版本
wget http://nginx.org/download/nginx-1.14.2.tar.gz
2. 源码安装
tar zxvf nginx-1.14.2.tar.gz cd nginx-1.14.2/ useradd nginx -s /sbin/nologin -M ./configure --user=nginx --group=nginx --prefix=/application/nginx-1.14.2/ --with-http_stub_status_module --with-http_ssl_module make make install ln -s /application/nginx-1.14.2 /application/nginx
3. 启动并检查安装结果
检查语法 /application/nginx/sbin/nginx -t 启动 pkill /application/nginx/sbin/nginx 重新加载 /application/nginx/sbin/nginx -s reload 检查状态 netstat -antulp|grep 80 lsof -i :80 curl 127.0.0.1
4. 查看编译时参数
/application/nginx/sbin/nginx -V
5. 查看配置文件
cd /application/nginx/conf/ egrep -v "#|^$" nginx.conf
三、虚拟主机
在web服务里就是一个独立的网站站点,这个站点对应独立的域名(IP或端口),具有独立的程序及资源目录,可以独立地对外提供用户访问。
虚拟主机类型:
1. 基于域名
cd /application/nginx/conf diff nginx.conf nginx.conf.default egrep -v "#|^$" nginx.conf.default > nginx.conf
- 单域名
mkdir -p /application/nginx/html/www echo "http://www.cool4.com" > /application/nginx/html/www/index.html cat /application/nginx/html/www/index.html http://www.cool4.com echo "192.168.176.88 www.cool4.com" >> /etc/hosts egrep -v "#|^$" nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.cool4.com; location / { root html/www; index index.html index.htm; } } } /application/nginx/sbin/nginx -s reload curl www.cool4.com http://www.cool4.com
- 多域名
cd /application/nginx/html/ mkdir bbs blog echo "192.168.176.88 bbs.cool4.com" >> /etc/hosts echo "192.168.176.88 blog.cool4.com" >> /etc/hosts echo "http://bbs.cool4.com" >> bbs/index.html echo "http://blog.cool4.com" >> blog/index.html egrep -v "#|^$" nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.cool4.com; location / { root html/www; index index.html index.htm; } } server { listen 80; server_name bbs.cool4.com; location / { root html/bbs; index index.html index.htm; } } server { listen 80; server_name blog.cool4.com; location / { root html/blog; index index.html index.htm; } } } /application/nginx/sbin/nginx -t /application/nginx/sbin/nginx -s reload curl www.cool4.com curl bbs.cool4.com curl blog.cool4.com
2. 基于端口
cd /application/nginx/conf diff nginx.conf nginx.conf.default egrep -v "#|^$" nginx.conf.default > nginx.conf
- 多端口(80、81、82)
egrep -v "#|^$" nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.cool4.com; location / { root html/www; index index.html index.htm; } } server { listen 81; server_name bbs.cool4.com; location / { root html/bbs; index index.html index.htm; } } server { listen 82; server_name blog.cool4.com; location / { root html/blog; index index.html index.htm; } } } /application/nginx/sbin/nginx -t /application/nginx/sbin/nginx -s reload curl www.cool4.com curl bbs.cool4.com:81 curl blog.cool4.com:82
3. 基于IP
一般企业不会用到,如需测试将listen、server_name对应更改即可
四、规范nginx配置文件
应用基于域名配置文件
1. 生成单独配置文件
cd /application/nginx/conf mkdir vhosts sed -n '10,17p' nginx.conf > vhosts/www.conf sed -n '18,25p' nginx.conf > vhosts/bbs.conf sed -n '26,33p' nginx.conf > vhosts/blog.conf sed -i '10,33d' nginx.conf sed -i '7 i include vhosts/*.conf;' nginx.conf cat nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; include vhosts/*.conf; default_type application/octet-stream; sendfile on; keepalive_timeout 65; } cat vhosts/www.conf server { listen 80; server_name www.cool4.com; location / { root html/www; index index.html index.htm; } } /application/nginx/sbin/nginx -t /application/nginx/sbin/nginx -s reload curl www.cool4.com curl bbs.cool4.com curl blog.cool4.com
五、状态信息查看
1. 增加配置文件
cat >>/application/nginx/conf/vhosts/status.conf<<EOF ##status server{ listen 80; server_name status.cool4.com; location / { stub_status on; access_log off; allow 192.168.176.0/24 deny all; } } EOF echo "192.168.176.88 status.cool4.com" >> /etc/hosts /application/nginx/sbin/nginx -t /application/nginx/sbin/nginx -s reload curl status.cool4.com Active connections: 1 server accepts handled requests 16 16 18 Reading: 0 Writing: 1 Waiting: 0
| Active connections: | 表示nginx正在处理的数动连接数 |
| server: | 表示nginx启动到现在共处理了16个连接 |
| accepts: | 表示nginx启动到现在共成功创建了16次握手 |
| 请求丢件数=(握手数-连接数) | |
| handled requests: | 表示总共处理了18次请求 |
| Reading: | 为nginx读取到客户端的Header信息数 |
| Writing: | 为nginx返回给客户端的Header信息数 |
| Waiting: | 为nginx已经处理完正在等候下一次请求指令的驻留连接。 |
| 在开启keepalive的情况下,这个值等于active-(reading+writing) |
随心、随意、随缘、大自在天。

浙公网安备 33010602011771号