Nginx-服务介绍

yum安装

a 使用官方yum源进行安装 安装的是最新版本 软件目录结构比较标准(推荐)
b 使用非官方yum源进行安装 安装的不是最新版本 目录结构会发生变化

编译安装

1、

wget http://nginx.org/download/nginx-1.18.0.tar.gz
PS: 解决软件的依赖    openssl-devel pcre-devel

2、

解压下载好的软件,并进到目录中

3、

a 进行配置操作

./configure
–prefix=PATH               set installation prefix    
                           指定程序安装路径

–user=USER                 set non-privileged user for worker processes
                           设置一个虚拟用户管理worker进程(安全)

–group=GROUP               set non-privileged group for worker processes
                           设置一个虚拟用户组管理worker进程(安全)

b 进行软件的编译过程:

make 编译

c 编译安装过程

make install

yum官方安装方法

1、

vim /etc/yum.repos.d/nginx.repos
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

2、yum安装nginx

yum -y install nginx

3、启动nginx服务,检查服务是否安装正确

systemctl start nginx
systemctl enable nginx

查看软件的目录结构

重要文件:

01:/etc/nginx               配置文件
02: /var/log/nginx           日志文件
03:/usr/bin/nginx           命令文件
04:/usr/share/nginx/html    站点目录
            图片 附件信息 音频 视频

nginx服务配置文件

/etc/nginx/nginx.conf        #主配置文件

第一个部分:配置文件珠区域配置

user  nginx;                    #定义worker进程管理的用户
补充:nginx的进程
master process:    主进程        #管理服务是否能够正常运行    boss
worker process:    工作进程      #处理用户的访问请求          员工
worker_processes  1;            #定义有几个worker进程 == CPU核数
error_log  /var/log/nginx/error.log warn;    #定义错误日志路径信息
pid        /var/run/nginx.pid;               #定义pid文件路径信息

第二个部分:配置文件事件区域

events {
  worker_connections  1024;    #一个worker进程可以同时接受1024个访问请求
}

第三个部分:配置http区域

http {
     include       /etc/nginx/mime.types;       #加载一个配置文件
     default_type  application/octet-stream;    #指定默认识别文件类型
log_format  main ' $remote_addr - $remote_user [$time_local] "$request" '
                 ' $status $body_bytes_sent "$http_referer" '
                 ' "$http_user_agent" "$http_x_forwarded_for" ';
                 #定义日志的格式
     access_log  /var/log/nginx/access.log  main; #指定日志路径
     sendfile        on;
     #tcp_nopush     on;
     keepalive_timeout  65;    #超时时间
     #gzip  on;
     include /etc/nginx/conf.d/*.conf;   #加载一个配置文件
}

/etc/nginx/nginx.d/default        #扩展配置(虚拟主机配置文件)

第四个部分:server区域信息(配置一个网站 www/bbs/blog — 一个虚拟主机)

server {
    listen       80;                         #指定监听的端口
    server_name  localhost;                  #指定网站域名            
    root   /usr/share/nginx/html;            #定义站点目录的位置
    index  index.html index.htm;             #定义首页文件
    error_page   500 502 503 504  /50x.html; #优雅显示页面信息
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

nginx服务的企业应用

利用nginx服务搭建一个网站

1、编写一个虚拟主机配置文件

cd /etc/nginx/conf.d/
vim www.conf
server {
        listen        80;
        server_name   www.test.com;
        location    /test {
            root    /usr/share/nginx/html;
            index   test.html;
        }     
}

2、需要获取开发人员编写的网站代码

<html>
      <meta charset=”utf-8″>
      <head>
        <title>test</title>
      </head>
      <body>
        <table border=1>
          <tr> <td>01</td> <td>test1</td> </tr>
          <tr> <td>02</td> <td>test2</td> </tr>
          <tr> <td>03</td> <td>test3</td> </tr>
        </table>
        <a href=”http://blog.testedu.com”>
          <img src=”test.jpg” />
        </a>
       </body>
</html>

3、重启nginx服务(平滑重启)

方法一:systemctl reload nginx
方法二:nginx -s reload

Nginx命令参数:

-t          :test configuration and exit
              检查测试配置文件语法
-s          :send signal to a master process,quie,reopen,reload        
              控制服务停止或者重新启动

4、编写DNS配置信息

真实域名:在阿里云上进行DNS解析记录配置
模拟域名:在windows主机的hosts文件中进行配置即可
C:\Windows\System32\drivers\etc\hosts

5、进行测试访问

浏览器中:http://www.test.com

部署搭建网站常见错误:

5.1. 网站服务配置文件编写不正确

404 错误

解决方法一:修改nginx配置文件—location
解决方法二:在站点目录中创建相应目录或文件数据信息

403 错误

解决方法一:不要禁止访问
解决方法二:没有首页文件

5.2. DNS信息配置不正确

5.3. 配置文件修改一定要重启服务;

站点目录中代码文件信息调整,不需要重启服务
利用Nginx搭建一个多网站(www bss blog)

5.3.1、创建多个虚拟主机配置文件

bbs.conf
    server {
        listen        80;
        server_name    bbs.test.top;
        location    / {
            root    /html/bbs;
            index    index.html;
        }
    }

blog.conf
    server {
        listen        80;
        server_name    blog.test.top;
        location    / {
            root    /html/blog;
            index    index.html;
        }
    }
www.conf
    server {
        listen        80;
        server_name    www.test.top;
        location    / {
            root    /html/www;
            index    index.html;
        }
    }

5.3.2、创建站点目录和目录中的首页文件

[root@web01 html]mkdir /html/{www,bbs,blog}

[root@web01 html]for name in /html/{www,bbs,blog}; do echo “10.0.0.7 $name.test.top” >> /html/$name/index.html ;done

[root@web01 html]# for name in {www,bbs,blog}; 
do cat  /html/$name/index.html ;done
 10.0.0.7 www.test.top
 10.0.0.7 bbs.test.top
 10.0.0.7 blog.test.top

5.3.3、编写host解析文件

10.0.0.7    www.test.top    bbs.test.top    blog.test.top

5.3.4、进行访问测试

1)利用windows进行浏览器访问测试
2)利用linux进行命令访问测试
[root@web02 ~]# curl www.test.top
10.0.0.7 www.test.top
[root@web02 ~]# curl bbs.test.top
10.0.0.7 bbs.test.top
[root@web02 ~]# curl blog.test.top
10.0.0.7 blog.test.top

3)企业中虚拟主机访问方式

a.基于域名的方式进行访问:
b.基于地址的方式进行访问:(只能用指定地址访问)   

用于负载均衡+高可用服务

server {
        listen        10.0.0.7:80;
        server_name    www.test.top;
        location    / {
           root    /html/www;
           index    index.html;
        }
}

PS:服务配置文件中涉及到地址修改,必须重启nginx服务,不能平滑重启

c.基于端口的方式进行访问:

server {
        listen        8080;
        server_name    www.test.top;
        location    / {
            root    /html/www;
            index    index.html;
        }
}

网站页面的访问原理:

1、将域名进行解析 www.test.top —— 10.0.0.7

2、建立TCP的链接(四层协议)
10.0.0.7 目标端口 80

3、根据应用层HTTP协议发出请求
请求报文:hosts:www.test.top

4、没有相同域名的server主机,会找满足端口要求的第一个主机
显示主机的网站页面
企业中网站的安全访问配置

a.根据用户访问的地址进行控制

10.0.0.0/24    www.test.top/AV/不能访问
172.16.1.0/24    www.test.vom/AV/可以访问

nginx访问模块:ngx_http_access_module

举例配置:

location / {
      deny  192.168.1.1;
      allow 192.168.1.0/24;
      allow 10.1.1.0/16;
      allow 2001:0db8::/32;
      deny  all;
}

指令用法

Syntax:    deny address | CIDR | unix: | all;
Default:    —
Context:    http, server, location, limit_except

1、编写配置文件

server {
        listen        80;
        server_name    www.test.top;
        location    / {
             root    /html/www;
             index    index.html;
        }
        location /AV {
             root     /html/www;
             index    index.html;
             deny 10.0.0.0/24;
             allow 172.16.1.0/24;    
        }
}

补充:

location外面的信息,全局配置信息
location里面的信息,局部配置信息

b.根据用户访问进行认证

nginx认证模块:module ngx_http_auth_basic_module

举例配置:

location / {
       auth_basic           “closed site”;    —开启认证功能
       auth_basic_user_file conf/htpasswd;      —加载用户密码文件
}

1、编写虚拟主机配置文件

server { 
    listen          80;
    server_name     www.test.top;
    location        / {
             root    /html/www;
             index   index.html;   auth_basic “test”;
             auth_basic_user_file password/htpasswd;
    }

2、创建密码文件(文件中密码信息必须是密文的)

htpasswd    创建一个有密文信息的密码文件

htpasswd命令参数说明:

-c  Create a new file.    ***                                            #创建一个密码文件
-n  Don’t update file; display results on stdout.						 #不会更新文件;显示文件内容信息
-b  Use the password from the command line rather than prompting for it. #免交互方式输入用户密码信息   
-i  Read password from stdin without verification (for script usage).    #读取密码采用标准输入方式,并不做检查  
-m  Force MD5 encryption of the password (default).                      #md5的加密算法
-2  Force SHA-256 crypt() hash of the password (secure).
-5  Force SHA-512 crypt() hash of the password (secure).
-B  Force bcrypt aencryption of the password (very secure).              #使用bcrypt对密码进行加密  
-C  Set the computing time used for the bcrypt algorithm
    (higher is more secure but slower, default: 5, valid: 4 to 31).      #使用bcrypt algorithm对密码进行加密  
-r  Set the number of rounds used for the SHA-256, SHA-512 algorithms
    (higher is more secure but slower, default: 5000).
-d  Force CRYPT encryption of the password (8 chars max, insecure).      #密码加密方式
-s  Force SHA-1 encryption of the password (insecure).                   #加密方式
-p  Do not encrypt the password (plaintext, insecure).                   #不进行加密
-D  Delete the specified user.                                           #删除指定用户
-v  Verify password for the specified user.

修改密码文件权限

chmod 600 /etc/nginx/htpasswd

500 Internal Server Error

1.内部程序代码编写有问题
2.程序服务中文件权限不正确

curl命令参数:

-u, –user USER[:PASSWORD]  Server user and password
[root@web02 ~]# curl www.test.top -u test
Enter host password for user ‘test’:
10.0.0.7 www.test.top
posted @ 2021-08-30 11:27  Cai_HL  阅读(77)  评论(0编辑  收藏  举报
>