ngnix简介和基础

一、Nginx简介

  • Nginx
    • 是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP代理服务器
    • 是一个模块化软件

【1】、安装nginx

  • 使用源码包编译安装

    cd /opt
    # 获取nginx的源码包
    wget https://nginx.org/download/nginx-1.24.0.tar.gz
    
  • 安装源码编译安装的依赖

    yum install -y gcc make pcre-devel openssl-devel
    
  • 编译安装

    cd nginx-1.24.0/
    ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module
    # with-http_ssl_module:安全模块
    make && make install
    
  • 安装完成

    root@proxy[00:32:33]:/opt/nginx-1.24.0
    $ cd /usr/local/nginx/
    root@proxy[00:32:42]:/usr/local/nginx
    $ ls
    conf  html  logs  sbin
    # 创建nginx的执行用户
    root@proxy[00:32:43]:/usr/local/nginx
    $ useradd nginx -s /sbin/nologin
    
  • 启动测试

    root@proxy[00:33:21]:/usr/local/nginx
    $ ./sbin/nginx 
    root@proxy[00:33:32]:/usr/local/nginx
    $ netstat -tunple |  grep 80
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      0          45415      8939/nginx: master
    

【2】、nginx目录解析

  • conf:存放nginx的配置文件
  • html:存放网页文件
  • sbin:主程序目录
  • logs:日志
root@proxy[00:41:29]:/usr/local/nginx
$ ./sbin/nginx -s stop
root@proxy[00:41:41]:/usr/local/nginx
$ ./sbin/nginx -V
nginx version: nginx/1.24.0
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-20) (GCC) 
built with OpenSSL 1.1.1k  FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module

重新加载配置

sbin/nginx -s reload

【3】、nginx配置文件

nginx配置文件分为3部分

指令 参数 ;

指令是在nginx中定义好的,参数可以有也可以没有

./conf/nginx.conf.default:是nginx的配置文件的原始模板,不要修改

# 第39行,取消注释,将原本的编码改为utf-8,网页即可支持中文
charset utf-8;

实现认证功能

在配置认证功能后,别人就不能随便访问了

在虚拟主机的范围中输入如下两行:

auth_basic "password"; 开启认证功能

auth_basic_user_file "/usr/local/nginx/pass"; 谁能访问都在这个文件中,一会新建这个文件

nginx要求:

pass文件中的密码必须是加密的形式,不能是明文。因此我们不能通过vim创建并写入内容

我们下载一个新的工具

yum install -y httpd-tools

这个工具提供了可以进行加密的方式:htpasswd命令

root@proxy[01:27:32]:/usr/local/nginx
$ htpasswd -c /usr/local/nginx/pass tim
New password: 
Re-type new password: 
Adding password for user tim
root@proxy[01:28:49]:/usr/local/nginx
$ cat /usr/local/nginx/pass 
tim:$apr1$pgY4Cs/n$isLfKsG9OPy33UmyhB0hY0
$ htpasswd /usr/local/nginx/pass tom
New password: 
Re-type new password: 
Adding password for user tom

【4】、nginx实现虚拟主机

和httpd一样

nginx也可以设置虚拟主机

修改nginx的配置文件

# 基于域名的虚拟机主机
server{
listen 80;
server_name www.a.com;
root html_a;
index index.html;
}
# 基于端口的虚拟主机
server{
listen 8080;
server_name www.a.com;
root html_a;
index index.html;
}

【5】、加密网站

  • 对称密钥
    • 同一个密码加密,同一个密码解密就是对称加密
    • AES
    • DES
    • 应用案例:RAR、ZIP压缩加密(只适合单机加密)
  • 非对称密钥
    • 使用公钥和私钥实现的
      • 公钥加密(🔒),公钥也被称之为证书
      • 私钥解密(🔑)
      • 比方说我想逛淘宝,淘宝就会发给我一个公钥,我是用淘宝发来的公钥进行加密,然后再进行传输;数据到达服务器后淘宝再使用自己的私钥进行解密
    • RSA
    • DSA
    • 应用案例:网络加密(https、ssh)
  • Hash值
    • MD5
    • SHA256
    • SHA512
      • 主要用于:数据完整性校验

修改nginx配置文件

在nginx中开发者为我们写好了一个实现非对称加密的虚拟主机,只不过是被注释掉了,我们找到后将注释取消即可

由于网站使用加密访问,将采用https协议

#这是nginx为我们写好的Virtual Host,我们只需将注释解除及可
server {
        listen       443 ssl; # https使用的是443端口,ssl是加密方式
        server_name  localhost;

        ssl_certificate      cert.pem;   # 公钥,要和配置文件放在同一 个目录下
        ssl_certificate_key  cert.key; # 私钥,要和配置文件放在同一 个目录下

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
            root   https;
            index  index.html index.htm;
        }

# 生成私钥
openssl genrsa > cert.key
Generating RSA private key, 2048 bit long modulus (2 primes)
..................+++++
.....................+++++
# 生成公钥
openssl req -x509 -key cert.key   > cert.pem
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
# 下面的这些内容目前可以随便填
Country Name (2 letter code) [XX]:cn
State or Province Name (full name) []:da     
Locality Name (eg, city) [Default City]:asd
Organization Name (eg, company) [Default Company Ltd]:aaa
Organizational Unit Name (eg, section) []:ccc
Common Name (eg, your name or your server's hostname) []:qw
Email Address []:ll@ll.com 

# 查看生成的公钥和私钥
ls conf/cert*
conf/cert.key  conf/cert.pem

# 在使用crul访问加密网站时,需要-k参数,来忽略加密
curl -k https://192.168.121.170
https
posted @ 2024-07-22 10:17  Linux小菜鸟  阅读(22)  评论(0编辑  收藏  举报