nginx(官方安装) Nginx相关文件 Nginx模块 location

案例

项目需求:

搭建小游戏

支持备份

限制192.168.1.8访问

部署nfs+sersync

1、backup

#安装rsync
[root@backup ~]# yum install rsync -y
[root@backup ~]# yum install -y nfs-utils rpcbind

#编写rsync服务端配置文件
[root@backup ~]# vim /etc/rsyncd.conf
uid = rsync
gid = rsync 
port = 873 
use chroot = no
fake super = yes
max connections = 200
timeout = 600
ignore errors     
read only = false
list = false      
auth users = yzl
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
[backup]
comment = welcome to oldboyedu backup!
path = /backup
#创建rsync服务需要使用的普通用户
[root@backup ~]# useradd rsync
#将用户名和密码写入rsync密码文件
[root@backup ~]# echo "yzl:1" >/etc/rsync.passwd
#更改密码文件权限
[root@backup ~]# chmod 600 !$
chmod 600 /etc/rsync.passwd
#创建模块目录,并更改其属组属主
[root@backup ~]# mkdir /backup
[root@backup ~]# chown -R www.www !$
#创建挂载点
[root@backup ~]# vim /etc/exports
/backup 172.16.1.0/24(rw,sync,all_squash)
#开启nfs和rsync服务
[root@backup ~]#  systemctl start nfs
[root@backup ~]# showmount -e
Export list for backup:
/backup 172.16.1.0/24
[root@backup ~]# systemctl start rsyncd


2、nfs

#将密码写入rsync客户端密码文件中,并更改权限为600
[root@nfs ~]# echo "1" >>/etc/rsync.passwd
[root@nfs ~]# chmod 600 !$
#创建挂载点
[root@nfs ~]#  vim /etc/exports
/sersync 172.16.1.0/24(rw,sync,all_squash,anonuid=1000,anongid=1000)

#创建用户并修改同步目录属主属组
[root@nfs ~]# useradd www -u 1000
[root@nfs ~]#  chown -R www.www /sersync
#开启nfs服务
[root@nfs ~]# systemctl start nfs-server rpcbind
#上传sersync压缩包
[root@nfs sersync]# rz -E
rz waiting to receive.
[root@nfs sersync]# tar -xf sersync.gz
[root@nfs sersync]# mv GNU-Linux-x86/* ./
#编辑sersync配置文件
[root@nfs sersync]# vim confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
    <host hostip="localhost" port="8008"></host>
    <debug start="false"/>
    <fileSystem xfs="false"/>
    <filter start="false">
	<exclude expression="(.*)\.svn"></exclude>
	<exclude expression="(.*)\.gz"></exclude>
	<exclude expression="^info/*"></exclude>
	<exclude expression="^static/*"></exclude>
    </filter>
    <inotify>
	<delete start="true"/>
	<createFolder start="true"/>
	<createFile start="true"/>
	<closeWrite start="true"/>
	<moveFrom start="true"/>
	<moveTo start="true"/>
	<attrib start="true"/>
	<modify start="true"/>
    </inotify>

    <sersync>
	<localpath watch="/sersync">
	    <remote ip="172.16.1.41" name="backup"/>
	    <!--<remote ip="192.168.8.39" name="tongbu"/>-->
	    <!--<remote ip="192.168.8.40" name="tongbu"/>-->
	</localpath>
	<rsync>
	    <commonParams params="-az"/>
	    <auth start="true" users="yzl" passwordfile="/etc/rsync.passwd"/>
	    <userDefinedPort start="false" port="873"/><!-- port=874 -->
	    <timeout start="false" time="100"/><!-- timeout=100 -->
	    <ssh start="false"/>
	</rsync>
	<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
	<crontab start="false" schedule="600"><!--600mins-->
	    <crontabfilter start="false">
		<exclude expression="*.php"></exclude>
		<exclude expression="info/*"></exclude>
	    </crontabfilter>
	</crontab>
	<plugin start="false" name="command"/>
    </sersync>

    <plugin name="command">
	<param prefix="/bin/sh" suffix="" ignoreError="true"/>	<!--prefix /opt/tongbu/mmm.sh suffix-->
	<filter start="false">
	    <include expression="(.*)\.php"/>
	    <include expression="(.*)\.sh"/>
	</filter>
    </plugin>

    <plugin name="socket">
	<localpath watch="/opt/tongbu">
	    <deshost ip="192.168.138.20" port="8009"/>
	</localpath>
    </plugin>
    <plugin name="refreshCDN">
	<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
	    <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
	    <sendurl base="http://pic.xoyo.com/cms"/>
	    <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
	</localpath>
    </plugin>
</head>

#运行测试
[root@nfs sersync]# ./sersync2 -dro confxml.xml 


下载nginx并安装


[root@web03 opt]# wget http://nginx.org/download/nginx-1.19.10.tar.gz
[root@web03 opt]# tar -xf nginx-1.19.10.tar.gz 
[root@web03 opt]# cd nginx-1.19.10/
[root@web03 nginx-1.19.10]# useradd www -u 1000


#检查系统配置
[root@web3 nginx-1.19.10]# ./configure --user=www --group=www --prefix=/usr/local/nginx   --with-stream    --with-mail        --with-http_perl_module      --without-http_gzip_module     --with-http_ssl_module

#解决依赖
[root@web03 nginx-1.19.10]# yum install -y pcre pcre-devel
[root@web03 nginx-1.19.10]# yum install -y openssl openssl-devel
[root@web03 nginx-1.19.10]# yum install perl-ExtUtils-Embed

#编译并安装
[root@web03 nginx-1.19.10]# make && make install

#将nginx加入环境变量
[root@web03 nginx-1.19.10]# vim /etc/profile
shift + g  按 o
NGINX_HOME=/usr/local/nginx/sbin
PATH=$PATH:$NGINX_HOME
export PATH
[root@web03 nginx-1.19.10]# source /etc/profile

#测试环境变量是否添加成功
[root@web03 nginx-1.19.10]# nginx -V

#解决启动nginx (Unit not found)问题
[root@web03 nginx-1.19.10]# vim /etc/init.d/nginx
#!/bin/sh
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
接下来就依次操作以下命令:
[root@web03 nginx-1.19.10]# cd /etc/init.d
[root@web03 init.d]# chmod 755 /etc/init.d/nginx
[root@web03 init.d]# chkconfig --add nginx
[root@web03 init.d]# service nginx start


配置nginx小游戏页面

1、nginx

#修改配置文件内容
[root@web03 init.d]# vim /usr/local/nginx/conf/nginx.conf


user  www;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


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"';

    access_log  /usr/local/nginx/logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;
    include /usr/local/nginx/conf.d/*.conf;
    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        access_log  logs/host.access.log  main;

        location / {
            root   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
        #
        #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
    #
    #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;
    #    }
    #}

}

#创建网页配置文件存放目录
[root@web03 init.d]# mkdir /usr/local/nginx/conf.d

#自定义网站配置文件
[root@web03 conf.d]# vim /usr/local/nginx/conf.d/zuoye.conf
server{
    listen 80;
    server_name www.zuoye.com;
    location / {
    root /code/zuoye;
    index index.html;
    allow all;
    deny 192.168.1.8;

    }

}

#创建站点目录
[root@web03 conf.d]# mkdir /code/zuoye -p

#上传网站所需的html文件
[root@web03 conf.d]# cd /code/zuoye/
[root@web03 zuoye]# rz -E
rz waiting to receive.
[root@web03 zuoye]# unzip kaoshi.zip 

#更改站点目录的属主属组
[root@web03 zuoye]# chown -R www.www /code

#测试重启
[root@web03 zuoye]# 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@web03 zuoye]# systemctl restart nginx



2、因为nginx无法处理动态请求,所以这里我们需要安装php

#下载php源码包到opt目录,然后解压
[root@web03 ~]# cd /opt
[root@web03 opt]# wget https://www.php.net/distributions/php-5.6.40.tar.gz
[root@web03 opt]# tar -xf php-5.6.40.tar.gz 
[root@web03 opt]# cd php-5.6.40/ls 
#检查系统配置,并指定安装模块与用户
[root@web03 php-5.6.40]# ./configure --prefix=/usr/local/php --with-mysql --enable-fpm --with-pdo-mysql --with-fpm-user=www --with-fpm-group=www --with-zlib --with-libxml-dir
#解决依赖
[root@web03 php-5.6.40]# yum install libxml2 libxml2-devel -y
#编译并安装
[root@web03 ~]# make -j && make install
#加入环境变量
[root@web03 ~]# vim /etc/profile
PATH=$PATH:/usr/local/php/sbin
export PATH
#启动php服务
[root@web03 ~]# systemctl start php-fpm
#通常源码安装无法直接用systemctl启动所以运行以下代码:
[root@web03 ~]# vi /lib/systemd/system/php-fpm.service
[Unit]
Description=php-fpm
After=network.target
[Service]
Type=forking
ExecStart=你的文件目录(我:/usr/local)/php/sbin/php-fpm
ExecStop=/bin/pkill -9 php-fpm
PrivateTmp=true
[Install]
WantedBy=multi-user.target
#添加完成以后保存,使用systemctl list-unit-files --type=service查看有没有php-fpm.service
#如果没有就是用systemctl daemon-reload重新加载,在使用以上命令查看

#添加开机自启 
systemctl enable php-fpm.service
[root@web03 ~]# systemctl start php-fpm

详情可参考
https://www.cnblogs.com/ikai/p/13691706.html


3、nginx绑定php

[root@web03 ~]# vim /usr/local/nginx/conf.d/zuoye.conf 

server{
    listen 80;
    server_name www.zuoye.com;
    location / {
    root /code/zuoye;
    index index.html;
    }
    location ~* \.php$ {
        fastcgi_pass localhost:9000;
        #fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /code/zuoye/$fastcgi_script_name;
        include fastcgi_params;
    }
}
#重启nginx与php
[root@web03 ~]# systemctl restart nginx
[root@web03 ~]# systemctl restart php-fpm

#重启失败:cat/var/log/messages:发现配置文件名不对,改名
[root@web03 local]# cd /usr/local/php/etc/
[root@web03 etc]# mv php-fpm.conf.default php-fpm.conf

#更改配置文件
[root@web03 etc]# vim  /code/zuoye/upload_file.php 
<?php

header("Content-type:text/html;charset=utf-8");

ini_set('date.timezone','Asia/Shanghai');


//$wen="C:\wamp\www\linux-54-".date("Y-m-d");
$wen="/你自己的目录(我:/code/zuoye)/upload";
$dd=date("Y-m-d");
$pre=preg_match("/^([0-9])+_/",$_FILES['file']["name"][0]);
$size =$_FILES['file']["size"][0];

if (!is_dir($wen.'/')) {

  MKDIR($wen.'/', 0777);

}




// foreach($_FILES['file']['error'] as $k=>$v){

  if ($_FILES["file"]["error"][0] > 0 ) {
    echo "上传失败!请查看是否选择了需要上传的文件!";
    }else if($pre==0){
   
    echo "上传失败!文件名有误,请修改文件名为你的编号加下划线开头<br/>例如:1_老男孩_lnmp架构.mp4";


  }else if ($size<10) {

    echo "上传失败!文件为空文件!";
  }else{
   
    $tmp_name = $_FILES["file"]["tmp_name"][0];
    $name =$_FILES["file"]["name"][0];
	$name = iconv('utf-8','gb2312',$name);

    if (file_exists($wen."/" . $name))
      {
      echo "上传失败,文件".$_FILES["file"]["name"][0] . " 已经存在 ";
      }
    else
      {
      move_uploaded_file($tmp_name,$wen."/" . $name);
      echo "文件".$_FILES["file"]["name"][0]."上传成功";
      }
    
}

// }
?>


## 测试:

将192.168.1.9   www.zuoye.com加入c:/windows/system32/drivers/etc/hosts文件中

在浏览器上传文件后,查看backup上/backup中是否有上传的文件

nginx(官方安装)

# 安装官方源
##  nginx.org ---> documentation ---> Installing nginx ---> packages --->   RHEL/CentOS

# http://nginx.org/en/linux_packages.html#RHEL-CentOS

[root@web01 ~]# cd /etc/yum.repos.d/
[root@web01 yum.repos.d]# vim nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[root@web1 ~]# yum clean all
[root@web1 ~]# yum makecache
[root@web1 ~]# yum install nginx -y

#启动
[root@web01 yum.repos.d]# systemctl start nginx
#加入开机自启
[root@web01 ~]# systemctl enable nginx
#nginx重新加载配置文件
[root@web01 ~]# systemctl reload nginx

Nginx相关文件

#查看nginx相关配置文件
[root@web01 ~]# rpm -ql nginx

1.Nginx主配置文件

路径类型作用
/etc/nginx/nginx.conf配置文件nginx主配置文件
/etc/nginx/conf.d/default.conf配置文件默认网站配置文件

2.Nginx代理相关参数文件

路径类型作用
/etc/nginx/fastcgi_params配置文件Fastcgi代理配置文件(php)
/etc/nginx/scgi_params配置文件scgi代理配置文件
/etc/nginx/uwsgi_params配置文件uwsgi代理配置文件(python)

3.Nginx编码相关配置文件

路径类型作用
/etc/nginx/win-utf配置文件Nginx编码转换映射文件
/etc/nginx/koi-utf配置文件Nginx编码转换映射文件
/etc/nginx/koi-win配置文件Nginx编码转换映射文件
/etc/nginx/mime.types配置文件Content-Type与扩展名

4.Nginx管理相关命令

路径类型作用
/usr/sbin/nginx命令Nginx命令行管理终端工具
/usr/sbin/nginx-debug命令Nginx命令行与终端调试工具

4.Nginx日志相关目录与文件

路径类型作用
/var/log/nginx目录Nginx默认存放日志目录
/etc/logrotate.d/nginx配置文件Nginx默认的日志切割

nginx安装、nginx配置文件、nginx日志、nginx的多站点、nginx日志切割、nginx排错。

nignx是一个高性能的WEB服务器,nginx支持多个模块。

nginx的autoindex模块

ngx_http_autoindex_module,作用是生成一个目录列表

  • 格式
Syntax:		autoindex on | off;		# 该选项的参数
Default:	autoindex off;			# 该选项的默认值
Context:	http, server, location	# 该选项可以使用的模块
  • 开启目录索引
server {
	listen 80;
	server_name localhost;

	location / {
		root /usr/share/nginx/html;
    		autoindex on;
	}
}
  • 格式化文件大小
server {
	listen 80;
	server_name localhost;

	location / {
			root /usr/share/nginx/html;
    		autoindex on;
    		autoindex_exact_size off;
	}
}
  • 显示本地时间
server {
	listen 80;
	server_name localhost;
	location / {
		root /usr/share/nginx/html;
    	autoindex on;
		autoindex_exact_size off;
		autoindex_localtime on;
	}
}
  • 案例
# 部署一个yum仓库

# 创建一个yum仓库目录
mkdir /usr/share/nginx/yum

[root@web01 ~]# cat /etc/nginx/conf.d/yum.conf 
server {

	listen 80;
	server_name www.yum.com;
	location / {
		root /usr/share/nginx/yum;
		autoindex on;
	}
	
}

# 重启nginx

# 编写yum源
[root@web01 yum.repos.d]# cat /etc/yum.repos.d/test.repo 
[nginx-test]
name='test nginx'
baseurl=http://www.yum.com/  # 注:需要解析www.yum.com

# 测试
[root@web01 yum.repos.d]# yum makecache



访问控制模块ngx_http_access_module

ngx_http_access_module: 允许限制对某些客户端地址的访问。

  • 格式
# 允许访问
Syntax:	allow address | CIDR | unix: | all;
Default:	—
Context:	http, server, location, limit_except

# 拒绝访问
Syntax:	deny address | CIDR | unix: | all;
Default:	—
Context:	http, server, location, limit_except

  • 测试禁止一个IP
server {

        listen 80;
        server_name www.deny.com;
        location / {
                root /usr/share/nginx/yum;
                index 50x.html
                deny 192.168.15.1;
                allow all;
        }

}
  • 只允许某一个IP访问
server {

	listen 80;
	server_name www.deny.com;
	location / {
		root /usr/share/nginx/yum;
		index index.html;
		allow 192.168.15.10;
		deny all;
	}
}
  • 允许某一个网段的IP来访问
server {

	listen 80;
	server_name www.deny.com;
	location / {
		root /usr/share/nginx/yum;
		index index.html;
		allow 192.168.15.0/24;
		deny all;
	}
}
  • 案例
# 只允许公司内部人员(192.168.15.0/24)来访问下载目录
server {

	listen 80;
	server_name www.game.com;
	location /download {
        root /usr/share/nginx/game/;
    	autoindex on;
		autoindex_exact_size off;
		autoindex_localtime on;

		allow 192.168.15.0/24;
		deny all;
	}

	location / {
		root /usr/share/nginx/game; 
		index index.html;
	}
	
}

nginx状态模块 ngx_http_stub_status_module

ngx_http_status_module :监控模块最主要的是监控nginx服务

  • 格式
Syntax:	stub_status;
Default:	—
Context:	server, location
  • 测试案例
Active connections: 2 
server accepts handled requests
 		2 		2 		1 
Reading: 0 Writing: 1 Waiting: 1 

Active connections: # 活跃的连接数
accepts # TCP连接总数
handle # 成功的TCP连接数
requests # 请求数
Reading # 读取请求头部
Writing # 放回给客户端的头部
Waiting # 等待的请求数


# 长链接和短连接
keepalive_timeout  0;

[root@web01 conf.d]# cat status.conf 
server {
	listen 80;
	server_name www.status.com;
	location / {
		stub_status;
	}
}

七种状态

Active connections: 2 
server accepts handled requests
 		 4 		 4 		56 
Reading: 0 Writing: 1 Waiting: 1 

Active connections:		 # 活跃的连接数
accepts					# TCP连接总数
handle					# 成功的TCP连接数
requests				# 请求数

Reading					# 读取请求头部
Writing					# 放回给客户端的头部
Waiting					# 等待的请求数

#注意:一次tcp连接,可以发起多次请求;
keepalive_timeout  0;   #类似于关闭长连接
keepalive_timeout  65;	#最长65秒没有活动则断开连接

访问认证密码模块 ngx_http_auth_basic_module

浏览器访问增加一个密码。

  • 格式
# 指定密码简介
Syntax:	auth_basic string | off;
Default:	auth_basic off;
Context:	http, server, location, limit_except

# 指定密码文件
Syntax:	auth_basic_user_file file;
Default:	—
Context:	http, server, location, limit_except
  • 案例
# 创建密码文件
[root@web01 conf.d]# yum provides htpasswd
[root@web01 nginx]# htpasswd -c /etc/nginx/string nginx
[root@web01 nginx]# htpasswd -c -b 1 /etc/nginx/string nginx

# nginx 配置文件
server {

	listen 80;
	server_name www.auth.basic.com;
	location / {

		root /usr/share/nginx/game;
		index index.html;

		charset utf-8,gbk;

		auth_basic "简介";
		auth_basic_user_file /etc/nginx/string;	

	}

}

# 测试登录

连接限制模块 ngx_http_limit_conn_module与ngx_http_limit_req_module

ngx_http_limit_conn_module:限制连接数

ngx_http_limit_req_module:限制速率

  • 限制连接数
# 调用限制连接模块
Syntax:	limit_conn zone number;
Default:	—
Context:	http, server, location

# 定义限制连接模块
Syntax:	limit_conn_zone key zone=name:size;
Default:	—
Context:	http

limit_conn_zone $binary_remote_addr zone=addr:10m;

server {
    location /download/ {
        limit_conn addr 1;
    }
}
  • 限制速率
# 调用
Syntax:	limit_req zone=name [burst=number] [nodelay | delay=number];
Default:	—
Context:	http, server, location

# 定义格式
Syntax:	limit_req_zone key zone=name:size rate=rate [sync];
Default:	—
Context:	http


limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;

server {
    location /search/ {
        limit_req zone=one burst=5;
    }
}

nodelay : 延时请求

对比

ngx_http_limit_conn_module : 限制连接数据

ngx_http_limit_req_module :限制访问的频率

Nginx location

使用Nginx Location可以控制访问网站的路径,但一个server可以有多个location配置, 多个location的优先级该如何区分

语法

Syntax:	location [ = | ~ | ~* | ^~ | / ] uri { ... }
		location @name { ... }
Default:	—
Context:	server, location

location匹配符(优先级)

在这里插入图片描述

验证location匹配顺序

[root@web02 conf.d]# vim testlocation.conf
server {
    listen 80;
    server_name www.linux.com;
    #location / {
    #    default_type text/html;
    #    return 200 "location /";
    #}

    location =/ {
        default_type text/html;
        return 200 "location =/";
    }

   location ~ / {
        default_type text/html;
        return 200 "location ~/";
    }

   location ^~ / {
      default_type text/html;
      return 200 "location ^~";
    }
}

验证访问文件

[root@web01 conf.d]# cat testserver.conf 
server {
    listen 80;
    server_name www.server.com;
	location / {
    	root /code;
	}
	 
	location ~ \.php$ {
	    root /php;
	}
	 
	location ~ \.jsp$ {
	    root /jsp;
	}
	 
	location ~* \.(jpg|gif|png|js|css)$ {
	    root /pic;
	}
	 
	location ~* \.(sql|bak|tgz|tar.gz|git)$ {
	    root /package;
	}      
}

www.server.com/1.PHP
www.server.com/2.JPG
www.server.com/3.jsp
www.server.com/4.tGz
www.server.com/5.Gif

系统优化小脚本

#!/bin/bash

rm -rf /etc/yum.repos.d/*
curl -o /etc/yum.repos.d/CentOS-Base.repo https://repo.huaweicloud.com/repository/conf/CentOS-7-reg.repo
cat >/etc/yum.repos.d/huawei_epel.repo<<EOF
[huawei_epel]
baseurl=https://repo.huaweicloud.com/epel/7/x86_64/
name="huawei"
enabled=1
gpgcheck=0
EOF
cat >/etc/yum.repos.d/nginx.repo<<EOF
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/\$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF
yum clean all
yum makecache
yum -y install tree nmap sysstat lrzsz telnet bash-completion vim lsof net-tools rsync ntpdate nfs-utils
systemctl disable --now firewalld
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
systemctl disable --now NetworkManage
yum update -y

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

琴声浮或沉__听懂只一人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值