侧边栏壁纸
博主头像
zyixin

当你觉得为时已晚的时候,恰恰是最早的时候

  • 累计撰写 64 篇文章
  • 累计创建 0 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

Nginx

zyixin
2022-03-31 / 0 评论 / 0 点赞 / 1,735 阅读 / 7,364 字
温馨提示:
本文最后更新于 2022-04-01,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

1 Nginx简介

· Nginx(“engine x”)
-是俄罗斯人编写的十分轻量级的HTTP服务器
-是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理服务器
-官方地址:http://nginx.org/

1.1 安装Nginx服务器

客户端:192.168.160.10 client
服务端:192.168.160.5 proxy
1)使用源码包安装nginx软件包

[root@proxy ~]# yum -y install gcc pcre-devel openssl-devel        //安装依赖包
[root@proxy ~]# useradd -s /sbin/nologin nginx
[root@proxy ~]# tar -xf nginx-1.10.3.tar.gz
[root@proxy ~]# cd  nginx-1.10.3
[root@proxy nginx-1.10.3]# ./configure   \
> --prefix=/usr/local/nginx   \                //指定安装路径
> --user=nginx   \                             //指定用户
> --group=nginx  \                             //指定组
> --with-http_ssl_module                       //开启SSL加密功能
  .. ..
nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"
[root@proxy nginx-1.10.3]# make && make install    //编译并安装

2)nginx命令的用法

[root@proxy ~]# /usr/local/nginx/sbin/nginx                   //启动服务
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s stop           //关闭服务
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload         //重新加载配置文件
[root@proxy ~]# /usr/local/nginx/sbin/nginx –V                //查看软件信息
[root@proxy ~]# ln -s /usr/local/nginx/sbin/nginx /sbin/      //方便后期使用

netstat命令可以查看系统中启动的端口信息,该命令常用选项如下:
-a显示所有端口的信息
-n以数字格式显示端口号
-t显示TCP连接的端口
-u显示UDP连接的端口
-l显示服务正在监听的端口信息,如httpd启动后,会一直监听80端口
-p显示监听端口的服务名称是什么(也就是程序名称)

nginx服务默认通过TCP 80端口监听客户端请求:

[root@proxy ~]# netstat  -anptu | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      9498/nginx: master

3)测试首页文件

Nginx Web服务默认首页文档存储目录为/usr/local/nginx/html/,在此目录下默认有一个名为index.html的文件,使用客户端访问测试页面:

[root@proxy ~]# curl http://192.168.160.5
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

1.2 升级Nginx服务器

1)编译新版本nginx软件

[root@proxy ~]# tar  -zxvf   nginx-1.12.2.tar.gz
[root@proxy ~]# cd nginx-1.12.2
[root@proxy nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module
[root@proxy nginx-1.12.2]# make

2) 备份老的nginx主程序,并使用编译好的新版本nginx替换老版本

[root@proxy nginx-1.12.2]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginxold
[root@proxy nginx-1.12.2]# cp objs/nginx  /usr/local/nginx/sbin/         //拷贝新版本
[root@proxy nginx-1.12.2]# make upgrade                            //升级
#或者使用killall nginx杀死进程后再启动nginx
/usr/local/nginx/sbin/nginx -t
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
kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
sleep 1
test -f /usr/local/nginx/logs/nginx.pid.oldbin
kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`
[root@proxy ~]# /usr/local/nginx/sbin/nginx
[root@proxy ~]# /usr/local/nginx/sbin/nginx –v                //查看版本

1.3 客户端访问测试

分别使用浏览器和命令行工具curl测试服务器页面
如果使用firefox火狐浏览器,注意在ssh远程的时候一定要加-X选项。

[root@client ~]# firefox http://192.168.4.5
[root@client ~]# curl http://192.168.4.5

2 用户认证

2.1 问题

通过调整Nginx服务端配置,实现以下目标:
1.访问Web页面需要进行用户认证
2.用户名为:tom,密码为:123456

2.2 方案

模板配置文件框架如下:

[root@proxy ~]# vim  /usr/local/nginx/conf/nginx.conf
全局配置(用户名,日志,进程)
http{
    server{
        listen 80;
        server_name localhost;
        root html;
       }
       server{
        listen 80;
        server_name www.zyx.com;
        root www;
       }
}

通过Nginx实现Web页面的认证,需要修改Nginx配置文件,在配置文件中添加auth语句实现用户认证。最后使用htpasswd命令创建用户及密码即可。

2.3 步骤

步骤一:修改Nginx配置文件
1)修改/usr/local/nginx/conf/nginx.conf
auth_basic “Input Password:”; //认证提示符
auth_basic_user_file “/usr/local/nginx/pass”; //认证密码文件

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
        listen       80;
        server_name  localhost;
        auth_basic "Input Password:";                        #认证提示符
        auth_basic_user_file "/usr/local/nginx/pass";        #认证密码文件
        location / {
            root   html;
            index  index.html index.htm;
        }
  }

2)生成密码文件,创建用户及密码
使用htpasswd命令创建账户文件,需要确保系统中已经安装了httpd-tools。

[root@proxy ~]# yum -y install  httpd-tools
[root@proxy ~]# htpasswd -c /usr/local/nginx/pass   tom        //创建密码文件
New password: 
Re-type new password: 
Adding password for user tom
[root@proxy ~]# htpasswd  /usr/local/nginx/pass   jerry      //追加用户,不使用-c选项
New password: 
Re-type new password: 
Adding password for user jerry
[root@proxy ~]# cat /usr/local/nginx/pass

3)重启Nginx服务

[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload    //重新加载配置文件

请先确保nginx是启动状态,否则运行该命令会报错,报错信息如下:
#[error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)

步骤二:客户端测试
登录192.168.4.10客户端主机进行测试

[root@client ~]# firefox http://192.168.4.5                    //输入密码后可以访问

3 基于域名的虚拟主机

3.1 问题

1.配置基于域名的虚拟主机,域名分别为www.a.com和www.b.com
2。对域名为www.a.com的站点进行用户认证,用户名称为tom,密码为123456

3.2 方案

修改Nginx配置文件,添加server容器实现虚拟主机功能;对于需要进行用户认证的虚拟主机添加auth认证语句。

虚拟主机一般可用分为:基于域名、基于IP和基于端口的虚拟主机。

3.3 步骤

步骤一:修改配置文件
1)修改Nginx服务配置,添加相关虚拟主机配置

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
        listen       80;                                      #端口
        server_name  www.a.com;                                #域名
auth_basic "Input Password:";                        #认证提示符
        auth_basic_user_file "/usr/local/nginx/pass";        #认证密码文件
location / {
            root   html;                                    #指定网站根路径
            index  index.html index.htm;
       }

}
… …
    server {
        listen  80;                                        #端口
        server_name  www.b.com;                                #域名
location / {
root   www;                                 #指定网站根路径
index  index.html index.htm;
}
}

2)创建网站根目录及对应首页文件

[root@proxy ~]# mkdir /usr/local/nginx/www
[root@proxy ~]# echo "www" > /usr/local/nginx/www/index.html

3)重启nginx服务

[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

步骤二:客户端测试
1)修改客户端主机192.168.160.10的/etc/hosts文件,进行域名解析

 [root@client ~]# vim /etc/hosts
192.168.160.5    www.a.com  www.b.com

2)登录192.168.4.10客户端主机进行测试

[root@client ~]# firefox http://www.a.com            //输入密码后可以访问
[root@client ~]# firefox http://www.b.com            //直接访问

其他类型的虚拟主机
1.基于端口的虚拟主机(参考模板)

server {
        listen       8080;                        #端口
        server_name  web1.example.com;          #域名
        ......
}
    server {
        listen       8000;                         #端口
        server_name  web1.example.com;           #域名
      .......
}

2.基于IP的虚拟主机(参考模板)

server {
        listen       192.168.0.1:80;              #IP地址与端口
        server_name  web1.example.com;          #域名
  ... ...
}
    server {
        listen       192.168.0.2:80;             #IP地址与端口
        server_name  web1.example.com;
... ...
}

4 SSL虚拟主机

4.1 问题

1.配置基于加密网站的虚拟主机
域名为www.c.com
2.该站点通过https访问
3.通过私钥、证书对该站点所有数据加密

4.2 方案

源码安装Nginx时必须使用–with-http_ssl_module参数,启用加密模块,对于需要进行SSL加密处理的站点添加ssl相关指令(设置网站需要的私钥和证书)。
加密算法一般分为对称算法、非对称算法、信息摘要。
对称算法有:AES、DES,主要应用在单机数据加密。
非对称算法有:RSA、DSA,主要应用在网络数据加密。
信息摘要:MD5、sha256,主要应用在数据完整性校验。

4.3 步骤

步骤一:配置SSL虚拟主机
1)生成私钥与证书

[root@proxy ~]# cd /usr/local/nginx/conf
[root@proxy conf]# openssl genrsa > cert.key                            //生成私钥
[root@proxy conf]# openssl req -new -x509 -key cert.key > cert.pem      //生成证书

2)修改Nginx配置文件,设置加密网站的虚拟主机
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf

… …
# HTTPS server
server {
        listen       443 ssl;
        server_name            www.c.com;
        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   html;
            index  index.html index.htm;
        }
    }

3)重启nginx服务

[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload

步骤二:客户端验证
1)修改客户端主机192.168.160.10的/etc/hosts文件,进行域名解析

[root@client ~]# vim /etc/hosts
192.168.160.5    www.a.com  www.b.com   www.c.com

2)登录192.168.6.10客户端主机进行测试

[root@client ~]# firefox https://www.c.com            //信任证书后可以访问
0

评论区