手把手教你,嘴对嘴传达------Nginx网站服务(访问状态统计,基于域名,端口,IP的虚拟web主机访问)

时间:2022-07-24
本文章向大家介绍手把手教你,嘴对嘴传达------Nginx网站服务(访问状态统计,基于域名,端口,IP的虚拟web主机访问),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一、Nginx简介

  • 在各种网站服务器软件中,除了Apache HTTP Server外,还有一款轻量级的HTTP服务器软件–Nginx,其稳定,高效的特性逐渐被越来越多的用户认可
  • 其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名
  • 其特点是:占有内存少,并发能力强 中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。

二、Nginx服务基础

1、Nginx概述

一款高性能、轻量级Web服务软件

●稳定性高

●系统资源消耗低

●对HTTP并发连接的处理能力高

●单台物理服务器可支持30 000 ~ 50000个并发请求

●占用内存少,并发能力强

2、Nginx编译安装(过程)

安装支持软件

[root@localhost ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel

创建运行用户、组

[root@localhost ~]# useradd -M -S /sbin/nologin nginx  '//-M 不创建家目录'

编译安装Nginx ,将软件包提前下载好

[root@localhost nginx-1.12.0]# ./configure 
--prefix=/usr/local/nginx 
--user=nginx 
--group=nginx 
--with-http_stub_ status_ module	'//开启stub_status状态统计模块'
[root@localhost nginx-1.12.0]# make && make install
[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx  /usr/local/sbin	'//nginx命令执行路径优化'
[root@locaThost nginx-1.12.0]# ls -l /usr/local/sbin/nginx
Irwxrwxrwx 1root root27 5月16 16:50 /usr/local/sbin/nginx ->/usr/local/nginx/sbin

运行控制(实验过程) 检查配置文件

[root@localhost ~]# nginx -t	'//检查'

启动、重载配置、停止Nginx

[root@localhost ~]# nginx	'//启动'
[root@localhost ~]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7180/nginx: master
[root@localhost ~]# yum -y install elinks
[root@localhost ~]# elinks http://localhost		'//显示"Welcome to nginx!"页面,表明Nginx服务已经正常运行'
[root@localhost ~]# killall -s HUP nginx	'//-S选项指定信号种类,HUP信号表示重载配置'
[root@localhost ~]# killall -s QUIT nginx	'//QUIT信号表示退出进程'

Nginx添加为系统服务

第一种方法,使用systemctl工具进行管理

[root@localhost ~]# vim /lib/systemd/system/nginx.service		'//添加使用systemctl工具进行管理'
[Unit]
Description=nginx	'//描述'
After=network.target	'//描述服务类别'

[Service]
Type=forking	'//后台运行形势'
PIDFile =/usr/local/nginx/logs/nginx.pid	'//PID文件位置'
ExecStart=/usr/local/nginx/sbin/nginx		'//启动服务'
ExecReload=/usr/bin/kill -S HUP $MAINPID	'//根据PID重载配置'
ExecStop=/usr/bin/kill -S QUIT $MAINPID		'//根据PID终止进程'
PrivateTmp=true

[Install]
WantedBy=multi-user.target
[root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service

第二种方法,添加使用service工具进行管理

[root@localhost ~]# cd /etc/inid.d		'//或者添加使用service工具进行管理'
[root@localhost init.d]# ls
[root@localhost init.d]# vim 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}"
  		exit 1
esac
exit 0
[root@localhost init.d]# chmod +x nginx
[root@localhost init.d]# chkconfig --add nginx
[root@localhost init.d]# chkconfig --level 35 nginx on

此时,开启服务,关闭防火墙,就可以访问nginx网址了

[root@localhost init.d]# service nginx start
[root@localhost init.d]# systemctl stop firewalld
[root@localhost init.d]# setenforce 0
[root@localhost init.d]# netstat -ntap | grep nginx

3、Nginx的访问状态统计

启用HTTP_ STUB_ STATUS状态统计模块

配置编译参数时添加–with-http stub status module

nginx -V查看已安装的Nginx是否包含HTTP_ STUB _STATUS模块

[root@localhost ~]# nginx -V
nginx version: nginx/1.12.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --
group= nginx --with-http_ stub_ status_ module
修改nginx.conf配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
http{
	server {
		listen 80;
		server name localhost;
		charset utf-8;
		location / {
			root html;
			index index.html index.php;
        }
		location ~/status {		'//添加此段'
			stub_ status on;
			access_ log off;
        }

三、Nginx访问控制

1、基于授权的访问控制

配置步骤与Apache基本一致

●生成用户密码认证文件

●修改主配置文件对相应目录,添加认证配置项

●重启服务,访问测试

生成用户密码认证文件

[root@localhost ~]# yum install -y httpd-tools	'//因为没有htpasswd工具,所以需要安装'
[root@localhost ~]# htpasswd -c /usr/local/nginx/passwd.db test
New password:
Re-type new password:
Adding password for user test
root@localhost ~]# cat /usr/local/nginx/ passwd.db
test:$apr1$x.UaSXIM$RRLa2KJcKwsGBVsikGcrR/

修改主配置文件对相应目录,添加认证配置项

[root@localhost ~]# chmod 400 /usr/local/nginx/passwd.db
[root@localhost ~]# chown nginx /usr/local/nginx/passwd.db
[root@localhost ~]# ll -d /usr/local/nginx/passwd.db
-r------- 1 nginx root43 5月16 22:26
/usr/local/nginx/passwd.db

重启服务,访问测试

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
server {
	location / {
		auth_basic "secret";
		auth_basic_user_file /usr/local/nginx/passwd.db;
	}

2、基于客户端的访问控制

通过客户端IP地址,决定是否允许对页面访问

配置规则

deny IP/IP段:拒绝某个IP或IP段的客户端访问

allow IP/IP段:允许某个IP或IP段的客户端访问

规则从上往下执行,如匹配则停止,不再往下匹配

修改主配置文件nginx.conf,添加相应配置项

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
server {
	location/ {
		deny 192.168.110.132;
		allow all; 
    }

四:Nginx虚拟主机

1、Nginx虚拟主机应用

Nginx支持的虚拟主机有三种

●基于域名的虚拟主机

●基于IP的虚拟主机

●基于端口的虚拟主机

通过"server{}" 配置段实现

2、基于域名的虚拟Web主机

配置步骤

●准备网站目录及测试文件

[root@localhost ~]# mkdir -p /var/www/html/shang/
[root@localhost ~]# mkdir -p /var/www/html/zhen/
[root@localhost ~]# echo "this is shang" >>
/var/www/html/shang/index.html
[root@localhost ~]# echo "this is zhen" >>
/var/www/html/zhen/index.html 
[root@localhost ~]# yum install bind -y		'//以下是配置DNS服务过程,具体内容不再赘述,如果不懂,翻阅我之前博客'
[root@localhost ~]# vim  /etc/named.conf
[root@localhost ~]# vim /etc/named.rfc1912.zones
[root@localhost ~]# cp -p /var/named/named.localhost /var/named/shang.com.zone
[root@localhost ~]# vim /var/named/shang.com.zone
[root@localhost ~]# cp -p /var/named/shang.com.zone /var/named/zhen.com.zone
[root@localhost ~]# systemctl start named

调整nginx.conf配置文件

vim /usr/ local/ nginx/ conf/ nginx. conf   //进入主配置文件  
    server {
        listen       80;
        server_name  www.shang.com;
        charset utf-8;
        access_log  logs/www.shang.com.log;
        location / {
            root   /var/www/html/shang;
            index  index.html index.htm;
        }
       error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

    server {
        listen       80;
        server_name  www.zhen.com;
        charset utf-8;
        access_log  logs/www.zhen.com.access.log;
        location / {
            root   /var/www/html/zhen;
            index  index.html index.htm;
        }


       error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

3、基于IP地址访问

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
server {
	listen	192.168.110.132:80;
	server name 192.168.110.132:80;
	....}
server {
	listen	192.168.132.133:80;
	server name 192.168.110.133:80;
	....}