Nginux

一.关于Nginux

一款高性能,轻量级WEB服务软件,稳定性高,系统资源消耗低,对HTTP并发连接的处理能(单台物理服务器可支持3000~50000个并发请求)Nginx配置文件支持正则表达式。

二.Nginux编译安装

2.1安装依赖包

yum -y install gcc gcc-c++ pcre-devel  zlib-devel  make 

2.2tar解包

tar zxf  nginx-1.12.2.tar.gz  -C /opt

2.3编译安装Nginux

cd /opt/nginx-1.12.2/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_modul
//--with-http_stub_status_modul为开启统计模块

2.4make 编译,make install 安装

make && make install  

2.5创建运行用户

useradd -M -s /sbin/nologin nginx 

2.6优化配置文件路径

ls -n /usr/local/nginx/sbin/*  /usr/local/sbin/

2.7检查, 启用,添加系统管理

①常用命令

nginux -t #检查配置文件是否正确
nginux    #启动

②停止nginx服务

cat /usr/local/nginx/logs/nginx.pid  查看nginx的PID号
kill -3 pid号
kill -s QUIT pid号
killall -3 nginx 
killall -s QUIT nginx // -s 选项指定信号种类,HUP信号表示重载配置,QUIT型号表示退出进程

三.添加Nginx系统服务

3.1在/etc/init.d目录下写入脚本用于service管理

vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20							# chkcofig - “-” 表示不启用开机启动管理 (同时 若不加“#”, chkconfig add nginx 会加载不到配置)
# description: Nginx Service Control Script
COM="/usr/local/nginx/sbin/nginx"				#命令程序文件位置(nginx)
PID="/usr/local/nginx/logs/nginx.pid"			#pid文件
case "$1" in
start)
   $COM
   ;;
stop)
   kill -s QUIT $(cat $PID)
   ;;
restart)
   $0 stop
   $0 start
   ;;
reload)
   kill -s HUP $(cat $PID)
   ;;
*)
       echo "Usage: $0 {start|stop|restart|reload}"
       exit 1
esac
exit 0

/etc/rc.d/ 系统管理识别的目录

chmod +x /etc/init.d/nginx 
chkconfig --add nginx  #添加为系统服务 
systemctl start nginx

2.3使用systemctl管理

vim  /usr/lib/systemd/system/nginx.service
[Unit]	
Description=nginx							#描述
After=network.target						#描述服务类别
[Service]
Type=forking								#后台运行类型
PIDFile =/usr/local/nginx/logs/nginx.pid	#PID文件位置
ExecStart=/usr/local/nginx/sbin/nginx		#启动服务
ExecrReload=/bin/kill -s HUP $MAINPID		#根据PID重载配置
ExecrStop=/bin/kill -s QUIT $MAINPID		#根据PID终止进程
PrivateTmp=true
[Install]
WantedBy=multi-user.target					#启动级别
[root@qingdong system]# chmod 754 
[root@qingdong system]# systemctl start nginx.service 
[root@qingdong system]# systemctl enable nginx.service 

四.详解Nginx配置文件

#user  nobody;     #默认运行/管理用户
worker_processes  1;   #工作进程运行数量,可配置成服务器内核数*2,如果网站访问量不大,一般设为1

#error_log  logs/error.log;  #错误日志文件路径/级别
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;   #PID文件位置


events {
    worker_connections  1024;   #每个进程最多处理的连接数量
}
# 如如提高每个进程的连接数还需执行“ulimit -n 65535(临时调整)命令临时修改本地每个进程可以同时打开的最大文件数”

http {
    include       mime.types;   #http协议的配置
    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"';

    #access_log  logs/access.log  main; #访问日志位置

    sendfile        on;  #支持文件发送(下载)
    #tcp_nopush     on;   
#此项允许或禁止使用socket的TcP_CORK的选项(发送数据包前先缓存数据),此选项仅在使用sendfile的时候使用

    #keepalive_timeout  0;  #连接保持超时时间
    keepalive_timeout  65;

    #gzip  on;               #压缩模块 on  表示开启

    server {                #web 服务的监听配置
        listen       80;     # 默认监听端口
        server_name  localhost;  #站点域名

        #charset koi8-r; #字符集支持(修改为中文)UTF-8

        #access_log  logs/host.access.log  main; #此 web服务的访问日志

        location / {    #“/”根目录位置 html位置
            root   html;  #网站根目录位置/usr/local/nginx/html(相对路径)
            index  index.html index.htm; #支持的首页文件格式
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html; #当发生错误的时候能够显示一个预定义的错误页面
        location = /50x.html {  #错误页面配置
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80 
        #以下是支持PHP及跳转的配置
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {      #虚拟主机的配置
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server    #HTTPS的配置
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;  #证书文件
    #    ssl_certificate_key  cert.key;   #证书的秘钥文件

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

五.访问状态统计

①使用命令nginx -V 查看是否含有 http_stub_status_module 模块
②修改/usr/local/nginx/conf/nginx.conf配置文件,指定访问位置并添加stub_status配置

在这里插入图片描述
③重启访问
在这里插入图片描述
Active connections :表示当前的活动连接数;
service accepts handled requests:表示已经处理的连接信息,三个数字依次表示已处理的连接数、成功的ncR握手次数、已处理的请求数。

五.访问控制

5.1基于授权的访问控制

htpawwd : htpasswd是一个用于目录访问权限认证的一个工具。
-c:创建密码文件,如果文件存在,那么内容被清空重写
1、生成用户密码认证文件

yum install httpd-tools

htpasswd -c /usr/ local/nginx/passwd.db zhangsan
#添加Inginx管理、给与400权限

chown nginx /usr/ local/ nginx/passwd.db
chmod 400 /usr/ local/nginx/passwd. db

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

vim /usr/ local/nginx/ conf/ nginx.conf
location / {
auth basic "secret" ;
#在主页配置项中添加认证
auth basic_user_file /usr/ local/nginx/passwd.db;
#在主页配置项中添加认证
root  html;
index index.html index.htm;

次是访问首页需要账户和密码
在这里插入图片描述
location设置
我们在不同的location位置可以做不同的功能限制,比如我们可以让用户可以直接访问首页但不允许用户直接访问状态统计模块

5.3基于客户端的访问控制

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

vim /usr/ local/nginx/ conf/ nginx.conf
location / {
root html;
index index.html index.htm;
deny 192.168.80.10;
#添加拒绝访问的客户瑞的IP
allow all;
#添加允许其他IP客户端访问

六.虚拟主机

6.1基于域名的Nginx虚拟主机

①添加域名解析

echo "192.168.80.2 www.zhaosan.com  www.lisi.com" >> /etc/hosts

②准备虚拟站点网页文档

[root@qingdong ~]# mkdir -p /var/www/html/zhangsan 
[root@qingdong ~]# mkdir -p /var/www/html/lisi 
[root@qingdong ~]# echo "<h1> hello </h1>" > /var/www/html/zhangsan/index.html
[root@qingdong ~]# echo "<h1> hi </h1>" > /var/www/html/lisi/index.html

③修改配置文件

vim /usr/local/nginx/conf/nginx.conf
  server {
 36         listen       80;
 37         server_name  www.zhangsan.com;
 38         charset utif-8;
 39         access_log  logs/zhangsan.access.log;
 40         location / {
 41             root   /var/www/html/zhangsan/;
 42             index  index.html index.htm;
 43         }
 44         error_page   500 502 503 504  /50x.html;
 45         location = /50x.html {
 46             root   html;
 47         }
 48 
 49     }
 50     server {
 51         listen       80;
 52         server_name  www.lisi.com;
 53         charset utif-8;
 54         access_log  logs/lisi.access.log;
 55         location / {
 56             root   /var/www/html/lisi/;
 57             index  index.html index.htm;
 58         }
 59         error_page   500 502 503 504  /50x.html;
 60         location = /50x.html {
 61             root   html;
 62         }
 63 
 64 }

在这里插入图片描述
④访问
在这里插入图片描述
在这里插入图片描述

6.2基于端口的虚拟主机

①创建8080端口的网页文件

mkdir -p /var/www/html/zhangsan8080/
echo "<h1>www.zhangsan8080.com</h1>" >  /var/www/html/zhangsan8080/index.html
server {
 35         listen  192.168.80.2:80;
 36         server_name  www.zhangsan.com;
 37         charset utif-8;
 38         access_log  logs/zhangsan.access.log;
 39         location / {
 40             root   /var/www/html/zhangsan/;
 41             index  index.html index.htm;
 42         }   
 43         error_page   500 502 503 504  /50x.html;
 44         location = /50x.html {
 45             root   html;
 46         }   
 47         
 48     }   
 49     server {
 50         listen      192.168.80.2:8080;        #指向8080端口
 51         server_name  www.zhangsan.com;
 52         charset utif-8;
 53         access_log  logs/zhangsan8080.access.log;  #便于区分,指定生成不同日志
 54         location / {
 55             root   /var/www/html/zhangsan8080/;      #指向8080端口的站点网页
 56             index  index.html index.htm;
 57         }
 58         error_page   500 502 503 504  /50x.html;
 59         location = /50x.html {
 60             root   html;
 61         }

②重启服务访问
在这里插入图片描述
在这里插入图片描述

6.3基于IP地址

①临时添加一块网卡

ifconfig ens33:0 192.168.80.50 netmask 255.255.255.0
mkdir -p /var/www/html/lisi50
echo "<h1>www.lisi50.com</h1>" > /var/www/html/lisi50/index.html 
echo "192.168.80.50 www.lisi.com" >> /etc/hosts
    server {
        listen  192.168.80.2:80;
        server_name  www.zhangsan.com;
        charset utif-8;
        access_log  logs/zhangsan.access.log;
        location / {
            root   /var/www/html/zhangsan/;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

    server {
        listen 192.168.80.50:80;
        server_name  www.lisi.com;
        charset utif-8;
        access_log  logs/lisi50.access.log;
        location / {
            root   /var/www/html/lisi50/;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

}

②访问
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值