一,介绍
ginx(发音同 engine x)是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发,最初供俄国大型的入口网站及搜寻引擎Rambler(俄文:Рамблер)使用。 其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页伺服器中表现较好.目前中国大陆使用nginx网站用户有:新浪、网易、 腾讯,另外知名的微网志Plurk也使用nginx。
二,优点
(1)Nginx 可以在大多数 Unix like OS 上编译运行,并有 Windows 移植版。 Nginx 的1.2.6稳定版已经于2012年12月11日发布,[1]1.3.10开发版已经于2012年12月25日发布,如果新建站点,建议使用最新稳定版作为生产版本,已有站点升级急迫性不高。Nginx 的源代码使用 2-clause BSD-like license。
(2)Nginx 是一个很强大的高性能Web和反向代理服务器,它具有很多非常优越的特性:
在高连接并发的情况下,Nginx是Apache服务器不错的替代品:Nginx在美国是做虚拟主机生意的老板们经常选择的软件平台之一。能够支持高达 50,000 个并发连接数的响应,感谢Nginx为我们选择了 epoll and kqueue作为开发模型。
(3)Nginx作为负载均衡服务器:Nginx 既可以在内部直接支持 Rails 和 PHP 程序对外进行服务,也可以支持作为 HTTP代理服务器对外进行服务。Nginx采用C进行编写,不论是系统资源开销还是CPU使用效率都比 Perlbal 要好很多。
作为邮件代理服务器:Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last. fm 描述了成功并且美妙的使用经验。
(4)Nginx 是一个安装非常的简单,配置文件非常简洁(还能够支持perl语法),Bugs非常少的服务器:Nginx 启动特别容易,并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动。你还能够不间断服务的情况下进行软件版本的升级。
3,安装
Nginx安装
下载依赖包
yum -y install gcc gcc-c++ pcre-devel zlib-devel make
rz 添加安装包
解压 tar zxf nginx-1.12.2.tar.gz -C /opt
编译安装
cd /opt/nginx-1.12.2/ #切换
./configure --prefix=/usr/local/nginx \ #指定安装路径
--user=nginx \ #指定用户
--group=nginx \ #指定组
--with-http_stub_status_module #开启状态统计模块,可以通过网页查看服务状态
make && make install #编译并安装
vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
Type=forking
PIDFile =/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecrReload=/bin/kill -s HUP $MAINPID
ExecrStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
vim /etc/init.d/nginx #在etc/init.d/nginx 写入脚本
# chkconfig: - 99 20
# description: Nginx Service Control Script
COM="/usr/local/nginx/sbin/nginx"
PID="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$COM
;;
stop)
kill -s QUIT $(cat $PID)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PID)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
chmod +x /etc/init.d/nginx //给权限
chkconfig --add nginx
vim /etc/hosts
27.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.75.10 www.zhangsan.com
访问状态统计
vim /usr/local/nginx/conf/nginx.conf //在里面编译
location / {
root html;
index index.html index.htm;
}
在下面添加
location /status {
stub_status on;
access_log off;
}
重启
systemctl restart nginx
访问控制
yum -y install httpd-tools
#生成用户密码认证文件
htpasswd -c /usr/local/nginx/passwd.db zhangsan
New password:
Re-type new password:
Adding password for user zhangsan
chown nginx /usr/local/nginx/passwd.db # 修改主配置文件对相应目录,添加认证配置项
chmod 400 /usr/local/nginx/passwd.db
vim /usr/local/nginx/conf/nginx.conf
location / {
auth_basic "secret";
auth_basic_user_file /usr/local/nginx/passwd.db;
root html;
index index.html index.htm;
}
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
systemctl restart nginx
基于域名的虚拟主机
vim /etc/hosts
192.168.75.10 www.acpp.com www.bcpp.com
mkdir -p /var/www/html/acpp
mkdir -p /var/www/html/bcpp
echo "<h1> www.acpp.com<h1>" >/var/www/html/acpp/index.html
echo "<h1> www.bcpp.com<h1>" >/var/www/html/bcpp/index.html
[root@www nginx]# cat /var/www/html/acpp/index.html //查看有无在路径下
<h1> www.acpp.com<h1>
[root@www nginx]# cat /var/www/html/bcpp/index.html
<h1> www.bcpp.com<h1>
vim /usr/local/nginx/conf/nginx.conf
server {
36 listen 80;
37 server_name www.acpp.com;
38
39 charset utf-8;
40
41 access_log logs/acpp.access.log;
42
43 location / {
44 root /var/www/html/acpp/;
45 index index.html index.htm;
46 }
47
48 error_page 500 502 503 504 /50x.html;
49 location = /50x.html {
50 root html;
51 }
52 }
53 server {
54 listen 80;
55 server_name www.bcpp.com;
56 charset utf-8;
57 access_log logs/bcpp.access.log;
58 location / {
59 root /var/www/html/bcpp/;
60 index index.html index.htm;
61 }
62 error_page 500 502 503 504 /50x.html;
63 location = /50x.html {
64 root html;
65 }
66 }