Nginx访问状态以及基于多域名、多端口、多IP配置虚拟主机

一、关于Nginx

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

稳定性高

系统资源消耗低

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

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

二、Nginx的优化服务

2.1 编译安装

[root@localhost ~]# yum -y install pcre-devel zlib-devel
[root@localhost ~]# tar zxf nginx-1.12.2.tar.gz
[root@localhost ~]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module 	#统计状态模块

[root@localhost nginx-1.12.2]# make && make install
[root@localhost ~]# useradd -M -s /sbin/nologin nginx  	#创建一个不可登录的程序用户
[root@localhost ~]# ln -s /usr/local/nginx/conf/nginx.conf /etc/nginx
[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/bin 	#优化执行路径
[root@localhost ~]# nginx 	#启动服务
[root@localhost ~]# netstat -anpt | grep ngin
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      3580/nginx: master
[root@localhost ~]# killall -s QUIT nginx	 #选项-s QUIT等于-3 停止服务
[root@localhost ~]# vi /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}"
        exit 1
esac
exit 0

[root@localhost ~]# chmod +x /etc/init.d/nginx
[root@localhost ~]# chkconfig --add nginx

2.2 Nginx访问状态的统计

  • 配置文件修改
[root@nginx ~]# vi /usr/local/nginx/conf/nginx.conf
user  nginx nginx 		#修改并删掉"#"号
error_log  logs/error.log  info 		#删掉"#"号

events {
    use epoll; 			#新增此行,默认使用select/poll
    worker_connections  1024; 		#表示一个工作进程允许1024个连接
}

 location ~ /status {  
             stub_status  on;
             access_log  off;
             }			#在配置统计功能
 
 [root@nginx ~]# 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
[root@nginx ~]# nginx -V		#检查开启的模块
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@nginx ~]# systemctl restart nginx
  • 结果
    在这里插入图片描述

2.3 Nginx身份验证访问

  • 创建能够访问web网站的用户
[root@localhost ~]# yum -y install httpd-tools
[root@localhost ~]# which htpasswd
/usr/bin/htpasswd				#htpasswd命令需要先安装httpd-tools
[root@localhost ~]# htpasswd -c /usr/local/nginx/passwd.db ben
New password: 
Re-type new password: 
Adding password for user ben
[root@localhost ~]# ll /usr/local/nginx/ | grep passwd.db
-rw-r--r--. 1 root  root   42 Nov 16 11:22 passwd.db
[root@localhost ~]# chown nginx.nginx /usr/local/nginx/passwd.db 
[root@localhost ~]# chmod 400 /usr/local/nginx/passwd.db 
[root@localhost ~]# vi /etc/nginx
 location / {
            auth_basic "secret";
            auth_basic_user_file /usr/local/nginx/passwd.db;
            root   html;
            index  index.html index.htm;
        }
        
[root@localhost ~]# systemctl restart nginx
  • 验证

在这里插入图片描述

在这里插入图片描述

  • 拒绝访问

deny IP/IP段:拒绝某个IP或IP段的客户端访问
allow IP/IP段:允许某个IP或IP段的客户端访问
规则从上往下执行,如匹配则停止,不再往下匹配

[root@localhost ~]# vi /etc/nginx 
 location / {
            auth_basic "secret";
            auth_basic_user_file /usr/local/nginx/passwd.db;
            deny   all;
            root   html;
            index  index.html index.htm;
        }
[root@localhost ~]# systemctl restart nginx
  • 结果验证

在这里插入图片描述

三、配置Nginx虚拟主机

3.1 基于域名

  • 修改配置文件
[root@nginx ~]# vi /etc/nginx 
    server {
        listen       80;
        server_name  www.test-1.com;		#localhost修改成www.test-1.com

        charset utf-8;

        #access_log  logs/host.access.log  main;
        
        location / {
            root   /var/www/test-1/;
            index  index.html index.htm;
        }
        
    server {
        listen       80;        
        server_name  www.test-2.com; 
        charset utf-8;

        #access_log  logs/host.access.log  main;

        location / {
            root   /var/www/test-2/;
            index  index.html index.htm;
        }
    }

[root@nginx ~]# systemctl restart nginx

在Windows 10的hosts里添加
10.0.0.71		www.test-1.com www.test-2.com
  • 创建不同网页
[root@localhost ~]# mkdir -p /var/www/test-1
[root@localhost ~]# mkdir -p /var/www/test-2
[root@localhost ~]# echo "<center>Web-1</center>" > /var/www/test-1/index.html
[root@localhost ~]# echo "<center>Web-2</center>" > /var/www/test-2/index.html
  • 结果验证

在这里插入图片描述

在这里插入图片描述

3.2 基于IP

  • 修改配置文件
[root@nginx ~]# ip a | grep '/16' | awk '{print $2}' | awk -F '/' '{print $1}'
10.0.0.71
10.0.0.108
[root@nginx ~]# vi /etc/nginx 
    server {
        listen       10.0.0.71:80;
        server_name  www.test-1.com;		

        charset utf-8;

        #access_log  logs/host.access.log  main;
        
        location / {
            root   /var/www/test-1/;
            index  index.html index.htm;
        }
        
    server {
        listen       10.0.0.108:80;      	#修改成另一个IP地址  
        server_name  www.test-2.com; 
        charset utf-8;

        #access_log  logs/host.access.log  main;

        location / {
            root   /var/www/test-2/;
            index  index.html index.htm;
        }
    }
    
[root@nginx ~]# systemctl restart nginx
[root@nginx ~]# netstat -anpt | grep nginx
tcp        0      0 10.0.0.108:80           0.0.0.0:*               LISTEN      1091/nginx: master  
tcp        0      0 10.0.0.71:80            0.0.0.0:*               LISTEN      1091/nginx: master  
  • 结果验证
    在这里插入图片描述

在这里插入图片描述

3.3 基于端口

  • 修改配置文件
[root@nginx ~]# vi /etc/nginx 
    server {
        listen       10.0.0.71:80;
        server_name  www.test-1.com;		

        charset utf-8;

        #access_log  logs/host.access.log  main;
        
        location / {
            root   /var/www/test-1/;
            index  index.html index.htm;
        }
        
    server {
        listen       10.0.0.71:8008;    	#开放8008端口    
        server_name  www.test-2.com; 
        charset utf-8;

        #access_log  logs/host.access.log  main;

        location / {
            root   /var/www/test-2/;
            index  index.html index.htm;
        }
    }

[root@nginx ~]# systemctl restart nginx
[root@nginx ~]# netstat -anpt | grep nginx
tcp        0      0 10.0.0.71:80            0.0.0.0:*               LISTEN      1123/nginx: master  
tcp        0      0 10.0.0.71:8008          0.0.0.0:*               LISTEN      1123/nginx: master  
  • 结果验证

在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值