Nginx基础配置

简介: Nginx基础配置

一、Nginx基础配置


(1)配置文件

******(1)做基础配置
[root@Centos7 ~]# hostnamectl set-hostname rzy
[root@Centos7 ~]# su 
[root@rzy ~]# systemctl stop firewalld
[root@rzy ~]# setenforce 0
setenforce: SELinux is disabled
[root@rzy ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
mount: /dev/sr0 已经挂载或 /mnt 忙
       /dev/sr0 已经挂载到 /mnt 上
******(2)上传Nginx源码包,进行安装
[root@rzy ~]# yum -y install pcre-devel zlib-devel  #安装前提软件
。。。。。。
完毕!
[root@rzy ~]# ll
总用量 1020
-rw-------. 1 root root    1264 1月  12 18:27 anaconda-ks.cfg
-rw-r--r--  1 root root 1039530 4月  21 23:48 nginx-1.18.0.tar.gz
[root@rzy ~]# tar xf nginx-1.18.0.tar.gz  -C /usr/src/
[root@rzy ~]# cd /usr/src/nginx-1.18.0/
[root@rzy nginx-1.18.0]# useradd -M -s /sbin/nologin  nginx
[root@rzy nginx-1.18.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install 
[root@rzy nginx-1.18.0]# cd
[root@rzy ~]# cd /usr/local/nginx/conf/
[root@rzy conf]# cp nginx.conf nginx.conf.bak #做一个配置文件的备份
[root@rzy conf]# ll | grep nginx.conf  #确认备份成功
-rw-r--r-- 1 root root 2656 4月  21 23:51 nginx.conf
-rw-r--r-- 1 root root 2656 4月  21 23:52 nginx.conf.bak
-rw-r--r-- 1 root root 2656 4月  21 23:51 nginx.conf.default
[root@rzy conf]# sed -i '/#/d' nginx.conf  #删除包含#的行
[root@rzy conf]# sed -i '/^$/d' nginx.conf  #删除空行
[root@rzy conf]# vim nginx.conf 
  1 worker_processes  1;    #工作进程,这个要和本机cpu核心的个数保持一致
  2 events {     #event区域,这个区域还可以指定nginx使用的IO模型,使用use指定,默认为epoll
  3     worker_connections  1024;  #表示每个进程可以处理的最大连接数量,即tcp连接
  4 }
  5 http {   #http区域,即网站区域
  6    include       mime.types;  
  7     default_type  application/octet-stream;
  8     sendfile        on;   #零拷贝,默认开启
  9     keepalive_timeout  65;  #长连接的超时时间,以秒为单位
 10     server {   #虚拟主机区域
 11         listen       80;   #监听的端口
 12         server_name  localhost;   #指定域名
 13         location / {   #location资源区域,指定用户访问什么资源时生效什么配置,location /即根,默认匹配,即用户不管访问什么资源都生效
 14             root   html;   #指定存放网页的路径,默认是html,这是一个相对路径,实际路径就是Nginx的安装目录下的html
 15             index  index.html index.htm;  #指定默认访问的首页文件
 16         }
 17         error_page   500 502 503 504  /50x.html;  #指定错误代码,当状态码为指定的数字时,就重定向到指定的网页
 18         location = /50x.html {  #指定用户访问的资源,=为精确匹配
 19             root   html;  #指定存放的目录
 20         }
 21     }
 22 }

个人理解


配置文件中,以{}来分为各种不同的区域,每个区域分别生效每个区域的配置,那么不在区域中的配置也就是{}外的配置就是main全局配置,即不管用户做什么事情全局配置都生效,常用的就是http{}网站区域,而http{}中又包含server{}虚拟主机区域,每个server{}都是一个虚拟主机,在server{}中,又包含了location{}资源区域,当用户访问location指定的资源时,location{}中的配置才会生效。


每一个区域都有着相应的嵌套关系,例如http{}中的配置会对每个server{}区域生效,当然也对每个server{}中的location{}区域生效,而只在server{}区域中的配置,只有对location{}生效,对http{}不生效


嵌套关系:main全局配置——http{}——server{}——location{}


(2)Nginx日志配置

******(1)继续上面的进度,优化Nginx命令,配置网页内容
[root@rzy ~]# ln -s /usr/local/nginx/sbin/nginx  /usr/local/sbin/  #优化nginx命令执行路径
[root@rzy ~]# vim /usr/lib/systemd/system/nginx.service   #编写Nginx的启动脚本
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@rzy ~]# systemctl start nginx
[root@rzy ~]# netstat -anpt | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      3749/nginx: master  
[root@rzy ~]# echo "aaaa" > /usr/local/nginx/html/index.html  #编写页面
[root@rzy ~]# curl 127.0.0.1 #访问本地
aaaa
******(2)使用curl命令查看http的请求和返回
[root@rzy ~]# curl -v http://127.0.0.1  #  >就是请求报文,<就是返回报文
* About to connect() to 127.0.0.1 port 80 (#0)
*   Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> GET / HTTP/1.1     #依次是请求方式GET, 请求资源 /  ,请求使用的协议 HTTP/1.1
> User-Agent: curl/7.29.0  #请求的方式,这里一般是浏览器的版本,curl就是我们刚才使用的curl工具
> Host: 127.0.0.1   #访问的目的ip
> Accept: */*
> 
< HTTP/1.1 200 OK    #依次是返回使用协议HTTP/1.1 ,状态码200 ok
< Server: nginx/1.18.0  #返回的服务器的web版本
< Date: Wed, 21 Apr 2021 16:24:36 GMT  #返回报文的时间
< Content-Type: text/html  #返回的数据类型
< Content-Length: 5  #返回数据的大小(字节数)
< Last-Modified: Wed, 21 Apr 2021 16:23:15 GMT #最近一次服务器修改配置文件的日期
< Connection: keep-alive  #连接模式,keep-alive长连接
< ETag: "60805173-5"
< Accept-Ranges: bytes
< 
aaaa  #具体的数据
* Connection #0 to host 127.0.0.1 left intact
******(3)在主配置文件中配置日志的区域,因为在写配置文件的时候,把#号开头的注释行和空行删除了,所以需要看一下之前备份的配置文件
[root@rzy ~]# vim /usr/local/nginx/conf/nginx.conf.bak 
。。。。。。
 20 
 21     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 22     #                  '$status $body_bytes_sent "$http_referer" '
 23     #                  '"$http_user_agent" "$http_x_forwarded_for"';
 24 
 25     #access_log  logs/access.log  main;
 26 
。。。。。。
#想要使用只需要把这些21到25行去掉#注释然后复制到正常使用的nginx.conf中的http{}区域中即可,这些都是nginx的内建变量,下面是解释:
$remote_addr:客户端的地址
$remote_user:http客户端请求nginx认证用户
$time_local:Nginx的时间
$request:请求行,有GET、Post等方法,HTTP协议的版本
$status:返回的状态码
$body_bytes_sent:从服务器响应给客户端body的信息大小
$http_referer:上一级页面,可以通过这个做防盗链、用户行为分析
$http_user_agent:头部信息,客户端访问使用的设备
$http_x_forwarded_for:请求携带的http,通过这个可以使用代理时也能知道客户端的真实ip

二、Nginx常用优化


(1)Nginx状态监控


  • 必要的模块:–with-http_stub_status_module

- 可以配置在主配置文件的区域:server、location


******(1)继续上面的进度,修改配置文件
[root@rzy ~]# vim /usr/local/nginx/conf/nginx.conf  #在server区域添加一个新的location
  1 worker_processes  1;
  2 events {
  3     worker_connections  1024;
  4 }
  5 http {
  6     include       mime.types;
  7     default_type  application/octet-stream;
  8     sendfile        on;
  9     keepalive_timeout  65;
 10     server {
 11         listen       80;
 12         server_name  localhost;
 13         location / {
 14             root   html;
 15             index  index.html index.htm;
 16         }
 17         location /aaa {
 18             stub_status on;
 19             access_log off;
 20         }
 21         error_page   500 502 503 504  /50x.html;
 22         location = /50x.html {
 23             root   html;
 24         }
 25     }
 26 }
#保存退出
[root@rzy ~]# nginx -s reload  #重载nginx,使修改的配置文件生效
[root@rzy ~]# curl 127.0.0.1/aaa #访问本地,指定刚才配置了状态监控的资源
Active connections: 1   #当前活跃连接数
server accepts handled requests
 3 3 3 #依次表示,总共接受的TCP连接次数,处理的TCP连接次数,总共的请求数量,当第一个和第二个不相同时,可以使用第二个减去第一个得到失败tcp连接次数
Reading: 0 Writing: 1 Waiting: 0 


(2)Nginx配置下载站点


  • 当我们访问一下软件的官网,比如Nginx的官网,里面可以看到很多目录并且资源包都是点一下就可以下载,Nginx的官网就是一个下载站点


- 注意:Nginx默认是不允许列出整个目录和下载的

- 可以配置的区域:http,server,location

******(1)继续上面的进度,修改配置文件
[root@rzy ~]# vim /usr/local/nginx/conf/nginx.conf
  1 worker_processes  1;
  2 events {
  3     worker_connections  1024;
  4 }
  5 http {
  6     include       mime.types;
  7     default_type  application/octet-stream;
  8     sendfile        on;
  9     keepalive_timeout  65;
 10     server {
 11         listen       80;
 12         server_name  localhost;
 13         location / {
 14             root   html;
 15             autoindex on;  #开启下载站点
 16         }
 17      }  
 28 }    
[root@rzy ~]# cd /usr/local/nginx/html/ 
[root@rzy html]# nginx -t #测试nginx语句是否正确
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@rzy html]# systemctl restart nginx  #重启服务
[root@rzy html]# rm -rf index.html    #删除原本的网页
[root@rzy html]# mkdir aaa   #创建两个目录,上传一个资源
[root@rzy html]# mkdir bbb
[root@rzy html]# mv /root/nginx-1.18.0.tar.gz .
[root@rzy html]# ll
总用量 1024
-rw-r--r-- 1 root root     494 4月  21 23:51 50x.html
drwxr-xr-x 2 root root       6 4月  22 02:22 aaa
drwxr-xr-x 2 root root       6 4月  22 02:22 bbb
-rw-r--r-- 1 root root       5 4月  22 00:23 index.html
-rw-r--r-- 1 root root 1039530 4月  21 23:48 nginx-1.18.0.tar.gz

使用浏览器访问:

2021050815155747.png

但是发现最右边的大小不容易换算,继续修改配置文件

 [root@rzy html]# vim /usr/local/nginx/conf/nginx.conf 
  1 worker_processes  1;
  2 events {
  3     worker_connections  1024;
  4 }
  5 http {
  6     include       mime.types;
  7     default_type  application/octet-stream;
  8     sendfile        on;
  9     keepalive_timeout  65;
 10     server {
 11         listen       80;
 12         server_name  localhost;
 13         location / {
 14             root   html;
 15             autoindex on;     #开启下载站点
 16             autoindex_exact_size off; #显示文件大小的格式,off可以显示单位,on开启直接以bytes比特为默认单位
 17         }
 18      }
 19 }
[root@rzy html]# nginx -t #测试nginx语句是否正确
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@rzy html]# systemctl restart nginx #重启nginx服务

再次使用浏览器访问


20210508151621255.png


(3)Nginx访问限制


  • 使用的模块:(ngxin默认已经安装)


limit_conn_module 这个模块可以限制访问Nginx的客户端在单一时间可以发起的TCP连接数量(单个客户端、单个ip地址)
limit_req_module 这个模块可以限制访问Nginx的客户端在单一时间可以发起的HTTP请求数量(单个客户端,单个ip地址)
  • http协议的连接和请求流程:


20210508151659535.png


  • HTTP协议的连接和请求


HTTP协议版本 连接关系
HTTP1.0 TCP连接不能多次使用
HTTP1.1 TCP可以按顺序多次使用
HTTP2.0 TCP可以多次使用,多路复用


-可以配置的区域:http,server,location

******(1)继续上面的进度,查看Nginx内置是否有这两个模块
[root@rzy ~]# cd /usr/src/nginx-1.18.0/
[root@rzy nginx-1.18.0]# ll
总用量 764
drwxr-xr-x 6 1001 1001    326 4月  21 23:50 auto
-rw-r--r-- 1 1001 1001 302863 4月  21 2020 CHANGES
-rw-r--r-- 1 1001 1001 462213 4月  21 2020 CHANGES.ru
drwxr-xr-x 2 1001 1001    168 4月  21 23:50 conf
-rwxr-xr-x 1 1001 1001   2502 4月  21 2020 configure
drwxr-xr-x 4 1001 1001     72 4月  21 23:50 contrib
drwxr-xr-x 2 1001 1001     40 4月  21 23:50 html
-rw-r--r-- 1 1001 1001   1397 4月  21 2020 LICENSE
-rw-r--r-- 1 root root    376 4月  21 23:51 Makefile
drwxr-xr-x 2 1001 1001     21 4月  21 23:50 man
drwxr-xr-x 3 root root    174 4月  21 23:51 objs
-rw-r--r-- 1 1001 1001     49 4月  21 2020 README
drwxr-xr-x 9 1001 1001     91 4月  21 23:50 src
[root@rzy nginx-1.18.0]# cat auto/options  | grep "HTTP_LIMIT_CONN=YES"  #查看是否有模块
HTTP_LIMIT_CONN=YES
[root@rzy nginx-1.18.0]# cat auto/options  | grep "HTTP_LIMIT_REQ=YES" #查看是否有模块
HTTP_LIMIT_REQ=YES
******(2)测试conn限制TCP连接的模块
[root@rzy nginx-1.18.0]# vim /usr/local/nginx/conf/nginx.conf #修改配置文件
  1 worker_processes  1;
  2 events {
  3     worker_connections  1024;
  4 }
  5 http {
  6     include       mime.types;
  7     default_type  application/octet-stream;
  8     sendfile        on;
  9     keepalive_timeout  65;
 10     limit_conn_zone $binary_remote_addr zone=conn_zone:10m; #同一时刻只允许一个客户端连接
 11     server {
 12         listen       80;
 13         server_name  localhost;
 14         location / {
 15             root   html;
 16             limit_conn conn_zone 1;  #统一时刻只允许一个客户端连接
 17         }
 18      }
 19 }
[root@rzy nginx-1.18.0]# yum -y install httpd-tools #安装ab压力测试工具
。。。。。。
完毕!
[root@rzy nginx-1.18.0]# cd
[root@rzy ~]# cd /usr/local/nginx/html/
[root@rzy html]# ll
总用量 1020
-rw-r--r-- 1 root root     494 4月  21 23:51 50x.html
drwxr-xr-x 2 root root       6 4月  22 02:22 aaa
drwxr-xr-x 2 root root       6 4月  22 02:22 bbb
-rw-r--r-- 1 root root 1039530 4月  21 23:48 nginx-1.18.0.tar.gz
[root@rzy html]# echo "1111" > index.html
[root@rzy html]# curl 127.0.0.1
1111
[root@rzy html]# systemctl restart nginx
#因为一次TCP可以进行多次请求,所以是测试不了的
******(3)测试req限制http请求的模块
[root@rzy html]# vim /usr/local/nginx/conf/nginx.conf
  1 worker_processes  1;
  2 events {
  3     worker_connections  1024;
  4 }
  5 http {
  6     include       mime.types;
  7     default_type  application/octet-stream;
  8     sendfile        on;
  9     keepalive_timeout  65;
 10     limit_req_zone $binary_remote_addr zone=req_zone:10m rate=30r/m; #限制速率,每秒最多30个请求
 11     server {
 12         listen       80;
 13         server_name  localhost;
 14         location / {
 15             root   html;
 16             limit_req zone=req_zone ; #指定req模块的区域为req_zone和上面的相同
 17         }
 18      }
 19 }
[root@rzy html]# systemctl restart nginx
[root@rzy html]# ab -n200 -c20 http://127.0.0.1/index.html  #进行测试
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 127.0.0.1 (be patient)
Completed 100 requests
Completed 200 requests
Finished 200 requests
Server Software:        nginx/1.18.0
Server Hostname:        127.0.0.1
Server Port:            80
Document Path:          /index.html
Document Length:        5 bytes
Concurrency Level:      20
Time taken for tests:   0.007 seconds
Complete requests:      200
Failed requests:        199   #失败199就表示测试成功了,因为每秒请求最多30个,因为所用时间就0.007秒,只成功了一个
   (Connect: 0, Receive: 0, Length: 199, Exceptions: 0)
Write errors:           0
Non-2xx responses:      199
Total transferred:      73665 bytes
HTML transferred:       39208 bytes
Requests per second:    29779.63 [#/sec] (mean)
Time per request:       0.672 [ms] (mean)
Time per request:       0.034 [ms] (mean, across all concurrent requests)
Transfer rate:          10711.51 [Kbytes/sec] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0       1
Processing:     0    0   0.1      0       1
Waiting:        0    0   0.1      0       0
Total:          0    1   0.1      1       1
Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      1
  80%      1
  90%      1
  95%      1
  98%      1
  99%      1
 100%      1 (longest request)


-连接限制和请求限制那个更加有效:


同一时刻只允许一个连接,和只允许一个请求,虽然都是一个,但是一个连接可以接受多个请求,而一个请求就是一个请求,所以说限制请求比限制连接更加有效


(4)Nginx访问控制


  • 使用的模块:


http_access_module 基于IP的访问控制
http_auth_basic_module 基于用户登录认证


  • 可以配置的区域:http.server,location,limit_except
******(1)继续上面的进度,修改配置文件(基于ip的)
[root@rzy ~]# vim /usr/local/nginx/conf/nginx.conf
  1 worker_processes  1;
  2 events {
  3     worker_connections  1024;
  4 }
  5 http {
  6     include       mime.types;
  7     default_type  application/octet-stream;
  8     sendfile        on;
  9     keepalive_timeout  65;
 10     server {
 11         listen       80;
 12         server_name  localhost;
 13         location / { 
 14             root   html;
 15             index index.html;
 16             deny 192.168.100.230;  #拒绝100.230地址
 17             allow all;             #允许所有
 18         }
 19      }
 20 }
[root@rzy ~]# systemctl restart nginx

使用100.230进行访问,发现报错403权限被拒绝,配置基于ip的访问控制成功


2021050815173766.png

基于客户端ip进行访问控制的局限性:


被拒绝的客户端可以使用代理进行访问,web服务器虽然可以找到代理服务器的ip地址,但是不知道是不是被拒绝的ip进行代理的。但是可以通过显示真实ip去显示真实的客户端ip

******(2)配置基于用户登录的访问控制,创建用户
[root@rzy ~]# htpasswd -c /usr/local/nginx/conf/auth_conf rzy
New password: 
Re-type new password: 
Adding password for user rzy
[root@rzy ~]# vim /usr/local/nginx/conf/nginx.conf
  1 worker_processes  1;
  2 events {
  3     worker_connections  1024;
  4 }
  5 http {
  6     include       mime.types;
  7     default_type  application/octet-stream;
  8     sendfile        on;
  9     keepalive_timeout  65;
 10     server {
 11         listen       80;
 12         server_name  localhost;
 13         location / {
 14             root   html;
 15             index index.html;
 16             auth_basic "请输入你的用户名和密码";
 17             auth_basic_user_file /usr/local/nginx/conf/auth_conf;
 18         }
 19      }
 20 }
[root@rzy ~]# systemctl restart nginx

使用浏览器再次进行访问,配置成功


20210508151759988.png

(5)Nginx虚拟主机


  • 和apache相同,有三种方式,即基于ip、基于端口、基于域名,因为域名使用的最多,所以只做基于域名的


******(1)每个server{}都是一个虚拟主机,所以只需要创建多个server就可以实现
[root@rzy ~]# vim /usr/local/nginx/conf/nginx.conf #修改配置文件
  1 worker_processes  1;
  2 events {
  3     worker_connections  1024;
  4 }
  5 http {
  6     include       mime.types;
  7     default_type  application/octet-stream;
  8     sendfile        on;
  9     keepalive_timeout  65;
 10     server {
 11         listen       80;
 12         server_name  www.aaa.com;  #不同域名
 13         location / {
 14             root   html/aaa;
 15             index index.html;
 16         }
 17      }
 18     server {
 19         listen       80;
 20         server_name  www.bbb.com;
 21         location / {
 22             root   html/bbb;
 23             index index.html;
 24         } 
 25      }
 26 }
[root@rzy ~]# cd /usr/local/nginx/html/
[root@rzy html]# mkdir aaa
[root@rzy html]# mkdir bbb
[root@rzy html]# echo "aaaaa" >aaa/index.html
[root@rzy html]# echo "bbbbb" >bbb/index.html
[root@rzy html]# systemctl restart nginx #重启服务


在本地写hosts文件

20210508151826745.png

进行访问


20210508151844806.png

配置完成!

相关实践学习
基于函数计算快速搭建Hexo博客系统
本场景介绍如何使用阿里云函数计算服务命令行工具快速搭建一个Hexo博客。
目录
相关文章
|
24天前
|
运维 前端开发 应用服务中间件
LNMP详解(八)——Nginx动静分离实战配置
LNMP详解(八)——Nginx动静分离实战配置
27 0
|
1月前
|
应用服务中间件 nginx
Nginx中如何配置中文域名?
Nginx中如何配置中文域名?
44 0
|
23天前
|
前端开发 应用服务中间件 nginx
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
Nginx配置详解Docker部署Nginx使用Nginx部署vue前端项目
94 0
|
1月前
|
PHP
百度虚拟机 bcloud_nginx_user.conf配置
百度虚拟机 bcloud_nginx_user.conf配置
22 0
|
3天前
|
安全 应用服务中间件 网络安全
SSL原理、生成SSL密钥对、Nginx配置SSL
现在,你的Nginx虚拟主机应该已经配置了SSL,可以通过HTTPS安全访问。确保在生产环境中使用有效的SSL证书来保护通信的安全性。
12 0
|
5天前
|
域名解析 缓存 负载均衡
Nginx正向代理域名的配置
Nginx正向代理域名的配置
|
6天前
|
前端开发 JavaScript 应用服务中间件
修改Jeecg-boot context-path(附加图片+Nginx配置)
修改Jeecg-boot context-path(附加图片+Nginx配置)
12 0
|
16天前
|
应用服务中间件 nginx
nginx进行反向代理的配置
在Nginx中设置反向代理的步骤:编辑`/etc/nginx/nginx.conf`,在http段加入配置,创建一个监听80端口、服务器名为example.com的虚拟主机。通过`location /`将请求代理到本地3000端口,并设置代理头。保存配置后,使用`sudo nginx -s reload`重载服务。完成配置,通过example.com访问代理服务器。
24 0
|
18天前
|
应用服务中间件 网络安全 nginx
nginx配置https访问
nginx配置https访问
29 0
|
26天前
|
应用服务中间件 nginx
nginx配置访问qicaitun.com强制跳转www.qicaitun.com
nginx配置访问qicaitun.com强制跳转www.qicaitun.com
9 0