第十章 nginx常用配置介绍

时间:2023-03-09 17:13:33
第十章 nginx常用配置介绍

一、虚拟主机

1.配置方式

#虚拟主机配置方式:
1.基于多IP的方式
2.基于多端口的方式
3.基于多域名的方式

2.方式一:基于多IP的方式

1.第一个配置文件
[root@web02 /etc/nginx/conf.d]# vim mali.conf
server {
  listen 10.0.0.8:80;
  server_name localhost;
  location / {
      root /code/zhiwu;
      index index.html;
  }
}

2.第二个配置文件
[root@web02 /etc/nginx/conf.d]# vim tank.conf
server {
  listen 172.16.1.8:80;
  server_name localhost;
  location / {
      root /code/tank;
      index index.html;
  }
}

3.检查配置重启
[root@web02 /etc/nginx/conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@web02 /etc/nginx/conf.d]# systemctl restart nginx

3.方式二:基于多端口的方式

1.第一个配置
[root@web02 /etc/nginx/conf.d]# cat mali.conf
server {
  listen 80;
  server_name localhost;
  location / {
      root /code/zhiwu;
      index index.html;
  }
}

2.第二个配置
[root@web02 /etc/nginx/conf.d]# cat tank.conf
server {
  listen 81;
  server_name localhost;
  location / {
      root /code/tank;
      index index.html;
  }
}

3.检查配置并重启
[root@web02 /etc/nginx/conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@web02 /etc/nginx/conf.d]# systemctl restart nginx

4.访问测试
1)访问 http://10.0.0.8:80
2)访问 http://10.0.0.8:81

4.方式三:基于多域名的方式

1.第一个配置
[root@web02 /etc/nginx/conf.d]# vim mali.conf
server {
  listen 80;
  server_name www.mali.com;
  location / {
      root /code/zhiwu;
      index index.html;
  }
}

2.第二个配置
[root@web02 /etc/nginx/conf.d]# vim tank.conf
server {
  listen 80;
  server_name www.tank.com;
  location / {
      root /code/tank;
      index index.html;
  }
}
3.检查并重启
[root@web03 /etc/nginx/conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@web03 /etc/nginx/conf.d]# systemctl restart nginx

4.配置本地hosts
C:\Windows\System32\drivers\etc\hosts
10.0.0.8 www.mali.com
10.0.0.8 www.tank.com

5.访问测试

5.日志配置

1.第一个配置
[root@web02 /etc/nginx/conf.d]# vim mali.conf
server {
  listen 80;
  server_name www.mali.com;
  access_log /var/log/nginx/www.mali.com.log main;
  location / {
      root /code/zhiwu;
      index index.html;
  }
}

2.第二个配置
[root@web02 /etc/nginx/conf.d]# vim tank.conf
server {
  listen 80;
  server_name www.tank.com;
  access_log /var/log/nginx/www.tank.com.log main;
  location / {
      root /code/tank;
      index index.html;
  }
}

3.访问页面,查看日志

二、日志

1.概述

Nginx有非常灵活的日志记录模式,每个级别的配置可以有各自独立的访问日志。日志格式通过log_format命令定义格式。

第十章 nginx常用配置介绍

2.日志语法

#配置语法: 包括: error.log access.log
#指定格式 日志格式名称   日志格式     日志内容
Syntax: log_format name [escape=default|json] string ...;
Default: log_format combined "...";
Context: http

3.log_format日志格式

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                 '$status $body_bytes_sent "$http_referer" '
                 '"$http_user_agent" "$http_x_forwarded_for"';

4.log_format日志参数

$remote_addr        # 记录客户端IP地址
$remote_user        # 记录客户端用户名
$time_local         # 记录通用的本地时间
$time_iso8601       # 记录ISO8601标准格式下的本地时间
$request            # 记录请求的方法以及请求的http协议
$status             # 记录请求状态码(用于定位错误信息)
$body_bytes_sent    # 发送给客户端的资源字节数,不包括响应头的大小
$bytes_sent         # 发送给客户端的总字节数
$msec               # 日志写入时间。单位为秒,精度是毫秒。
$http_referer       # 记录从哪个页面链接访问过来的
$http_user_agent    # 记录客户端浏览器相关信息
$http_x_forwarded_for #记录客户端IP地址
$request_length     # 请求的长度(包括请求行, 请求头和请求正文)。
$request_time       # 请求花费的时间,单位为秒,精度毫秒
# 注:如果Nginx位于负载均衡器,nginx反向代理之后, web服务器无法直接获取到客户端真实的IP地址。
# $remote_addr获取的是反向代理的IP地址。 反向代理服务器在转发请求的http头信息中,
# 增加X-Forwarded-For信息,用来记录客户端IP地址和客户端请求的服务器地址。

5.日志切割

1.脚本
[root@web02 ~]# vim /etc/logrotate.d/nginx
#指定切割的日志
/var/log/nginx/*.log {
#每天切割一次
      daily
       #忽略丢失的日志
      missingok
       #日志的保留时间
      rotate 52
       #日志文件压缩
      compress
       #延时压缩
      delaycompress
       #忽略空文件
      not if empty
       #指定日志文件权限
      create 640 nginx adm
       #开启脚本
      shared scripts
       #脚本内容
      postrotate
      #判断nginx是否启动
               if [ -f /var/run/nginx.pid ]; then
              #生成新的nginx访问日志
                       kill -USR1 `cat /var/run/nginx.pid`
               fi
       #结束脚本
      endscript
}

三、nginx常用模块

1.目录索引模块ngx_http_autoindex_module

1.概述
该ngx_http_autoindex_module模块处理以斜杠字符(' /')结尾的请求,生成目录列表。
通常,ngx_http_autoindex_module当ngx_http_index_module模块找不到索引文件时,会将请求传递给模块 。

2.语法
Syntax: autoindex on | off;
Default: autoindex off;
Context: http, server, location

3.配置
[root@web02 /etc/nginx/conf.d]# vim mali.conf
server {
  listen 80;
  server_name www.mali.com;
  access_log /var/log/nginx/www.mali.com.log json;
  location / {
      root /code/zhiwu;
      index index.html;
  }
  location /download {
      root /code;
      autoindex on;
      index index.html;
  }
}

4.常用优化参数
#显示文件字节大小 配置 on 显示字节 配置 off 显示方便读取的大小 K/M/G
Syntax: autoindex_exact_size on | off;
Default: autoindex_exact_size on;
Context: http, server, location

#显示时间,如果是off,时间与真实时间相差8小时,如果是on,是真实时间
Syntax: autoindex_localtime on | off;
Default: autoindex_localtime off;
Context: http, server, location

#字符集问题
charset utf-8;

5.完整配置
[root@web02 /etc/nginx/conf.d]# vim mali.conf
server {
  listen 80;
  server_name www.mali.com;
  charset utf-8;
  access_log /var/log/nginx/www.mali.com.log json;
  location / {
      root /code/zhiwu;
      index index.html;
  }
  location /download {
      root /code;
      autoindex on;
      autoindex_exact_size off;
      autoindex_localtime on;
      index index.html;
  }
}

2.访问控制模块 ngx_http_access_module

1.语法
#允许访问的语法
Syntax: allow address | all;
Default: —
Context: http, server, location, limit_except

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

2.配置访问控制案例
1)案例一:#要求允许10.0.0.1可以访问我的/download页面,其他网站不允许
[root@web02 /etc/nginx/conf.d]# cat mali.conf
server {
  listen 80;
  server_name www.mali.com;
  charset utf-8;
  access_log /var/log/nginx/www.mali.com.log json;
  location / {
      root /code/zhiwu;
      index index.html;
  }
  location /download {
      root /code;
  autoindex on;
  autoindex_exact_size off;
  autoindex_localtime on;
index index.html;
allow 10.0.0.1;
deny all;
  }
}

2)案例二:#要求:10.0.0.1不能访问我的/download,其他网站都可以访问
[root@web02 /etc/nginx/conf.d]# cat mali.conf
server {
  listen 80;
  server_name www.mali.com;
  charset utf-8;
  access_log /var/log/nginx/www.mali.com.log json;
  location / {
      root /code/zhiwu;
      index index.html;
  }
  location /download {
  root /code;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
index index.html;
deny 10.0.0.1;
allow all;
  }
}

#all的配置不管是允许还是拒绝,都一定配置在最后

3)案例三:#要求:允许10.0.0.0网段访问我的/download,拒绝其他网段
[root@web02 /etc/nginx/conf.d]# cat mali.conf
server {
  listen 80;
  server_name www.mali.com;
  charset utf-8;
  access_log /var/log/nginx/www.mali.com.log json;
  location / {
      root /code/zhiwu;
      index index.html;
  }
  location /download {
      root /code;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
      index index.html;
allow 10.0.0.0/24;
deny all;
  }
}

4)案例四:#要求:访问www.mali.com,出现游戏界面,访问www.mali.com/download,出现目录页面,且download页面只允许10.0.0.3访问
[root@web02 /etc/nginx/conf.d]# cat mali.conf
server {
  listen 80;
  server_name www.mali.com;
  charset utf-8;
  access_log /var/log/nginx/www.mali.com.log json;
  location / {
      root /code/zhiwu;
      index index.html;
  }
  location /download {
root /code;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
index index.html;
allow 10.0.0.3;
deny all;
  }
}

3.访问控制模块ngx_http_auth_basic_module

1.语法
#开启认证控制,没有任何作用就是为了开启
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

2.配置文件及密码
#生成密码文件
[root@web02 /etc/nginx/conf.d]# htpasswd -c /etc/nginx/auth_basic lhd
New password:
Re-type new password:
Adding password for user lhd

#生成密码,在命令行输入密码
[root@web02 /etc/nginx/conf.d]# htpasswd -b -c /etc/nginx/auth_basic lhd linux
Adding password for user lhd

#查看
[root@web02 /etc/nginx/conf.d]# vim /etc/nginx/auth_basic
lhd:$apr1$JmblF9to$jDnvQn1w7oETPYyvaL2OG.

3.配置
[root@web02 /etc/nginx/conf.d]# cat test.conf
server {
listen 80;
server_name www.test.com;

location / {
root /code;
index index.html;
}
location /download {
root /code;
index index.html;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
allow 10.0.0.1;
deny all;
auth_basic "输入用户名和密码";
auth_basic_user_file /etc/nginx/auth_basic;
  }
}

4.添加多用户
#不添加-c参数可以添加多个用户
[root@web02 /etc/nginx/conf.d]# htpasswd /etc/nginx/auth_basic lhd
New password:
Re-type new password:
Adding password for user lhd

[root@web02 /etc/nginx/conf.d]# vim /etc/nginx/auth_basic
qiudao:$apr1$UL89inf6$.59e04v5ILGHpkMs2xZzF.
lhd:$apr1$9fOQ/hLl$DEugqKzv.0SNBziFMLdVZ1

4.nginx状态模块ngx_http_stub_status_module

1.语法
Syntax: stub_status;
Default: —
Context: server, location

2.配置
[root@web02 /etc/nginx/conf.d]# cat test.conf
server {
listen 80;
server_name www.test.com;

location / {
root /code;
index index.html;
}
location /download {
root /code;
index index.html;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
allow 10.0.0.1;
deny all;
auth_basic "输入用户名和密码";
auth_basic_user_file /etc/nginx/auth_basic;
  }
location /status {
stub_status;
  }
}

3.状态页
#访问 http://www.test.com/status

#返回内容
Active connections: 2
server accepts handled requests
 2 2   1
Reading: 0 Writing: 1 Waiting: 1

#nginx七种状态
Active connections #活跃的连接数
accepts   #接受的TCP连接数
handled   #已处理的TCP连接数
requests   #请求数
Reading   #读取的请求头的数量
Writing   #响应的请求头的数量
Waiting   #等待的请求数量

#可以用作监控日PV
[root@web02 /etc/nginx/conf.d]# curl -s http://www.test.com/status | awk 'NR==3 {print $3}'

5.连接限制模块ngx_http_limit_conn_module

1.语法
#设置限制的空间
Syntax: limit_conn_zone key zone=name:size;
Default: —
Context: http

limit_conn_zone #调用限制模块
key   #存储的内容
zone=   #空间
name:   #空间的名字
size;   #空间的大小

#调用空间
Syntax: limit_conn zone number;
Default: —
Context: http, server, location

limit_conn #调用空间
zone #空间名字
number; #同一个信息可以保存的次数

2.配置
[root@web02 /etc/nginx/conf.d]# cat test.conf

limit_conn_zone $remote_addr zone=conn_zone:1m;
server {
listen 80;
server_name www.test.com;
limit_conn conn_zone 1;

location / {
root /code;
index index.html;
}
location /download {
root /code;
index index.html;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
allow 10.0.0.1;
deny all;
auth_basic "输入用户名和密码";
auth_basic_user_file /etc/nginx/auth_basic;
  }
location /status {
stub_status;
  }
}

6.请求限制模块ngx_http_limit_req_module

1.语法
#设置请求限制的空间
Syntax: limit_req_zone key zone=name:size rate=rate [sync];
Default: —
Context: http

limit_req_zone #调用模块
key #空间存储的内容
zone= #指定空间
name: #空间的名字
size #空间的大小
rate=rate; #读写速率

#调用空间
Syntax: limit_req zone=name [burst=number] [nodelay | delay=number];
Default: —
Context: http, server, location

limit_req #调用空间
zone=name #指定空间名字
[burst=number] #扩展
[nodelay | delay=number];  #延时

2.配置
[root@web02 /etc/nginx/conf.d]# cat test.conf
limit_conn_zone $remote_addr zone=conn_zone:1m;
limit_req_zone $remote_addr zone=req_zone:1m rate=1r/s;

server {
listen 80;
server_name www.test.com;
limit_conn conn_zone 1;
limit_req zone=req_zone;
location / {
root /code;
index index.html;
}
location /download {
root /code;
index index.html;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
allow 10.0.0.1;
deny all;
auth_basic "输入用户名和密码";
auth_basic_user_file /etc/nginx/auth_basic;
  }
location /status {
stub_status;
  }
}

3.测试请求限制
[root@web02 /etc/nginx/conf.d]# ab -n 200 -c 2 http://www.test.com/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.test.com (be patient)
Completed 100 requests
Completed 200 requests
Finished 200 requests


Server Software:       nginx/1.18.0
Server Hostname:       www.test.com
Server Port:            80

Document Path:         /
Document Length:        13 bytes

Concurrency Level:      2
Time taken for tests:   0.036 seconds
Complete requests:      200
Failed requests:        199
  (Connect: 0, Receive: 0, Length: 199, Exceptions: 0)
Write errors:           0
Non-2xx responses:      199
Total transferred:      73674 bytes
HTML transferred:       39216 bytes
Requests per second:    5492.24 [#/sec] (mean)
Time per request:       0.364 [ms] (mean)
Time per request:       0.182 [ms] (mean, across all concurrent requests)
Transfer rate:          1975.76 [Kbytes/sec] received

Connection Times (ms)
            min mean[+/-sd] median   max
Connect:        0    0   0.8      0      12
Processing:     0    0   0.6      0       4
Waiting:        0    0   0.5      0       4
Total:          0    0   1.0      0      12

Percentage of the requests served within a certain time (ms)
 50%      0
 66%      0
 75%      0
 80%      0
 90%      0
 95%      0
 98%      4
 99%      4
100%     12 (longest request)
[root@web02 /etc/nginx/conf.d]#

四、nginx的location配置

1.概述

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

2.location语法

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

3.location匹配符

匹配符 匹配规则 优先级
= 精确匹配 1
^~ 以某个字符串开头 2
~ 区分大小写的正则匹配 3
~* 不区分大小写的正则匹配 3
/ 通用匹配,任何请求都会匹配到 4

4.优先级验证

[root@web02 /etc/nginx/conf.d]# cat location.conf 
server {
  listen 80;
  server_name www.location.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 ^~";
  }
}

5.location应用场景

#通用匹配,任何请求都会匹配到
location / {
  ...
}

#严格区分大小写,匹配以.php结尾的都走这个location    
location ~ \.php$ {
  ...
}

#严格区分大小写,匹配以.jsp结尾的都走这个location
location ~ \.jsp$ {
  ...
}

#不区分大小写匹配,只要用户访问.jpg,gif,png,js,css结尾的都走这条location
location ~* .*\.(jpg|gif|png|js|css)$ {
  ...
}

#不区分大小写匹配
location ~* "\.(sql|bak|tgz|tar.gz|.git)$" {
  ...
}

五、nginx实战演练

1.需求

1.恢复快照
2.三台机器使用官方源安装
3.搭建小游戏
4.使用模块搭建目录索引页面,可以下载文件

2.环境准备

主机 角色 IP
web01 使用官方源搭建nginx完成相关要求 10.0.0.7
web02 使用官方源搭建nginx完成相关要求 10.0.0.8
web03 使用官方源搭建nginx完成相关要求 10.0.0.9

3.web01服务器官方源搭建nginx

1.关闭防火墙
[root@web01 ~]# systemctl stop firewalld
[root@web01 ~]# systemctl disable firewalld

2.关闭selinux
[root@web01 ~]# setenforce 0        
[root@web01 ~]# vim /etc/selinux/config
SELINUX=disabled

3.配置官方源
[root@web01 ~]# vim /etc/yum.repos.d/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

4.安装依赖
[root@web01 ~]# yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree

5.安装nginx
[root@web01 ~]# yum -y install nginx

6.启动服务并验证服务
[root@web01 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      6125/rpcbind        
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      9456/nginx: master  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      7108/sshd          
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      7249/master        
tcp6       0      0 :::111                 :::*                   LISTEN      6125/rpcbind        
tcp6       0      0 :::22                   :::*                   LISTEN      7108/sshd          
tcp6       0      0 ::1:25                 :::*                   LISTEN      7249/master        
7.配置nginx文件
[root@web01 ~]# vim /etc/nginx/nginx.conf
user www;

8.创建统一用户
[root@web01 ~]# groupadd -g 666 www
[root@web01 ~]# useradd -u 666 -g 666 www

9.配置nginx站点目录
[root@web01 /code/download]# vim /etc/nginx/conf.d/tank.conf
server {
  listen 80;
  server_name localhost;
  charset utf-8;
  access_log /var/log/nginx/tank.log main;

location / {
  root /code/tank;
  index index.html;
  }

location /download {
  root /code;
  autoindex on;
  autoindex_exact_size off;
  autoindex_localtime on;
  index index.html;
  }
}


10.创建目录
[root@web01 ~]# mkdir /code
[root@web01 ~]# mkdir /code/download

11.上传游戏压缩包并解压到指定文件夹
[root@web01 ~]# rz -E
rz waiting to receive.
[root@web01 ~]# ll
total 172
-rw-------. 1 root root   1350 2020-06-09 21:42 anaconda-ks.cfg
-rw-r--r--. 1 root root    497 2020-08-05 16:53 hostname_ip.sh
-rw-r--r--  1 root root 167427 2020-06-05 00:00 tank.zip
[root@web01 ~]#
[root@web01 ~]# unzip tank.zip -d /code/

12.上传文件到指定目录
[root@web01 /code/download]# ll
total 340
-rw-r--r-- 1 root root  53519 2020-08-19 14:46 HTTP_Status_Chrome插件_1_0_2_.crx
-rw-r--r-- 1 root root  26995 2020-08-13 16:42 kaoshi.zip
-rw-r--r-- 1 root root  64723 2020-06-04 18:40 tuixiangzi.zip
-rw-r--r-- 1 root root 195152 2020-06-04 18:38 wuziqi.zip

13.授权目录
[root@web01 /code/download]#chown -R www:www /code

14.检查服务并重启
[root@web01 /code/download]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@web01 /code/download]# systemctl restart nginx

4.web02服务器官方源搭建nginx

1.关闭防火墙
[root@web02 ~]# systemctl stop firewalld
[root@web02 ~]# systemctl disable firewalld

2.关闭selinux
[root@web02 ~]# setenforce 0        
[root@web02 ~]# vim /etc/selinux/config
SELINUX=disabled

3.配置官方源
[root@web02 ~]# vim /etc/yum.repos.d/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

4.安装依赖
[root@web02 ~]# yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree

5.安装nginx
[root@web02 ~]# yum -y install nginx

6.启动服务并验证
[root@web02 ~]# systemctl restart nginx
[root@web02 ~]# ps aux |grep nginx
root      24880  0.0  0.0  46352   968 ?       Ss   16:18   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     24881  0.0  0.0  46744  1668 ?       S    16:18   0:00 nginx: worker process
root      24883  0.0  0.0 112708   972 pts/2   R+   16:18   0:00 grep --color=auto nginx


7.配置nginx文件
user www;

8.创建统一用户
[root@web02 ~]# groupadd -g 666 www
[root@web02 ~]# useradd -u 666 -g 666 www

9.配置nginx站点文件
[root@web02 ~]# vim /etc/nginx/conf.d/default.conf
server {
  listen       80;
  server_name www.jindada.com;
  access_log /var/log/nginx/www.jindada.com.log main;
  charset utf-8;

location / {
  root /code/tuixiangzi;
  index index.html;
}

location /mydownload {
  root /code;
  index index.html;
  autoindex on;
  autoindex_exact_size off;
  autoindex_localtime on;
}
}

10.创建目录
[root@web02 ~]# mkdir /code
[root@web02 ~]# mkdir /code/mydownload

11.上传游戏压缩包并解压到指定文件夹
[root@web02 ~]# rz
[root@web02 ~]# ll
total 72
-rw-------. 1 root root  1350 2020-06-09 21:42 anaconda-ks.cfg
-rw-r--r--. 1 root root   497 2020-08-05 16:53 hostname_ip.sh
-rw-r--r--  1 root root 64723 2020-06-04 18:40 tuixiangzi.zip
[root@web02 ~]# unzip tuixiangzi.zip -d /code/

12.上传文件到指定文件夹
[root@web02 ~]# ll /code/mydownload/
total 1020
-rw-r--r-- 1 root root  17742 2017-11-30 15:41 mysql函数练习1.docx
-rw-r--r-- 1 root root 714029 2017-12-11 14:46 mysql视图操作练习.docx
-rw-r--r-- 1 root root      0 2020-08-24 16:33 ؍rz
-rw-r--r-- 1 root root    289 2017-10-16 15:01 作业注意事项.txt
-rw-r--r-- 1 root root  26934 2017-11-02 14:20 单表查询操作.docx
-rw-r--r-- 1 root root  55632 2017-12-07 16:25 数据库操作命令和截图.docx
-rw-r--r-- 1 root root 120070 2017-10-19 16:10 数据查询操作.docx
-rw-r--r-- 1 root root  15662 2017-11-02 15:07 数据查询操作练习.docx
-rw-r--r-- 1 root root  77312 2017-10-16 21:08 新建_Microsoft_Word_文档.doc

13.授权目录
[root@web02 ~]# chown -R www:www /code/

14.配置本地域名解析
C:\Windows\System32\drivers\etc
10.0.0.6   www.jindada.com

15.检查服务和重启服务
[root@web02 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web02 ~]# systemctl restart nginx

5.web03服务器官方源搭建nginx

1.关闭防火墙
[root@web03 ~]# systemctl stop firewalld
[root@web03 ~]# systemctl disable firewalld

2.关闭selinux
[root@web03 ~]# setenforce 0        
[root@web03 ~]# vim /etc/selinux/config
SELINUX=disabled

3.配置官方源
[root@web03 ~]# vim /etc/yum.repos.d/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

4.安装依赖
[root@web03 ~]# yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree

5.安装nginx
[root@web03 ~]# yum -y install nginx

6.启动服务和验证服务
[root@web03 ~]# systemctl start nginx
[root@web03 ~]# ps aux |grep nginx
root      24946  0.0  0.0  46352   976 ?       Ss   16:43   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     24947  0.0  0.0  46744  1936 ?       S    16:43   0:00 nginx: worker process
root      24949  0.0  0.0 112708   976 pts/2   R+   16:43   0:00 grep --color=auto nginx

7.配置nginx主配置文件
[root@web03 ~]# vim /etc/nginx/nginx.conf
user www;

8.创建统一用户
[root@web03 ~]# groupadd -g 666 www
[root@web03 ~]# useradd -u 666 -g 666 www

9.配置nginx站点目录
[root@web03 ~]# vim /etc/nginx/conf.d/default.conf
server {
  listen       80;
  server_name www.jh.com;
  charset utf-8;
  access_log /var/log/nginx/www.jh.com.log main;

location / {
  root /code/wuziqi;
  index index.html;
}

location /jhdownload {
  root /code;
  index index.html;
  autoindex on;
  autoindex_exact_size off;
  autoindex_localtime on;
}
}

10.创建目录
[root@web03 ~]# mkdir /code
[root@web03 ~]# mkdir /code/jhdownload

11.上传游戏压缩包并解压到指定目录
[root@web03 ~]# rz
[root@web03 ~]# ll
total 200
-rw-------. 1 root root   1350 2020-06-09 21:42 anaconda-ks.cfg
-rw-r--r--. 1 root root    497 2020-08-05 16:53 hostname_ip.sh
-rw-r--r--  1 root root 195152 2020-06-04 18:38 wuziqi.zip
[root@web03 ~]# unzip wuziqi.zip -d /code/

12.上传文件到指定目录
root@web03 /code/jhdownload]#ll
total 1852
-rw-r--r-- 1 root root 545433 2017-08-30 16:01 云计算安全框架V2.0部署手册.pdf
-rw-r--r-- 1 root root 545433 2017-08-30 16:01 云计算安全框架V2.0部署手册.pdf.0
-rw-r--r-- 1 root root 795660 2017-08-30 16:01 先电云计算网络搭建操作手册-Clo

13.授权目录
[root@web03 /code/jhdownload]# chown -R www:www /code/

14.修改本地域名解析文件
C:\Windows\System32\drivers\etc
10.0.0.9 www.jh.com

15.检查服务并重启
[root@web03 /code/jhdownload]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web03 /code/jhdownload]#
[root@web03 /code/jhdownload]# systemctl restart nginx

6.测试

1.web01服务器
可以正常访问游戏页面和文件页面
2.web02服务器
可以正常访问游戏页面和文件页面
3.web03服务器
可以正常访问游戏页面和文件页面