Nginx相关介绍

1.wbe介绍

目录

1.wbe介绍

2.nginx的安装配置

1,nginx的安装(在Linux上安装)

3,nginx的配置

4,nginx配置项的作用

3.http协议简介 

4.nginx配置多站点

1,多端口配置多站点

2,多IP配置多站点 

5.include配置文件

 6.nginx的日志记录

7.nginx开启basic认证

 8.nginx开启目录浏览功能

9. nginx控制访问

10.nginx的常用变量


web全称:World Wide Web,全球广域网,又称万维网。

web项目架构:

1.C/S架构(Client-Server),即客户端服务端架构。例如:淘宝,微信等各种手机软件。服务端负责数据的管理,客户端负责数据的展示。

2.B/S架构(Browser-Server),及浏览器服务端架构,其实这也是C/S架构的一种。例如:百度网页,淘宝网页等。这种架构的网站非常多,因为开发一个软件的成本非常高,而且用户要安装哼多软件在手机或电脑上这也非常不方便。所以,这样我们只需要有浏览器,通过访问网站来实现我们想要的服务。

2.nginx的安装配置

nginx,是一款web服务应用软件,即webserver。类似的还有:apach,tomcat,microsoft iis。

apach出来的比较早,在当时是市面上最主流的webserver,nginx出来的比较晚,是由Igor Sysoev创建,但是现在nginx可以说是现在最常用的webserver。

1,nginx的安装(在Linux上安装)

#首先要更新一下yum安装源。

[root@192 /]# netstat lntup
#安装nginx

[root@192 /]# yum install nginx -y
#启动nginx服务

[root@192 /]# systemctl start nginx
#设置开机自启动

[root@192 /]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

#先安装netstat:它可以监测主机端口开放情况。

[root@192 bin]# yum install net-tools -y
#查看nginx占用端口

[root@192 bin]# netstat -lntup
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:80              0.0.0.0:*               LISTEN      1787/nginx: master  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1422/sshd           
tcp6       0      0 :::80                   :::*                    LISTEN      1787/nginx: master  
tcp6       0      0 :::22                   :::*                    LISTEN      1422/sshd           
udp        0      0 0.0.0.0:68              0.0.0.0:*                           1219/dhclient       
udp        0      0 127.0.0.1:323           0.0.0.0:*                           583/chronyd         
udp6       0      0 ::1:323                 :::*                                583/chronyd         

#可以看到nginx占用了80端口

#关闭主机的防火墙,并且是指默认关闭

[root@192 bin]# systemctl stop firewalld
[root@192 bin]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

 

#这时可以通过主机IP来访问nginx部署的网站,默认是80端口。

访问nginx时看到的网页文件放在了 /usr/share/nginx/html/

[root@192 nginx]# cd /usr/share/nginx/html/
[root@192 html]# ls
404.html  50x.html  en-US  icons  img  index.html  nginx-logo.png  poweredby.png

#我们看到的页面是index.html

3,nginx的配置

#nginx的配置文件路径:/etc/nginx/

[root@192 ~]# ls /etc/nginx
conf.d     fastcgi.conf          fastcgi_params          koi-utf  mime.types          nginx.conf          scgi_params          uwsgi_params          win-utf
default.d  fastcgi.conf.default  fastcgi_params.default  koi-win  mime.types.default  nginx.conf.default  scgi_params.default  uwsgi_params.default

 

#nginx.conf是nginx的主配置文件, nginx.conf.default是nginx.conf的备份文件。(nginx的所有配置文件都有备份)

#更改配置主文件

[root@192 nginx]# grep -Ev '#|^$' nginx.conf.default >nginx.conf   #删除文件中的注释与空行。

[root@192 nginx]# cat nginx.conf -n
     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            error_page   500 502 503 504  /50x.html;
    18            location = /50x.html {
    19                root   html;
    20            }
    21        }
    22    }

#先把17-20行删掉,还有8,9行。删完之后就是nginx的最小配置。

  1 worker_processes  1;
  2 events {
  3     worker_connections  1024;
  4 }
  5 http {
  6     include       mime.types;
  7     default_type  application/octet-stream;
  8     server {
  9         listen       80;
 10         server_name  localhost;
 11         location / { 
 12             root   html;
 13             index  index.html index.htm;
 14         }   
 15     }   
 16 }   

#修改完之后要重启nginx,配置才会生效。

[root@192 nginx]# systemctl restart nginx

4,nginx配置项的作用

nginx配置项总的来说有3大部分。

1.全局部分:

worker_processes:nginx的工作进程。

2.evevts部分:

worker_connections:一个工作进程能处理的请求最大值(1024),也就是说一个工作进程同时可以处理1024个人对该网站发送的不同请求。

3.http,server部分:

3.http中的include:nginx能够识别出来的文件类型,对应/etc/nginx/下的mime.type文件,include可以加载文件。

4.http中的default_type:nginx识别不出来的文件类型,会以application/octet-stream形式发给浏览器。

5.server对应的是网站的相关配置,listen是访问的端口号,server_name是主机名即域名,如果注册了域名就可以改。location部分:location /是网站根目录,root表示站点根目录,就是html(/usr/share/nginx/html/),index  index.html index.htm:在你访问改网站时,没有指定文件,nginx会默认找站点根目录下的这些文件,如果没有这些文件就会报403。(一个server可以有多个server)

3.http协议简介 

http全称是Hyper Text Transfer Protocol(超文本传输协议),主要传输html文件,html就是超文本标记语言。http协议就是参与网络通信双方都必须要遵守的规定。

http工作过程:

一次请求可以有多个响应,多次请求可能也只有一个响应。简单来说http协议就是规范了请求与响应的加工处理格式。

4.nginx配置多站点

我们访问网站其实就是访问了某台主机中的资源,一台主机可以有多个端口,多个IP,这样就可以部署多个网站。

1,多端口配置多站点

nginx.conf中的server是对应网站的配置,要配置多个网站,就要有多个server。

有俩个server,就可以部署两个网站。这两个server中的端口不能一样,站点根目录也不能一样。

可以重新创建一个站点根目录,在里面创建一些文件。

[root@192 html]# cd /
[root@192 /]# mkdir web
[root@192 /]# cd web
[root@192 web]# ls
index.html
[root@192 web]# 

#这里创建了一个站点根目录,创建了一个文件index.html。

现在把server的配置改掉。

#保存退出,在重启nginx。

[root@192 nginx]# systemctl restart nginx 

#首先进入到 /etc/selinux目录,找到config这个配置文件 ,将SELinux=enforcing改成SELinux=disable。selinux是安全增强型的Linux,它是Linux内核的一个模块,也是Linux的一个安全子系统。selinux的主要作用就是最大限度的减小系统中服务进程可访问的资源。它会限制哼多功能,所以要将它关闭,否则不了访问主机的81端口,会报403。

#可以看到通过同一ip的不同端口访问到了不同的网站。 

2,多IP配置多站点 

一台主机可以有多个端口也可以有多个IP地址。不同IP地址可以配置不同的网站。所以一台服务器上也可以跑多个网站。

Linux配置多个IP。

#进入/etc/sysconfig/network-scripts/

[root@192 ~]# cd /etc/sysconfig/network-scripts/
[root@192 network-scripts]# ls
ifcfg-ens33  ifdown-bnep  ifdown-ipv6  ifdown-ppp     ifdown-Team      ifup          ifup-eth   ifup-isdn   ifup-post    ifup-sit       ifup-tunnel       network-functions
ifcfg-lo     ifdown-eth   ifdown-isdn  ifdown-routes  ifdown-TeamPort  ifup-aliases  ifup-ippp  ifup-plip   ifup-ppp     ifup-Team      ifup-wireless     network-functions-ipv6
ifdown       ifdown-ippp  ifdown-post  ifdown-sit     ifdown-tunnel    ifup-bnep     ifup-ipv6  ifup-plusb  ifup-routes  ifup-TeamPort  init.ipv6-global
[root@192 network-scripts]# 

#编辑ifcfg-ens33这个文件

TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="dhcp"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="848966a7-bc3b-4f72-bb3e-56bebcf3fb5d"
DEVICE="ens33"
ONBOOT="yes"

#把BOOTPROTO="dhcp"改成BOOTPROTO="statice"

#添加多个IP:IPADDR1=192.168.93.153  IPADDR2=192.168.93.154

#配置子网掩码:NETMASK=255.255.255.0

#配置网关:GATEWAY=192.168.0.1

#配置DNS:DNS=8.8.8.8

#修nginx的配置文件


#把之前81端口改成80,在指定IP地址就可以。在重启nginx

[root@192 nginx]# systemctl restart nginx

5.include配置文件

一台服务器配置多个网站种子配置文件中就要有多个server,这要很不方便来管理。把每个server单独拿出来,放入不同的文件中,每个文件配置一个网站跟方便管理。在nginx配置文件中有个文件专门用来放server,是conf.d。

#把nginx.conf中的server复制一份,再删除掉。在conf.d中根据不同的网站来创建文件来放server。在配置中写include来加载配置文件。例如: include       /etc/nginx/conf.d/*.conf;

#加载所有以conf.d结尾的文件。

[root@192 nginx]# cd conf.d/
[root@192 conf.d]# touch one.conf  two.conf
 
#再把server放入对应的文件中。

[root@192 conf.d]# cat one.conf 
server {
        listen       192.168.93.153:80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
    }

#这样就可以。在重启nginx。

#也可以在其中一个service中的端口号后加 default_server表示默认网站,当访问该主机找不到相应的网站就会默认访问该网站。

 6.nginx的日志记录

/var/log/secure #这里存放了系统的日志记录,登陆系统就会有记录。

web服务程序也一样,也有日志记录,会把每次请求的信息记录下来。nginx的默认日志记录在/var/log/nginx/下。

[root@192 ~]# cd /var/log/nginx/
[root@192 nginx]# ls
access.log  access.log-20240221  error.log  error.log-20240221

#可以看到有访问成功的日志与访问失败的日志。当天的日志会单独记录。

#这台主机部署了两个网站,想要把他们的日志记录分开就要修改配置文件。

#先创建一个目录来存放日志: 

[root@192 /]# mkdir log
[root@192 /]# cd log/
[root@192 log]# touch one.log two.log
[root@192 log]# ls
one.log  two.log
[root@192 log]# 

[root@192 /]# ll log
总用量 8
-rw-r--r-- 1 root root 439 2月  21 23:40 one.log
-rw-r--r-- 1 root root 990 2月  21 23:40 two.log

#可以开到这两个文件是从属与root用户,而nginx的启动用户是nginx,所以要把该文件权限给nginx

[root@192 /]# chown nginx:nginx /log/one.log 
[root@192 /]# chown nginx:nginx /log/two.log 
[root@192 /]# ll /log
总用量 8
-rw-r--r-- 1 nginx nginx 439 2月  21 23:40 one.log
-rw-r--r-- 1 nginx nginx 990 2月  21 23:40 two.log

#修改配置文件,在各自的网站配置文件中加上access_log这个参数,在指定日志存放路径。

例如:

[root@192 conf.d]# cat one.conf 
server {
        listen       192.168.93.153:80;
        server_name  localhost;
        access_log /log/one.log;
        location / {
            root   html;
            index  index.html index.htm;
        }
    }

[root@192 conf.d]# cat two.conf 
server {
        listen       192.168.93.154:80;
        server_name  localhost;
        access_log   /log/two.log;
        location / {
            root   /web;
            index  index.html index.htm;
        }
    } 

#在重启nginx。 

root@192 conf.d]# cd /log
[root@192 log]# cat one.log 
192.168.93.1 - - [21/Feb/2024:23:40:29 +0800] "GET / HTTP/1.1" 200 4833 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0"
192.168.93.1 - - [21/Feb/2024:23:40:29 +0800] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.93.153/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0"

#这样就把日志分开记录。

7.nginx开启basic认证

Basic认证是一种HTTP协议的身份验证方式,它通过在请求头中添加用户名和密码的Base64编码来进行身份验证。具体步骤如下:

  1. 客户端发送HTTP请求时,在请求头中添加Authorization字段,并将用户名和密码进行Base64编码。
  2. 服务器接收到请求后,解码Authorization字段,并验证用户名和密码是否正确。
  3. 如果验证成功,服务器返回HTTP状态码200,表示认证通过;如果验证失败,服务器返回HTTP状态码401,表示认证失败。

Basic认证的优点是简单易实现,适用于简单的身份验证场景。然而,它的缺点是不安全,因为用户名和密码是以明文形式传输的,容易被窃取。因此,在使用Basic认证时,建议使用HTTPS协议来加密通信,增加安全性。

#首先生成一个账号密码文件,很多在线网站多可以生成。

#把生成的结果放到指定文件中。

[root@192 /]# vim /etc/nginx/passwd
[root@192 /]# cat /etc/nginx/passwd
Jerry:xa2aXpZb3n8cU

 #再修改配置文件:

#auth_basic  是备注信息

#auth_basic_user_file  是存放密码文件的路径

#最后重启nginx

 访问该网站,会显示要身份验证。

 8.nginx开启目录浏览功能

#autoindex on   开启目录浏览功能,要开启目录浏览功能,站点根目录下不能有index.html。

#autoindex_exact_size   off    #这个配置可以显示文件大小

#重启nginx服务。

#这样就开启了目录浏览功能。

#nginx也可以在特定的目录下开启basic认证。上面说过一个server可以写多个location。

9. nginx控制访问

Nginx是一个高性能的开源Web服务器和反向代理服务器。它可以通过配置文件来控制访问。

IP地址过滤:可以使用allowdeny指令来限制特定IP地址或IP地址段的访问。例如,可以使用allow 192.168.1.0/24;来允许192.168.1.0/24网段的IP地址访问。

#deny 后面我跟的是net模式的网关 。

#allow  0.0.0.0  表示允许所有网段来访问。

#被禁止访问后会显示403。 

10.nginx的常用变量

nginx常用变量表是指在nginx配置文件中可以使用的一些预定义变量。这些变量提供了一些有用的信息,可以在配置文件中使用它们来实现一些功能或者进行一些判断。

以下是nginx常用变量表的一些示例:

  1. $args:请求中的参数部分,例如:http://example.com/index.html?a=1&b=2,$args的值为a=1&b=2。
  2. $uri:请求的URI部分,例如:http://example.com/index.html,$uri的值为/index.html。
  3. $request_uri:完整的请求URI,包括参数部分,例如:http://example.com/index.html?a=1&b=2,$request_uri的值为/index.html?a=1&b=2。
  4. $request_method:请求的方法,例如:GET、POST等。
  5. $http_user_agent:客户端的User-Agent信息。
  6. $remote_addr:客户端的IP地址。
  7. $server_name:当前请求的服务器名。
  8. $host:请求中的Host头部信息。

这些变量可以在nginx配置文件中使用,例如:

location / { if ($args = "a=1") { return 200 "Hello, World!"; } }

上述配置中,如果请求的参数为"a=1",则返回"Hello, World!"。

  • 31
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值