Web 基础——Nginx(二)

24 篇文章 0 订阅

Web 基础——Nginx(二)

一、Nginx 基础配置

1.Nginx 配置文件

  • Nginx 主配置文件是一个纯文本类型的文件,整个配置文件是以区块的形式组成的。一般每个区块以一对 {} 大括号。

    // 全局配置:
    user # 配置 Nginx 服务的系统使??户
    worker_processes # ?作进程. 配置和 CPU 个数保持?致
    error_log # 错误?志. 后?接?的是路径
    pid # Nginx 服务启动时的 PID
    // Events 事件模块:
    events { # 事件模块
    worker_connections # 每个 Worker 进程?持的最?连接数
    use # 内核模型. select | poll | epoll
    // HTTP 配置:
    http {

    server { # 第一个虚拟主机
    listen 80; # 监听端?. 默认 80
    server_name localhost; # 提供服务的域名或主机名
    ‘location’ / { # 控制?站访问路径
    root /usr/share/nginx/html; # 存放?站路径
    index index.html index.htm; # 默认访问首页?件
    }
    error_page 500 502 503 504 /50x.html; # 指定错误代码. 统?定义错误??. 错误代码重定向到新的 Locaiton
    ‘location’ = /50x.html {
    root html;
    }
    }

    server { # 第?个虚拟主机配置

    }

2.Nginx 日志配置

  • 开启 Nginx 日志配置只需要将配置文件中以下内容将 # 去掉即可。

    [root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
    13 #log_format main '$remote_addr - r e m o t e u s e r [ remote_user [ remoteuser[time_local] “KaTeX parse error: Expected 'EOF', got '#' at position 19: …uest" ' 14 #̲ …status b o d y b y t e s s e n t " body_bytes_sent " bodybytessent"http_referer” ’
    15 # ‘“ h t t p u s e r a g e n t " " http_user_agent" " httpuseragent""http_x_forwarded_for”’;
    16 #access_log logs/access.log main;

Nginx 日志变量:

  • $remote_addr:表示客户端地址。
  • $remote_userhttp 客户端请求 Nginx 认证用户名。
  • $time_local:Nginx 的本地时间。
  • $request:Request 请求行,GET 等方法、http 协议版本。
  • $statusrespose 返回的状态码。
  • $body_bytes_sent:从服务端响应给客户端 body 信息大小。
  • $http_refererhttp 上一级页面,防盗链、用户行为分析。
  • $http_user_agenthttp 头部信息,客户端访问设备。
  • $http_x_forwarded_forhttp 请求携带的 http

3.Nginx 下载站点

  • Nginx 默认是不允许列出整个目录浏览下载。

    语法格式:autoindex on | off 可在 http | server | location 区域中添加

配置目录浏览功能

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
    root   html;
    index  index.html index.htm;
    autoindex on;												# 开启目录浏览
    autoindex_exact_size off;									# 修改为 off 即可显示出文件的大概大小. 单位是 KB | MB | GB
[root@localhost ~]# systemctl restart nginx						# 重启 Nginx 服务
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# ls
index.html
[root@localhost html]# rm -rf index.html						# 删除默认网页
[root@localhost html]# mkdir zhangsan
[root@localhost html]# mv /root/nginx-1.18.0.tar.gz .
[root@localhost html]# ls
nginx-1.18.0.tar.gz  zhangsan

验证:
在这里插入图片描述

4.Nginx 访问控制

1)第一种方式

  • limit_conn_module:限制 Nginx 服务器所承载的单个客户端单个 IP 地址在单一时间所发起的连接数量(防爬虫)
  • limit_req_module:限制 Nginx 服务器所承载的单个客户端单个 IP 地址在单一时间所发起的请求数量。

查看 Nginx 默认安装的模块,在解压 Nginx 源代码目录 下运行以下命令

[root@localhost ~]# cd /usr/src/nginx-1.18.0/
[root@localhost nginx-1.18.0]# cat auto/options | grep 'YES'				# 查看 Nginx 默认安装的所有模块

查看是否有 limit_connlimit_req 这两个模块

[root@localhost nginx-1.18.0]# cat auto/options | grep 'HTTP_LIMIT_CONN=YES'
HTTP_LIMIT_CONN=YES
[root@localhost nginx-1.18.0]# cat auto/options | grep 'HTTP_LIMIT_REQ=YES'
HTTP_LIMIT_REQ=YES

在这里插入图片描述
配置 Nginx 连接限制

语法格式:
limit_conn_zone key zone=name:size							# 需要在 http 区域中配置
limit_conn zone number										# 可以在 http | server | location 区域中配置


[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
http {
    ...
    limit_conn_zone $binary_remote_addr zone=conn_zone:10m;
    server {
        ...
        location / {
            ...
            limit_conn conn_zone 1;							# 同一时刻只允许一个客户端 IP 地址
[root@localhost ~]# systemctl restart nginx

验证:

[root@localhost ~]# yum -y install httpd-tools				# 安装 AB 压力测试工具
[root@localhost ~]# echo "<h1>Hello</h1>" > /usr/local/nginx/html/index.html		# 编写测试页面
[root@localhost ~]# ab -n 200 -c 20 http://127.0.0.1/

在这里插入图片描述
配置 Nginx 请求限制

语法格式:
limit_req_zone key zone=name:size rate=rate						# 需要在 http 区域中配置
limit_req zone number [burst=number] [nodelay]					# 可以在 http | server | location 区域中配置


[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
http {
    ...
    limit_req_zone $binary_remote_addr zone=req_zone:10m rate=30r/m;		# rate 限制速率. 限制一秒钟最多一个 IP 请求
    server {
        ...
        location / {
            ...
            limit_req zone=req_zone;
[root@localhost ~]# systemctl restart nginx

验证:

[root@localhost ~]# ab -n 200 -c 20 http://127.0.0.1/

在这里插入图片描述

2)第二种方式

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

配置基于 IP 的访问控制

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
    ...
    deny 192.168.1.250;										# 拒绝单个IP. 可以在 http | server | location 区域中配置
    allow all;												# 允许所有
[root@localhost ~]# systemctl restart nginx					# 重启 Ngix 服务

验证:
在这里插入图片描述
基于用户登录认证

[root@localhost ~]# htpasswd -c /usr/local/nginx/conf/auth_conf zhangsan	# 创建测试用户
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
    ...
    auth_basic "Auth access Blog Input your Passwd ~";						# 提示语. 可以在 在 http | server | location 区域中配置
    auth_basic_user_file /usr/local/nginx/conf/auth_conf;					# 限制用户文件. 可以在 http | server | location 区域配置
[root@localhost ~]# systemctl restart nginx									# 重启 Ngix 服务

验证:
在这里插入图片描述
在这里插入图片描述

5.Nginx 虚拟主机

所谓虚拟主机,就是在 Web 服务器里是一个独立的网站站点,这个站点对应独立的域名(也有可能是 IP 或端口),具有独立的程序及资源目录,可以独立地对外提供服务供用户访问。

1)创建 Web 站点目录

[root@localhost ~]# mkdir /usr/local/nginx/html/Coco/
[root@localhost ~]# mkdir /usr/local/nginx/html/Zozo/
[root@localhost ~]# echo "Hello Coco" > /usr/local/nginx/html/Coco/index.html
[root@localhost ~]# echo "Hello Zozo" > /usr/local/nginx/html/Zozo/index.html

在这里插入图片描述
2)配置基于域名的虚拟主机

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
http {
    ...
    server {
        listen       80; 
        server_name  www.Coco.com;
        location / { 
            root   html/Coco;
            index  index.html index.htm;
        }   
    }   
    server {
        listen       80; 
        server_name  www.Zozo.com;
        location / { 
            root   html/Zozo;
            index  index.html index.htm;
        }   
    }   
}
[root@localhost ~]# systemctl restart nginx							#重启 Nginx 服务
[root@localhost ~]# cat <<END >> /etc/hosts							#配置 Hosts 文件解析
192.168.1.1 www.Coco.com
192.168.1.1 www.Zozo.com
END

验证:

[root@localhost ~]# curl http://www.Coco.com
[root@localhost ~]# curl http://www.Zozo.com

在这里插入图片描述
配置虚拟主机别名

  • 所谓虚拟主机别名,就是虚拟主机设置除了主域名以外的一个域名,实现用户访问的多个域名对应同一个。

    [root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
    http {

    server {
    listen 80;
    server_name www.Coco.com Coco.com;
    location / {
    root html/Coco;
    index index.html index.htm;
    }
    }
    server {
    listen 80;
    server_name www.Zozo.com;
    location / {
    root html/Zozo;
    index index.html index.htm;
    }
    }
    }
    [root@localhost ~]# cat <> /etc/hosts
    192.168.1.1 Coco.com
    END
    [root@localhost ~]# curl http://Coco.com
    [root@localhost ~]# curl http://www.Coco.com

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Nginx是一款高性能的Web服务器软件,它支持SSL/TLS协议,可以为Web应用程序提供安全的访问。本文将介绍如何在Nginx上开启SSL模块,并配置SSL证书,使得Web应用程序支持HTTPS访问。 1. 安装SSL模块 在编译Nginx的时候,需要开启SSL模块。可以在编译选项中加入--with-http_ssl_module参数来开启SSL模块。如果使用的是预编译的进制包,可以通过查看nginx.conf文件来确定是否开启了SSL模块。如果存在以下配置项,则说明SSL模块已经开启。 ``` listen 443 ssl; ssl_certificate /path/to/cert.pem; ssl_certificate_key /path/to/key.pem; ``` 2. 配置SSL证书 SSL证书用于加密网站和客户端之间的通信,防止敏感信息在传输过程中被窃取。可以购买SSL证书,也可以使用免费的证书,例如Let's Encrypt。 在配置SSL证书之前,需要先生成证书和私钥。可以使用如下命令生成自签名证书和私钥。 ``` openssl req -newkey rsa:2048 -nodes -keyout server.key -x509 -days 365 -out server.crt ``` 生成的server.key和server.crt文件分别是私钥和证书。接下来,需要将证书和私钥拷贝到Nginx配置文件所在的目录。 ``` cp server.crt /etc/nginx/ cp server.key /etc/nginx/ ``` 然后,在Nginx配置文件中添加以下配置项,指定证书和私钥的路径。 ``` ssl_certificate /etc/nginx/server.crt; ssl_certificate_key /etc/nginx/server.key; ``` 3. 配置SSL协议和加密算法 在Nginx配置文件中,可以配置SSL协议和加密算法。可以使用如下配置项指定SSL协议。 ``` ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ``` 可以使用如下配置项指定加密算法。 ``` ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ``` 4. 配置HTTPS监听端口 在Nginx配置文件中,可以使用如下配置项开启HTTPS监听端口。 ``` server { listen 443 ssl; server_name example.com; ssl_certificate /etc/nginx/server.crt; ssl_certificate_key /etc/nginx/server.key; location / { # 配置Web应用程序的根目录 root /var/www/html; index index.html; } } ``` 在以上配置中,listen 443 ssl指定了HTTPS监听端口,并且ssl_certificate和ssl_certificate_key指定了SSL证书和私钥的路径。location /用于配置Web应用程序的根目录。 5. 重启Nginx服务 完成以上配置后,需要重启Nginx服务,使得配置生效。 ``` service nginx restart ``` 通过以上步骤,就可以在Nginx上开启SSL模块,并配置SSL证书,使得Web应用程序支持HTTPS访问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值