文章目录

  • 案例
  • 部署nfs+sersync
  • 1、backup
  • 2、nfs
  • 下载nginx并安装
  • 配置nginx小游戏页面
  • 1、nginx
  • 2、因为nginx无法处理动态请求,所以这里我们需要安装php
  • 3、nginx绑定php
  • nginx(官方安装)
  • Nginx相关文件
  • nginx的autoindex模块
  • 访问控制模块ngx_http_access_module
  • nginx状态模块 ngx_http_stub_status_module
  • 七种状态
  • 访问认证密码模块 ngx_http_auth_basic_module
  • 连接限制模块 ngx_http_limit_conn_module与ngx_http_limit_req_module
  • 对比
  • Nginx location
  • 语法
  • location匹配符(优先级)
  • 验证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.confuser  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.confserver{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
<?phpheader("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.htmldeny 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 requests2        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 requests4         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:    httplimit_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:    httplimit_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/bashrm -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

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

  1. Nginx增加m3u8流视频文件功能模块 推流和拉流

    Nginx增加m3u8流视频文件功能模块 推流和拉流 FastDFS分布式文件系统安装和配置_亲测成功 ./configure --prefix=/u06/data/apps/nginx-rtmp \ ...

  2. nginx yum安装后的文件路径

    1.nginx.conf路径:/etc/nginx/ 2.日志路径:/var/log/nginx 3.安装目录:/etc/nginx/nginx.conf 4.config: /etc/sysconf ...

  3. php7.0搭配nginx,Linux安装配置php7.0+nginx

    2.安装nginx 查看nginx相关信息 yum list | grep nginx collectd-nginx.x86_64 4.10.9-1.el6 epel munin-nginx.noar ...

  4. CentOS7.9下nginx的安装与配置(实现任意目录下均可直接执行 nginx 命令,以及开机自启动)

    目录 1.​​​​​​Nginx介绍 2.Nginx和apache的优缺点 2.1nginx相对于apache的优点: 2.2apache 相对于nginx 的优点: 3.Tengine介绍 3.1t ...

  5. Keepalived+Nginx实现高可用,反向代理---Keepalived安装及相关配置

    [前言] 在博文<智能一代云平台(十五):Keepalived+Nginx实现高可用,反向代理---Nginx安装及配置>中介绍了Nginx的安装及相关配置的信息,由于篇幅原因不能用一篇博 ...

  6. Nginx编译-安装-配置-优化实践总结

    http://www.zhuxiaodong.net/2016/configure-nginx-server-support-https-http2-on-centos7/ 一些更新说明: 2016- ...

  7. nginx的安装及简单负载均衡配置

    Nginx 是一个很强大的高性能Web和反向代理服务器,它具有很多非常优越的特性.本人目前所在公司也使用nginx,主要用来做负载均衡服务器.同时也可以作为邮件代理服务器. 1. nginx的安装.本 ...

  8. 完全卸载nginx及安装的详细步骤

    前言 在开局配置Nginx时有可能会配置错误,报各种错误代码.看不懂或者懒得去看这个报错时,其实最简单的方式是卸载并重装咯.今天就带大家一起学习下,如何彻底卸载nginx程序. 一.卸载NGINX 卸 ...

  9. Linux中完全卸载nginx及安装的详细步骤

    前言 在开局配置Nginx时有可能会配置错误,报各种错误代码.看不懂或者懒得去看这个报错时,其实最简单的方式是卸载并重装咯.今天就带大家一起学习下,如何彻底卸载nginx程序. 一.卸载NGINX 卸 ...

最新文章

  1. 详细介绍!Linux 上几种常用的文件传输方式
  2. 搜索引擎:文本分类——TF/IDF算法
  3. IDEA插件-生成对象所有set方法--->GenerateAllSetter
  4. 采用PHP实现”服务器推”技术的聊天室
  5. 防遗忘笔记,Fedora交叉编译window下的virt-iewer的汉化
  6. 【每周CV论文推荐】 初学GAN必须要读的文章
  7. Windows 10 下 Anaconda3 (Python 3.8) 配置 OpenCV-4.4.0
  8. KBQA相关论文分类整理:简单KBQA和复杂KBQA
  9. [翻译] Fast Image Cache
  10. react dispatch_记 react 项目在 TypeScript 化中的一个坑,以及相应的类型改动
  11. binlog日志整理
  12. OkHttp3用法全解析
  13. 统一认证 - Apereo CAS 客户端的集成以及小结
  14. 微信号码检测工具为什么不能免费使用?(2017)
  15. 财务数字转换--大小写转换
  16. linux mysql 视频教程_Linux视频教程基础入门到精通Shell高级编程实战/Nginx/MySQL运维视频教程下载...
  17. 工作日记:JavaScript生成随机色
  18. 【Nginx】关于二级域名配置https
  19. 周超臣:支付宝的套路是我走过最长的路
  20. 业余草告诉你甲骨文正式宣布将 Java EE 移交给 Eclipse 基金会

热门文章

  1. 手机如何制作gif动图?一招教你快速制作gif动图
  2. 解决ios输入框无法自动弹起的问题
  3. 大数据治理平台建设方案(文末附PDF下载)
  4. 游戏建模里的POLY思维和Nurbs建模思维,你知道什么意思吗?
  5. c语言剪裁矩形,六、矩形、区域和剪裁—CLOVER程序
  6. 「津津乐道播客」#241. 拼娃时代:该不该给孩子读四大名著?
  7. 基于STM32 + SYN6288语音播报
  8. 华为荣耀20和x10比较_客观说说华为畅享20pro和华为荣耀x10有什么区别呢?哪个好?深度测评剖析...
  9. oracle结果相减_oracle 两个时间相减得秒
  10. python多核并行处理数据,python多进程处理数据