Nginx 服务 (编译安装--访问控制--虚拟主机)详解

一、Nginx 服务基础

1.1、Nginx 概述

Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。
Nginx是一款轻量级的Web服务器/反向代理服务器以及电子邮件代理服务器,并在一个BSD-like协议下发行。由俄罗斯的程序设计师lgor Sysoev所开发,供俄国大型的入口网站及搜索引擎Rambler使用。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好。
Nginx相较于Apache\lighttpd具有占有内存少,稳定性高等优势,并且依靠并发能力强,丰富的模块库以及友好灵活的配置而闻名。在Linux操作系统下,nginx使用epoll事件模型,得益此,nginx在Linux操作系统下效率相当高。同时Nginx在OpenBSD或FreeBSD操作统上采用类似于Epoll的高效事件模型kqueue.
—款高性能、轻量级Web服务软件
稳定性高
系统资源消耗低
对HTTP并发连接的处理能力高
单台物理服务器可支持30 000~50 000个并发请求

1.2、Nginx 的编译安装

1.2.1、编译安装步骤

1.挂载宿主机的共享文件夹,读取下载好的源码文件

2.解压跨平台组件包和源码包
将跨平台组件包移动到源码包目录下

3.安装编译器和其他工具

yum -y install gcc \
gcc-c++ \
make \
pcre-devel \
expat-devel \
perl \
zlib-devel \
pcre

4.configure配置

./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module

5.make编译和make install

6.路径优化

ln -s /usr/local/nginx/sbin/nginx  /usr/local/sbin	'//nginx命令执行路径优化'
useradd -M -s /sbin/nologin nginx

1.2.2、使用systemctl控制

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

chmod 754 /lib/systemd/system/nginx.service

1.2.3、开启nginx,关闭防火墙

[root@localhost system]# chmod +754 nginx.service 
[root@localhost system]# systemctl start nginx.service 
[root@localhost system]# netstat -ntap |grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      5396/nginx: master  
tcp        0      0 192.168.197.192:56680   192.168.100.3:445       ESTABLISHED -                   
[root@localhost system]# systemctl stop firewalld.service 
[root@localhost system]# setenforce 0

安装好之后启动服务,在浏览器输入IP,出现这个画面,服务成功启动
在这里插入图片描述

1.3、Nginx 的访问状态统计

ginx内置了 http_stub_status 状态统计模块,用来反馈当前的Web访问情况,配置编译参数时可添加
–with-http_stub_status_module 来启用此模块支持,可使用命令 /usr/local/nginx/sbin/nginx -V 来查看已安装的nginx是否包含 http_stub_status 模块。
要使用 nginx 的状态统计功能,除了启用内建模块,还需要修改nginx.conf 配置文件,指定访问位置并添加 stub_status 配置代码

1.3.1、统计实验

修改nginx.conf配置文件

[root@localhost ~]# vi /usr/local/nginx/conf/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;
        }
    }
}

改好之后 wq 保存,然后用 Nginx -t 查看语法有无报错。如果没有错误,再刷新配置文件
真机输入20.0.0.25
在这里插入图片描述

二、Nginx 访问控制

2.1、基于授权的访问控制

生成用户密码认证文件

[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 ~]# vim /usr/local/nginx/conf/nginx.conf
server {
    
    
	location / {
    
    
		auth_basic "secret";
		auth_basic_user_file /usr/local/nginx/.passwd.db;
	}
[root@localhost ~]# killall -s HUP nginx	

在真机的浏览器中输入20.0.0.25,提示输入账户密码
在这里插入图片描述
点击登陆后 出现 welcome to nginx
在这里插入图片描述

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

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

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

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

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

配置步骤:
修改主配置文件nginx.conf,添加相应配置项
除主机20.0.0.1之外允许其他客户端访问

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
location / {
    
    
            Order deny,allow;
            Deny from 20.0.0.1;
        }
[root@localhost ~]# killall -s HUP nginx

在这里插入图片描述

三、Nginx 虚拟主机

3.1、基于域名的虚拟主机

准备网站目录及测试文件

[root@localhost ~]# mkdir -p /var/www/html/51xit/
[root@localhost ~]# mkdir -p /var/www/html/52xit/
[root@localhost ~]# echo "www.51xit.top" >> /var/www/html/51xit/index.html
[root@localhost ~]# echo "www.52xit.top" >> /var/www/html/52xit/index.html
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
server {
    
    
        listen       80;
        server_name  www.51xit.top;
        charset utf-8;
        access_log  logs/www.51xit.top.access.log;
       location / {
    
    
            root /var/www/html/51xit;
            index  index.html index.htm;
        }
        location /status {
    
    
            stub_status on;
            access_log off;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
    
    
            root   html;
        }
server {
    
    
        listen       80;
        server_name  www.52xit.top;
        charset utf-8;
        access_log  logs/www.52xit.top.access.log;
       location / {
    
    
            root /var/www/html/52xit;
            index  index.html index.htm;
        }
        location /status {
    
    
            stub_status on;
            access_log off;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
    
    
            root   html;
        }
[root@localhost ~]# killall -s HUP nginx

在这里插入图片描述

在这里插入图片描述

3.2、基于IP 的虚拟主机

主机配置两个IP地址

修改配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
server {
    
    
	listen	20.0.0.25:80;
	server name 20.0.0.25:80;
	....}
server {
    
    
	listen	192.168.100.25:80;
	server name 192.168.100.25:80;
	....}
[root@localhost ~]# killall -s HUP nginx

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

3.3、基于端口的虚拟主机

修改配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
server {
    
    
	listen	20.0.0.25:6666;
	server name 20.0.0.25:6666;
	......}
server {
    
    
	listen	20.0.0.25:8888;
	server name 20.0.0.25:8888;
	......}
[root@localhost ~]# killall -s HUP nginx

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

猜你喜欢

转载自blog.csdn.net/weixin_48191211/article/details/108516987