码迷,mamicode.com
首页 > Web开发 > 详细

WEB服务器-Nginx之虚拟主机、日志、认证及优化

时间:2017-05-25 22:03:28      阅读:488      评论:0      收藏:0      [点我收藏+]

标签:nginx 虚拟主机   优化   认证 别名

WEB服务器-Nginx之虚拟主机、日志、认证及优化

概述

Nginx ("engine x") 是一个高性能的HTTP反向代理服务器,也是一个IMAP/POP3/SMTP服务器Nginx是由Igor Sysoev俄罗斯访问量第二的Rambler.ru站点开发的,第一个公开版本0.1.0发布于2004104日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名201161日,nginx 1.0.4发布。

Nginx是一款轻量级Web 服务器/反向代理服务器及电子邮件IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发,供俄国大型的入口网站及搜索引擎Rambler使用。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东新浪网易腾讯淘宝

Nginx的重要特性及应用场合

1Nginx重要特性

(1)可针对静态资源告诉高并发访问及缓存

(2)可使用反向代理加速,并且可进行数据缓存

(3)具有简单的负载均衡,节点讲课检查和容错功能

(4)支持原创fastcgi服务的缓存加速

(5)支持基于域名、端口和IP的多虚拟主机站点等

 

2Nginx主要企业功能应用

1)作为web服务软件

Nginx是个支持高性能、高并发的web软件,作为web服务器,Nginx能够支持更多的并发连接访问,并且占用的资源很少,效率更高。

(2)作为反向代理或者负载均衡器

在反向代理或负载均衡服务方面,Nginx可以作为web服务,phpjava的办法动态服务及memcache缓存的代理服务。

3)前端业务数据缓存服务

Nginx可以通过自身的proxy_cache模块实现类似squid等专业缓存软件功能

NginxWEB服务

1Nginx作为web服务器的主要应用场景包括:

(1)使用N信息运行HTMLJSCSS等静态数据

(2)Nginx结合fastcgi运行PHP等动态程序(使用fastcgi_pass方式)

(3)Nginx 结合TomcatResin等支持java动态程序(使用proxy_pass方式)

 

2Nginx总体性能比Apache

Nginx使用最新的epollkqueue异步网络IO模型,而Apache使用的是传统的select模型。而目前Linux下能够承受的高并发访问squidmemcache软件采用都是epoll模型。戴亮连接读写时,Apache采用的select网络IO模型比较低。

3Apache selectNginx epoll的技术对比

指标

select

epoll

性能

随着连接数的增加心梗几句下滑

随着连接数的增加,性能基本上没有改变

连接数

连接数有限制,处理的最大连接数不超过1024

连接数无限制

内在处理机制

线性轮询

回调callback

开发复杂性

 

Nginx技术的深入剖析

Nginx软件之所以强大,是因为他具有众多的通能模块。

1Nginx模块主要有4种角色: 
    (1) core(核心模块):构建nginx基础服务、管理其他模块。 
    (2) handlers(处理模块): 用于处理HTTP请求,然后产生输出。 
    (3) filters(过滤模块): 过滤handler产生的输出。 
    (4) load-balancers(负载均衡模块):当有多于一台的后端备选服务器时,选择一台转发HTTP请求。

Nginx发送文件或者转发请求到其他服务器,由handlers(处理模块)load-balancers(负载均衡模块)提供服务;

当需要Nginx把输出压缩或者在服务端加一些东西,由filters(过滤模块) 提供服务。

2Nginx的几个常用模块

1Nginx的核心功能模块 Nginx core module

Nginx可信功能模块负载全局应用,主要对饮主配置文件的main区块和events区块区域,这里有很多Nginx必须的全局参数配置。

2)标准的http功能模块集合

这些标准的http功能模块,虽然不是Nginx软件所必须的,但是都是很常用的,因此绝大部分默认都会安装到Nginx软件中

在生产环节中,配置,调优及优化Nginx软件,主要就是根据这些模块的功能修改相应的参数来实现的。

 

Nginx http功能模块

模块说明

ngx_http_core_moudle

包括一些核心的http参数配置,对应的nginx的配置为http区块

ngx_http_access_moudle

访问控制模块,用来控制网站用户对Nginx的访问

ngx_http_gzip_moudle

压缩模块,对Nginx返回的数据压缩,属于心梗优化模块

ngx_http_fastcgi_moudle

FastCGI模块,和动态相关的模块,如PHP/JAVA

ngx_http_proxy_moudle

Proxy代理模块

ngx_http_upstream_moudle

负载均衡模块,可以实现网站的负载均衡功能,和节点的健康检查

ngx_http_rewrite_moudle

URL地址重写模块

ngx_http_limit_conn_moudle

限制用户并发连接数及请求书模块

ngx_http_limit_req_moudle

根据定义的key限制Nginx请求过程速率

ngx_http_log_moudle

访问日志模块,以指定格式记录Nginx客户访问日志等信息

ngx_http_auth_basic_moudle

Web认证模块,设置web用户通过账户,密码访问Nginx

ngx_http_ssl_moudle

Ssk模块,用于加密的httpd连接,如https

ngx_http_stub_status_moudle

记录Nginx基本访问状态信息等模块

 

 

一、实验目标

1、实战:Nginx基于域名、端口和IP的多个虚拟主机

2、实战:Nginx虚拟主机的别名功能

3、实战:Nginx基于多个虚拟地址和端口的优化

4、实战:Nginx的状态信息功能实战

5、实战:Nginx的访问认证

6、实战:Nginx访问日志及轮询切割

二、实验环境

系统环境

主机名

IP地址

软件

Rhel6.5

yu63

192.168.1.63

Nginx

 

三、实验步骤

1、安装nginx

[root@yu63~]#service httpd stop

[root@yu63~]#service iptables stop

[root@yu63~]#yum install pcre pcre-devel openssl openssl-devel

[root@yu63~]#mkdir /opt/yu && cd /opt/yu/

[root@yu63 yu]#wget -q http://nginx.org/download/nginx-1.13.0.tar.gz

[root@yu63 yu]#tar -zxvf nginx-1.13.0.tar.gz

[root@yu63 yu]#cd nginx-1.13.0

[root@yu63 nginx-1.13.0]#useradd nginx -s /sbin/nologin

[root@yu63 nginx-1.13.0]#./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

[root@yu63nginx-1.13.0]#make -j 4

[root@yu63nginx-1.13.0]#make install

[root@yu63 nginx-1.13.0]# cd /usr/local/nginx/

[root@yu63 nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

[root@yu63 nginx]#/usr/local/nginx/sbin/nginx -t

[root@yu63 nginx]#/usr/local/nginx/sbin/nginx

 

Nginx虚拟主机配置实战

虚拟主机的概念

所谓的虚拟主机,在web服务里就是独立的网站站点,这个站点对应duld域名、IP、端口。具有独立的程序及资源目录,可以独立的对外提供给服务供用户访问。

对于Apache来说,虚拟主机的标签通常被包含在<VirtualHost>~~~</VirtualHost>中,而Nginx软件则是使用server{}标签来表示一个虚拟主机,一个web服务里可以有多个虚拟主机标签,即可以同时支持多个虚拟主机站点。

虚拟主机的类型

1)基于域名的虚拟主机

2)基于端口的虚拟主机

3)基于IP地址的虚拟主机

 

实战1:基于域名和端口的多个虚拟主机

[root@yu63 nginx]# cd conf/

[root@yu63 conf]#egrep -v ‘#|^$‘ nginx.conf.default > nginx.conf

[root@yu63nginx]# rm -rf html/index.html

[root@yu63nginx]# mkdir -p html/mobanche

[root@yu63nginx]# mkdir -p html/mobanche1

[root@yu63nginx]# mkdir -p html/mobanche2

[root@yu63nginx]#echo ‘mobanche‘> html/index.html

[root@yu63nginx]# echo ‘mobanche----1‘ > html/mobanche1/index.html

[root@yu63nginx]# echo ‘mobanche----2‘ > html/mobanche2/index.html

[root@yu63nginx]#vim conf/nginx.conf

[root@yu63nginx]# cat conf/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.mobanche.com;

        location / {

            root   html/mobanche;

            index  index.html index.htm;

        }

        }

    server {

        listen       81;

        server_name  bbs.mobanche1.com;

        location / {

            root   html/mobanche1;

            index  index.html index.htm;

        }

        }

    server {

        listen       82;

        server_name  blog.mobanche2.com;

        location / {

            root   html/mobanche2;

            index  index.html index.htm;

        }

    }

}

 

[root@yu63nginx]# /usr/local/nginx/sbin/nginx -t

[root@yu63nginx]#/usr/local/nginx/sbin/nginx -s reload

[root@yu63nginx]# cat /etc/hosts

192.168.1.63 www.mobanche.com bbs.mobanche1.com blog.mobanche2.com

Windos下面的hosts文件

技术分享

技术分享

技术分享

技术分享

 

 

实战2Nginx虚拟主机的别名功能

[root@yu63nginx]# vim conf/nginx.conf

[root@yu63nginx]# cat conf/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.mobanche.com mobanche.com;

        location / {

            root   html/mobanche;

            index  index.html index.htm;

        }

        }

    server {

        listen       81;

        server_name  bbs.mobanche1.com mobanche1.com;

        location / {

            root   html/mobanche1;

            index  index.html index.htm;

        }

        }

    server {

        listen       82;

        server_name  blog.mobanche2.com mobanche2.com;

        location / {

            root   html/mobanche2;

            index  index.html index.htm;

        }

    }

}

 

技术分享

技术分享

技术分享

 

实战3Nginx基于多个域名、ip和端口的优化

[root@yu63 conf]#mkdir extra

      173  sed -n ‘ 11,18p ‘ nginx.conf > extra/mobanche.conf

  176  sed -n ‘ 19,26p ‘ nginx.conf > extra/mobanche1.conf

  177  sed -n ‘ 27,34p ‘ nginx.conf > extra/mobanche2.conf

[root@yu63nginx]#vim nginx.conf #删除11-34

 sed -i ‘10 i include extra/mobanche.conf;\ninclude extra/mobanche1.conf;\ninclude extra/mobanche2.conf;‘ nginx.conf

[root@yu63 nginx]# cat conf/extra/mobanche.conf

   server {

        listen       80;

        server_name  www.mobanche.com;

        location / {

            root   html/mobanche;

            index  index.html index.htm;

        }

        }

[root@yu63nginx]#/usr/local/nginx/sbin/nginx -t

[root@yu63nginx]#/usr/local/nginx/sbin/nginx

实战4Nginx的状态信息功能实战

Nginx软件功能模块中有一个ngx_http_stub_status_moudle模块,这个模块的主要功能就是记录Nginx的节本访问状态信息,让使用者了解Nginx的工作状态,例如连接数等信息。

 

[root@yu63 nginx]#cd extra

[root@yu63 extra]#

cat >> status.conf << EOFEOF

>># status

>>server {

>>        listen       80;

>>        server_name  status.mobanche.com;

>>        location / {

>>stub_status on;

>>access_log off;

>>        }

>>        }

 

[root@yu63 extra]#sed -i ‘ 13 i include extra/status.conf; ‘ ../nginx.conf

[root@yu63 extra]# ../../sbin/nginx -t

[root@yu63 extra]#. ../../sbin/nginx -s reload

技术分享 

实战5Nginx 的访问认证

[root@yu63extra]# pwd

/usr/local/nginx/conf/extra

[root@yu63extra]#vim mobanche.conf

   server {

        listen       80;

        server_name  www.mobanche.com mobanche.com;

        location / {

            root   html/mobanche;

            index  index.html index.htm;

        auth_basic "welcome please enter user and passwd";

        auth_basic_user_file /usr/local/nginx/conf/htpasswd;

        }

        }

[root@yu63conf]# htpasswd -bc /usr/local/nginx/conf/htpasswd yu 123456

Adding password for user yu

[root@yu63conf]# chmod 400 htpasswd

[root@yu63conf]# chown nginx htpasswd

[root@yu63conf]# cat htpasswd

yu:c5XqWgCfsgKoo 

[root@yu63extra]# ../../sbin/nginx -t

[root@yu63extra]# ../../sbin/nginx -s reload

技术分享 

 

 

实战6:访问日志及轮询切割

Nginx软件会把每个用户的访问网站的日志信息记录到指定的入职文件中,供网站提供者分析用户的浏览行为。

1、控制日志的参数

参数

说明

log_format

用来定义记录日志的格式(可以定义多种日志格式)

access_log

用来指定日志文件的路径及使用何种日志格式记录日志

2Nginx的访问日志主要默认参数

log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘

         ‘$status $body_bytes_sent "$http_referer" ‘

         ‘"$http_user_agent" "$http_x_forwarded_for"‘;

3、日志变量说明

 

日志变量

说明

$remote_addr

用以记录客户端的ip地址

$remote_user

用来记录客户端用户名称

$time_local

用来记录访问时间与时区

$request

用来记录请求的urlhttp协议

$status

用来记录请求状态;成功是200

$body_bytes_sent

服务器发送给客户端响应的body字节数

$http_referer

用来记录从那个页面链接访问过来的

$http_user_agent

记录客户端浏览器的相关信息

$http_x_forwarded_for

当前端有代理服务器时没设置web节点记录客户端地址的配置,此参数生效的前提是代理服务器上也进行了x_forwarded_for设置

 

 

[root@yu63nginx]# cat conf/nginx.conf

[root@yu63 nginx]# cat conf/nginx.conf

worker_processes  1;

error_log  logs/error.log;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘

         ‘$status $body_bytes_sent "$http_referer" ‘

         ‘"$http_user_agent" "$http_x_forwarded_for"‘;

    sendfile        on;

    keepalive_timeout  65;

include extra/mobanche.conf;

include extra/mobanche1.conf;

include extra/mobanche2.conf;

    }

[root@yu63 nginx]# cat conf/extra/mobanche.conf

   server {

        listen       80;

        server_name  www.mobanche.com;

        location / {

            root   html/mobanche;

            index  index.html index.htm;

        }

 access_log  logs/access_mobanche.log  main;

        }

[root@yu63 nginx]# cat conf/extra/mobanche1.conf

   server {

        listen       81;

        server_name  www.mobanche1.com;

        location / {

            root   html/mobanche;

            index  index.html index.htm;

        }

access_log  logs/access_mobanche1.log  main;

        }

[root@yu63 nginx]# cat conf/extra/mobanche2.conf

   server {

        listen       82;

        server_name  www.mobanche2.com;

        location / {

            root   html/mobanche;

            index  index.html index.htm;

        }

access_log  logs/access_mobanche2.log  main;

        }

[root@yu63 nginx]# tail -1 logs/access_mobanche.log

192.168.1.63 - - [15/May/2017:22:25:42 +0800] "GET / HTTP/1.1" 200 9 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.3.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "-"

[root@yu63extra]# ../../sbin/nginx -t

[root@yu63extra]# ../../sbin/nginx -s reload

[root@yu63 nginx]# tail -3 logs/access_mobanche.log

192.168.1.105 - - [15/May/2017:22:26:51 +0800] "GET /favicon.ico HTTP/1.1" 404 571 "http://www.mobanche.com/" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.2669.400 QQBrowser/9.6.10990.400" "-"

192.168.1.105 - - [15/May/2017:22:26:51 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.2669.400 QQBrowser/9.6.10990.400" "-"

192.168.1.105 - - [15/May/2017:22:26:51 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.2669.400 QQBrowser/9.6.10990.400" "-"

注释:在没有开启访问实战时,查看日志中的内容,只是本机的IP地址,而配置之后,就可以看到客户端的主机IP

3Nginx 访问日志轮询切割脚本

[root@yu63 extra]# cat cut_nginx_log.sh

#!/bin/sh

Dateformat=`date +%Y%m%d`

Basedir="/usr/local/nginx"

Nginxlogdir="$Basedir/logs"

Logname="access_www"

[ -d $Nginxlogdir ] && cd $Nginxlogdir||exit 1

[ -f ${Logname}.log ]||exit 1

/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log

/bin/mv access_mobanche1.log ${Dateformat}_access_mobanche1.log

/bin/mv access_mobanche2.log ${Dateformat}_access_mobanche2.log

$Basedir/sbin/nginx -s reload

[root@yu63 extra]# crontab -l  #crontab -e编写

45 22 * * * /bin/bash  /usr/local/nginx/sbin/cut_nginx_log.sh

 


本文出自 “末班车” 博客,请务必保留此出处http://blxueyuan.blog.51cto.com/9673381/1929432

WEB服务器-Nginx之虚拟主机、日志、认证及优化

标签:nginx 虚拟主机   优化   认证 别名

原文地址:http://blxueyuan.blog.51cto.com/9673381/1929432

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!