项目目录
关于Nginx
- 一款高性能、轻量级Web服务软件
- 稳定性高
- 系统资源消耗低
- 对HTTP并发连接的处理能力高
- 单台物理服务器可支持30 000~50 000个并发请求
Nginx服务基础
nginx服务器IP地址:20.0.0.10
1、导入数据包
[root@nginx opt]# ll
总用量 960
-rw-r–r-- 1 root root 981687 10月 16 08:14 nginx-1.12.2.tar.gz
tar zxvf nginx-1.12.2.tar.gz
2、安装编译工具
[root@nginx opt]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel
3、创建一个不可登陆的程序用户
[root@nginx opt]# useradd -M -s /sbin/nologin nginx
4、安装功能模块
[root@nginx opt]# cd nginx-1.12.2/
./configure --prefix=/usr/local/nginx \
--user=nginx\
--group=nginx\
--with-http_stub_status_module
5、编译安装
[root@nginx opt]# cd nginx-1.12.2/
[root@nginx nginx-1.12.2]# make && make install
6、优化路径
[root@nginx nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@nginx nginx-1.12.2]# ln -s /usr/local/nginx/conf/nginx.conf /etc/nginx.conf
7、设置nginx启动进程
[root@nginx nginx-1.12.2]# vim /etc/init.d/nginx
#!/bin/bash
#chkconfig: 35 99 20
#nginx
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage:$0 {start|stop|restart|reload}"
exit 1
esac
exit 0
8、设置权限
[root@nginx nginx-1.12.2]# chmod +x /etc/init.d/nginx
[root@nginx nginx-1.12.2]# chkconfig --add nginx
9、修改配置文件
[root@nginx nginx-1.12.2]# vim /etc/nginx.conf
user nobody; 去掉#,指定用户,默认是匿名用户
10、启动nginx
[root@nginx nginx-1.12.2]# systemctl start nginx
[root@nginx nginx-1.12.2]# systemctl enable nginx
11、查看nginx是否启动
[root@nginx nginx-1.12.2]# netstat -anpt|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4691/nginx: master
12、测试
Nginx配置文件参数详情
#user nobody; ##指定用户,默认是匿名用户
worker_processes 1; ##工作进程,实现高并发可以增加值
#error_log logs/error.log; ##错误日志文件
#pid logs/nginx.pid; ##pid文件
进程数配置
events { ##一个进程包含多个线程
use epoll; #能显著提高程序在大量并发连接中只有少量活跃的情况下的系统CPU利用率
worker_connections 4096; #连接数4096基于线程数
}
http配置
http{ }:协议层面;server{ }:服务层面;location{ }:网页站点目录层面
http {
access_log logs/access.log main;
sendfile on; ##发送邮件
keepalive timeout 65; ##连接超时时间
server {
listen 80;
server_name localhost; ##域名
charset utf-8; ##字符集
location / {
root html; ##网页站点目录名称
index index.html index.php; } ##主页的相对路径
error_page 500 502 503 504 /50x.html; ##提示错误页面(500是服务器出错)
location = /50x.html {
root html; }
}
}
Nginx的访问状态统计
[root@nginx ~]# vim /etc/nginx.conf
#user nobody #修为 user nginx nginx
#error_log logs/error.log info; 去掉#
events {
use epoll; #增加
worker_connections 1024;
}
在#charset koi8-r;下面增加
location ~ /status { ###配置统计功能
stub_status on;
access_log off;
} ###在server模块里的error_page上面增加
[root@nginx ~]# systemctl restart nginx.service
Nginx访问控制
1、安装httpd-tools
[root@nginx ~]# yum -y install httpd-tools.x86_64
2、创建访问用户
[root@nginx ~]# htpasswd -c /usr/local/nginx/passwd.db abc
New password:
Re-type new password:
Adding password for user abc
3、设置密码验证
[root@nginx ~]# vim /etc/nginx.conf
location / {
root html;
index index.html index.htm;
deny all; ###拒绝所有
auth_basic "secret"; ###验证方式为密码验证
auth_basic_user_file /usr/local/nginx/passwd.db; ###密码所在文件
}
4、检查语法
[root@nginx ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
5、重启服务
[root@nginx ~]# systemctl restart nginx.service
6、测试
Nginx虚拟主机
1、虚拟主机基于域名访问
[root@nginx ~]# vim /etc/nginx.conf
server {
listen 80;
server_name www.abc.com;
charset utf-8
location / {
root /var/www/abc;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.edf.com;
charset utf-8;
location / {
root /var/www/edf;
index index.html index.htm;
}
}
设置测试主页
[root@nginx ~]# mkdir -p /var/www/abc
[root@nginx ~]# mkdir -p /var/www/edf
[root@nginx ~]# echo "<h1>This is nginx 1</h1>" >/var/www/abc/index.html
[root@nginx ~]# echo "<h1>This is nginx 2</h1>" >/var/www/edf/index.html
重启服务
[root@nginx ~]# systemctl restart nginx.service
测试
测试机需要进行dns设置
[root@ciesi ~]# vim /etc/hosts
20.0.0.10 www.abc.com www.edf.com #增加
2、虚拟主机基于端口访问
[root@nginx ~]# vim /etc/nginx.conf
server {
listen 20.0.0.1080;
server_name www.abc.com;
charset utf-8
location / {
root /var/www/abc;
index index.html index.htm;
}
}
server {
listen 20.0.0.10:8080;
server_name www.abc.com;
charset utf-8;
location / {
root /var/www/edf;
index index.html index.htm;
}
}
设置测试主页
[root@nginx ~]# mkdir -p /var/www/abc
[root@nginx ~]# mkdir -p /var/www/edf
[root@nginx ~]# echo "<h1>This is nginx 1</h1>" >/var/www/abc/index.html
[root@nginx ~]# echo "<h1>This is nginx 2</h1>" >/var/www/edf/index.html
重启服务
[root@nginx ~]# systemctl restart nginx.service
测试
3、虚拟主机基于IP地址访问
[root@nginx ~]# vim /etc/nginx.conf
server {
listen 20.0.0.10:80;
server_name www.abc.com;
charset utf-8
location / {
root /var/www/abc;
index index.html index.htm;
}
}
server {
listen 20.0.0.100:80;
server_name www.edf.com;
charset utf-8;
location / {
root /var/www/edf;
index index.html index.htm;
}
}
设置测试主页
[root@nginx ~]# mkdir -p /var/www/abc
[root@nginx ~]# mkdir -p /var/www/edf
[root@nginx ~]# echo "<h1>This is nginx 1</h1>" >/var/www/abc/index.html
[root@nginx ~]# echo "<h1>This is nginx 2</h1>" >/var/www/edf/index.html
重启服务
[root@nginx ~]# systemctl restart nginx.service
测试
测试机需要进行dns设置
[root@ciesi ~]# vim /etc/hosts
20.0.0.10 www.abc.com #增加
20.0.0.100 www.edf.com #增加