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

news/2024/7/9 16:16:53 标签: 运维, epoll, php

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文件

wKioL1kmiI-wpyO3AAAOyXNaKNc806.png

wKiom1kmiJDjCE5PAAAgXCO-Wjo810.png

wKiom1kmiJDxNxDTAAAfV7rgNY8023.png

wKiom1kmiJDwWEfsAAAd6BNrO1U321.png

 

 

实战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;

        }

    }

}

 

wKioL1kmiKHjrGo0AAAe0rsFihg252.png

wKiom1kmiKKw7NUOAAAdMswdAE0735.png

wKioL1kmiKLSBmciAAAnOQmjwLw931.png

 

实战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

wKioL1kmiK7DW3GFAAAi1eaLrJ4573.png 

实战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

wKiom1kmiLigT3ILAABf7YKepas575.png 

 

 

实战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://www.niftyadmin.cn/n/1777142.html

相关文章

用想象力做产品

我要用想象力做产品研发&#xff0c;研发具有想象力的产品&#xff0c;震撼&#xff0c;感动&#xff0c;给它注入灵魂。 python做的一个产品1万行左右分布式转码系统&#xff0c;千年不死&#xff0c;不眠不休&#xff1a; MediaPlayer base on OSMF, supports HTTP/RTMP/TV…

Dependency Walker使用说明[转]

在Windows世界中&#xff0c;有无数块活动的大陆&#xff0c;它们都有一个共同的名字——动态链接库。现在就让我们走进这些神奇的活动大陆&#xff0c;找出它们隐藏已久的秘密吧&#xff01;   初窥门径:Windows的基石   随便打开一个系统目录&#xff0c;一眼望去就能看到…

监测linux服务器文件夹变化,监测Linux服务器CPU、硬盘、内存等硬件工作状态(上)...

随着Linux应用的日益广泛&#xff0c;有大量的网络服务器使用Linux操作系统。为了全面衡量网络运行状况&#xff0c;就需要对网络状态做更细致、更精确的测量。SNMP协议的制订为互联网测量提供了有力支持。计算机系统是由软件系统硬件系统组成的&#xff0c;检测硬件状态对于保…

P1169 [ZJOI2007]棋盘制作

题目描述 国际象棋是世界上最古老的博弈游戏之一&#xff0c;和中国的围棋、象棋以及日本的将棋同享盛名。据说国际象棋起源于易经的思想&#xff0c;棋盘是一个8*8大小的黑白相间的方阵&#xff0c;对应八八六十四卦&#xff0c;黑白对应阴阳。 而我们的主人公小Q&#xff0c;…

有些相似之处

鹪鹩巢于深林&#xff0c;不过一枝&#xff1b;鼹鼠饮河&#xff0c;不过满腹。 意思是对资源的索取&#xff0c;人类应该效法自然&#xff0c;“不用太嚣张&#xff0c;没什么用”。 软件架构中&#xff0c;有种情况叫分析瘫痪&#xff0c;就是想得太多&#xff1b;软件实现…

开机启动日志 linux,linux笔记之 开机服务启动的控制,系统日志的查看,防火墙的关闭...

/etc/inittab 配置默认运行级别/etc/sysconfig/init 控制tty终端开启数量 终端颜色方案/etc/init/rcS.conf 加载rc.sysinit脚本&#xff0c;完成初始化任务/etc/rc.d/rc.local 可以更改该文件&#xff0c;添加开机默认启动的命令命令&#xff1a;/etc/init.d/服务名称 控制类型…

怀念一些书

对于专业软件工程师&#xff0c;我觉得最重要的是热情&#xff0c;有了热情&#xff0c;看起书来才能消化良好。 我怀念那些在桂花树下慢慢看书的时光&#xff0c;带着崇敬和欣喜。 我怀念一些书&#xff0c;恍如昨日&#xff1a; 《深入浅出MFC》&#xff0c;可能是中国人写…

Redhat5_linux 系统环境下 oracl11g的安装教程图解

linux_oracl11g 安装步骤 操作系统的安装敬请参考此文:VM 安装 linux Enterprise_R5_U4_Server_I386_DVD教程图解 设置linux服务器的静态地址请参考该文《oracl11g的安装教程图解》yum仓库搭建&#xff1a;Vmware Workstation虚拟机 请参考《Vmware Workstation _linux yum 仓…