Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。
其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
1.1:Nginx概述
一款高性能、轻量级Web服务软件
●稳定性高
●系统资源消耗低
●对HTTP并发连接的处理能力高
●单台物理服务器可支持30 000 ~ 50000个并发请求
●占用内存少,并发能力强
Nginx支持的虚拟主机有三种
●基于域名的虚拟主机
●基于IP的虚拟主机
●基于端口的虚拟主机
通过"server{}" 配置段实现
[root@sha opt]# yum -y install gcc \
> gcc-c++ \
> make \
> pcre-devel \
> expat-devel \
> perl \
> zlib-devel \
> pcre
解压nginx 软件
[root@sha opt]# tar zxvf nginx-1.12.2.tar.gz
创建程序账户(不创建加目录 不允许登录)
[root@shanan nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
[root@shanan nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \ //设置用户
> --group=nginx \ //设置组
> --with-http_stub_status_module //安装统计模块
安装nginx
[root@sha nginx-1.12.2]# make && make install
[root@sha conf]# vim /etc/init.d/nginx
#!/bin/bash
#chkconfig:- 99 20
#description: Nginx Service Control Script
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}"
esac
exit0
测试服务管理
[root@sha logs]# service nginx start
[root@sha logs]# service nginx stop
[root@sha logs]# service nginx start
[root@sha logs]# service nginx restart
使用elinks启动、重载配置、停止Nginx
[root@sha /]]# nginx '//启动'
[root@sha /]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7180/nginx: master
[root@sha /]# yum -y install elinks
[root@sha /]# elinks http://localhost '//显示"Welcome to nginx!"页面,表明Nginx服务已经正常运行'
[root@sha /]# killall -s HUP nginx '//-S选项指定信号种类,HUP信号表示重载配置'
[root@sha /]# killall -s QUIT nginx '//QUIT信号表示退出进程'
yum install -y bind
[root@sha conf]# vim /etc/named.conf
[root@sha conf]# vim /etc/named.rfc1912.zones
zone "dog.com" IN {
type master;
file "dog.com.zone";
allow-update { none; };
};
zone "pig.com" IN {
type master;
file "pig.com.zone";
allow-update { none; };
};
root@sha conf]# vim /etc/named.conf
[root@sha conf]# vim /etc/named.rfc1912.zones
[root@sha named]# cp -p named.localhost dog.com.zone
[root@sha named]# vim d
data/ dog.com.zone dynamic/
[root@sha named]# vim dog.com.zone
[root@sha named]# cp -p dog.com.zone pig.com.zone
检查配置文件
[root@localhost ~]# nginx -t '//检查'
优化配置文件设置
[root@sha logs]# cd /usr/local/nginx/conf
[root@sha conf]# mv nginx.conf nginx.conf.back
[root@sha conf]# grep -v "#" nginx.conf.back > nginx.conf
[root@sha logs]# cd /usr/local/nginx/conf
[root@sha conf]# mv nginx.conf nginx.conf.back
[root@sha conf]# grep -v "#" nginx.conf.back > nginx.conf
Vim /usr/local/nginx/conf/nginx.conf
server {
server_name www.dog.com;
location / {
root /var/www/dog;
index index.html index.php;
}
}
server {
server_name www.pig.com;
location / {
root /var/www/pig;
index index.html index.php;
}
}
Vim /usr/local/nginx/conf/nginx.conf
vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.100.50:80;
server_name www.pig.com;
location / {
root /var/www/pig;
index index.html index.php;
}
}
server {
listen 192.168.100.50:8080;
server_name www.pig.com;
charset utf-8;
access_log logs/www.pig8080.com.access.log;
location / {
root /var/www/pig8080;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[root@sha pig8080]# vim /var/named/dog.com.zone
---完整的配置文件
vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 192.168.100.131:80;
server_name www.dog.com;
location / {
root /var/www/dog;
index index.html index.php;
}
}
server {
listen 192.168.100.50:80;
server_name www.pig.com;
location / {
root /var/www/pig;
index index.html index.php;
}
}
server {
listen 192.168.100.50:8080;
server_name www.pig.com;
charset utf-8;
access_log logs/www.pig8080.com.access.log;
location / {
root /var/www/pig8080;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
配置步骤与Apache基本一致
●生成用户密码认证文件
●修改主配置文件对相应目录,添加认证配置项
●重启服务,访问测试
[root@sha dog]# yum install -y httpd-tools //因为没有htpasswd工具,所以需要安装
创建用户sha 并设置密码asd123
[root@sha dog]# htpasswd -c /usr/local/nginx/passwd.db sha
New password:
Re-type new password:
Adding password for user sha
[root@sha dog]#
生成用户密码认证文件
[root@sha dog]# cat /usr/local/nginx/passwd.db
sha:$apr1$ddD6d/d2$5wtNjPjm9RKpu/b0T/qvL.
[root@sha dog]# cd /usr/local/nginx/
[root@sha nginx]# ls
client_body_temp fastcgi_temp logs proxy_temp scgi_temp
conf html passwd.db sbin uwsgi_temp
[root@sha nginx]# chmod 400 /usr/local/nginx/passwd.db
[root@sha nginx]# chown nginx /usr/local/nginx/passwd.db
[root@sha nginx]# ll -d /usr/local/nginx/passwd.db
-r--------. 1 nginx root 42 8月 8 13:24 /usr/local/nginx/passwd.db
[root@sha conf]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.100.131:80;
server_name www.dog.com;
auth_basic "secret";
auth_basic_user_file /usr/local/nginx/passwd.db;
location / {
root /var/www/dog;
index index.html index.php;
}
}
[root@sha conf]# service nginx restart
通过客户端IP地址,决定是否允许对页面访问
deny IP/IP段:拒绝某个IP或IP段的客户端访问
allow IP/IP段:允许某个IP或IP段的客户端访问
规则从上往下执行,如匹配则停止,不再往下匹配
修改主配置文件nginx.conf,添加相应配置项
vim /usr/local/nginx/conf/nginx.conf
[root@sha conf]# service nginx restart