Nginx基础配置

一、Nginx基础配置

(1)配置文件

[root@7-10 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@7-10 ~]# yum -y install pcre-devel zlib-devel
[root@7-10 ~]#  tar xf nginx-1.12.0.tar.gz  -C /usr/src/
[root@7-10 ~]# cd /usr/src/nginx-1.12.0/
[root@7-10 nginx-1.12.0]# useradd -M -s /sbin/nologin  nginx
[root@7-10 nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install
[root@7-10 nginx-1.12.0]# cd /usr/local/nginx/conf/
[root@7-10 conf]# cp nginx.conf nginx.conf.bak
[root@7-10 conf]# ll | grep nginx.conf
-rw-r--r-- 1 root root 2656 5月  27 15:03 nginx.conf
-rw-r--r-- 1 root root 2656 5月  27 15:04 nginx.conf.bak
-rw-r--r-- 1 root root 2656 5月  27 15:03 nginx.conf.default
[root@7-10 conf]# sed -i '/#/d' nginx.conf
[root@7-10 conf]# sed -i '/^$/d' nginx.conf
[root@7-10 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 }

(2)Nginx日志配置

[root@7-10 conf]# ln -s /usr/local/nginx/sbin/nginx  /usr/local/sbin/  #优化nginx命令执行路径
[root@7-10 conf]# 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@7-10 conf]# systemctl start nginx
[root@7-10 conf]# netstat -anpt | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      43314/nginx: master 
[root@7-10 conf]# echo "aaaa" > /usr/local/nginx/html/index.html          #编写页面
[root@7-10 conf]# curl 192.168.100.10              #访问本地
aaaa

(2)使用curl命令查看http的请求和返回

[root@7-10 conf]# curl -v http://192.168.100.10  #  >就是请求报文,<就是返回报文
* About to connect() to 192.168.100.10 port 80 (#0)
*   Trying 192.168.100.10...
* Connected to 192.168.100.10 (192.168.100.10) port 80 (#0)
> GET / HTTP/1.1        #依次是请求方式GET, 请求资源 /  ,请求使用的协议 HTTP/1.1
> User-Agent: curl/7.29.0     #请求的方式,这里一般是浏览器的版本,curl就是我们刚才使用的curl工具
> Host: 192.168.100.10       #访问的目的ip
> Accept: */*
> 
< HTTP/1.1 200 OK        #依次是返回使用协议HTTP/1.1 ,状态码200 ok
< Server: nginx/1.12.0    #返回的服务器的web版本
< Date: Thu, 27 May 2021 07:25:27 GMT    #返回报文的时间
< Content-Type: text/html     #返回的数据类型
< Content-Length: 5    #返回数据的大小(字节数)
< Last-Modified: Thu, 27 May 2021 07:17:08 GMT      #最近一次服务器修改配置文件的日期
< Connection: keep-alive        #连接模式,keep-alive长连接
< ETag: "60af4774-5"
< Accept-Ranges: bytes
< 
aaaa       #具体的数据
* Connection #0 to host 192.168.100.10 left intact

(3)在主配置文件中配置日志的区域,因为在写配置文件的时候,把#号开头的注释行和空行删除了,所以需要看一下之前备份的配置文件

[root@7-10 conf]# 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 
。。。。。。

$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@7-10 ~]# 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@7-10 ~]# nginx -s reload  #重载nginx,使修改的配置文件生效
[root@7-10 ~]#  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@7-10 ~]# 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;  #开启下载站点
                autoindex_exact_size off; #显示文件大小的格式,off可以显示单位,on开启直接以bytes比特为默认单位
 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@7-10 html]# systemctl restart nginx  #重启服务
[root@7-10 html]# rm -rf index.html    #删除原本的网页
[root@7-10 html]# mkdir aaa   #创建两个目录,上传一个资源
[root@7-10 html]# mkdir bbb
[root@7-10 html]# mv /root/nginx-1.18.0.tar.gz .
[root@7-10 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.12.0.tar.gz

再次使用浏览器访问
在这里插入图片描述

(3)Nginx访问限制

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

    (1)客户端向服务端发起TCP连接
    (2)成功建立TCP连接后客户端向服务端发送HTTP请求
    (3)服务器收到请求向客户端进行回应
    (4) 请求完成后,客户端和服务器断开TCP连接

在这里插入图片描述

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

[root@7-10 nginx-1.12.0]# cat auto/options  | grep "HTTP_LIMIT_CONN=YES"  #查看是否有模块
HTTP_LIMIT_CONN=YES
[root@7-10 nginx-1.12.0]# cat auto/options  | grep "HTTP_LIMIT_REQ=YES" #查看是否有模块
HTTP_LIMIT_REQ=YES

******(2)测试conn限制TCP连接的模块
[root@7-10 nginx-1.12.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.12.0]# yum -y install httpd-tools #安装ab压力测试工具
。。。。。。
完毕!
[root@7-10 nginx-1.12.0]# cd
[root@7-10 ~]# cd /usr/local/nginx/html/
[root@7-100 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.12.0.tar.gz
[root@7-10 html]# echo "1111" > index.html
[root@7-10 html]# curl 192.168.100.10
1111
[root@7-10 html]# systemctl restart nginx
#因为一次TCP可以进行多次请求,所以是测试不了的

******(3)测试req限制http请求的模块
[root@7-10 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://192.168.100.10/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 192.168.100.10 (be patient)
Completed 100 requests
Completed 200 requests
Finished 200 requests


Server Software:        nginx/1.12.0
Server Hostname:        192.168.100.10
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@7-10 ~]# 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@7-10 ~]# systemctl restart nginx

使用100.230进行访问,发现报错403权限被拒绝,配置基于ip的访问控制成功
在这里插入图片描述
但是基于客户端ip进行访问控制有很大的局限性,比如你拒绝了他的ip,但是他还可以找个代理服务器就可以继续访问了,所以局限性很大

(2)配置基于用户登录的访问控制,创建用户
[root@7-10 ~]# htpasswd -c /usr/local/nginx/conf/auth_conf rzy
New password: 
Re-type new password: 
Adding password for user rzy
[root@7-10 ~]# 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@7-10 ~]# systemctl restart nginx

在这里插入图片描述

(5)Nginx虚拟主机

  • 和apache相同,有三种方式,即基于ip、基于端口、基于域名,因为域名使用的最多,所以只做基于域名的
(1)每个server{}都是一个虚拟主机,所以只需要创建多个server就可以实现
[root@7-10 ~]# 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@7-10 ~]# cd /usr/local/nginx/html/
[root@7-10 html]# mkdir aaa
[root@7-10 html]# mkdir bbb
[root@7-10 html]# echo "aaaaa" >aaa/index.html
[root@7-10 html]# echo "bbbbb" >bbb/index.html
[root@7-10 html]# systemctl restart nginx #重启服务

开启一台win7指以下DNS
在这里插入图片描述

配置完成

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
nginx基础配置可以在其配置文件nginx.conf中找到。配置文件的路径通常是/nginx/conf/nginx.conf。 在nginx.conf文件中,可以设置许多选项来配置nginx的行为。其中一些基本的配置选项包括: - http块:在配置文件中,http块定义了全局的http配置,包括一些常用的配置项,如mime.types文件的引入、代理配置文件的引入、fastcgi配置文件的引入以及默认的索引文件等。 - server块:在http块中,可以有多个server块,每个server块定义了一个虚拟主机的配置信息。可以在每个server块中指定域名或IP地址,以及监听的端口号等。 - location块:在server块中,可以有多个location块,每个location块定义了一组匹配规则和对应的处理方式。可以通过location块来指定请求的URL匹配规则,并根据规则配置相应的处理方式,如代理请求到其他服务器、处理静态文件等。 此外,nginx还支持在配置文件中引入其他文件,以便更好地组织和管理配置。例如,可以在nginx.conf中使用include指令来引入其他配置文件,如mime.types、proxy.conf和fastcgi.conf。 总结起来,nginx基础配置可以在nginx.conf文件中找到,其中包括http块、server块和location块等配置项。可以使用include指令来引入其他配置文件。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Nginx的基本配置](https://blog.csdn.net/weixin_46007090/article/details/120105907)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Nginx基础配置](https://blog.csdn.net/weixin_41968982/article/details/123687834)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值