0
点赞
收藏
分享

微信扫一扫

nginx学习总结

椰果玩安卓 2024-11-18 阅读 24

一、搭建Nginx服务器

环境准备

主机名 IP地址 角色

client eth0:192.168.88.10/24 客户端

proxy eth0:192.168.88.5/24

eth1:192.168.99.5/24 web服务器

web1 eth1:192.168.99.100/24 /

web2 eth1:192.168.99.200/24 /

按照上述要求创建虚拟机,并配置ip地

实验要求

在proxy主机上安装部署Nginx服务,并可以将Nginx服务器,要求编译时启用如下功能:

   支持SSL加密功能

   设置Nginx账户及组名称均为nginx


客户端访问页面验证Nginx Web服务器

   使用浏览器访问

   使用curl访问

构建Nginx服务器

1)使用源码包安装nginx软件包

使用WindTerm或Xshell等工具把软件包拖拽到自己的环境中

[root@proxy ~]# yum -y install gcc make pcre-devel openssl-devel      #安装编译工具,正则表达式依赖包,SSL加密依赖包

[root@proxy ~]# tar -xf /root/lnmp_soft.tar.gz  

[root@proxy ~]# cd lnmp_soft/

[root@proxy ~]# tar -xf nginx-1.22.1.tar.gz

[root@proxy lnmp_soft]# cd nginx-1.22.1/

[root@proxy nginx-1.22.1]# ./configure   --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module  #指定安装路径,指定用户,指定组,开启SSL加密功能

[root@proxy nginx-1.22.1]# make            #编译

[root@proxy nginx-1.22.1]# make install    #安装

[root@proxy nginx-1.22.1]# cd /usr/local/nginx/

[root@proxy nginx]# ls

conf  html  logs  sbin

目录说明:

   conf 配置文件目录

   sbin 主程序目录

   html 网站页面目录

   logs 日志目录

2)启动nginx

[root@proxy nginx]# useradd nginx -s /sbin/nologin

[root@proxy nginx]# /usr/local/nginx/sbin/nginx     #启动服务

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

[root@proxy nginx]# ss  -antlp  |  grep 80

tcp   LISTEN 0      128          0.0.0.0:80          0.0.0.0:*     users:(("nginx",pid=7681,fd=6),("nginx",pid=7680,fd=6))


ss命令可以查看系统中启动的端口信息,该命令常用选项如下:

-a显示所有端口的信息

-n以数字格式显示端口号

-t显示TCP连接的端口

-u显示UDP连接的端口

-l显示服务正在监听的端口信息,如httpd启动后,会一直监听80端口

-p显示监听端口的服务名称是什么(也就是程序名称)


nginx命令其他命令:

[root@proxy nginx]# /usr/local/nginx/sbin/nginx -V                 #查看软件信息

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

[root@proxy nginx]# /usr/local/nginx/sbin/nginx -s stop            #关闭服务

3)客户端访问测试

Nginx服务默认首页文档存储目录为/usr/local/nginx/html/,在此目录下默认有一个index.html的文件

命令行访问:

[root@proxy nginx]# /usr/local/nginx/sbin/nginx          

[root@client ~]# curl http://192.168.88.5    

<html>

<head>

<title>Welcome to nginx!</title>

</head>

...


浏览器访问:

真机浏览器访问192.168.88.5

定义测试页面

1)测试网站自定义页面

[root@proxy nginx]# echo "abc-test~"  >  html/abc.html   #创建新页面

[root@client ~]# curl 192.168.88.5/abc.html     #客户端访问新页面

abc-test~

真机浏览器访问:192.168.88.5/abc.html #如果无效,可以按ctrl+f5强制刷新

2)测试成品网站页面

[root@proxy nginx]# yum -y install unzip     #安装解压缩工具

[root@proxy nginx]# unzip  /root/lnmp_soft/www_template.zip    #解压缩网站模板

[root@proxy nginx]# cp -r www_template/* html/  #拷贝网站模板文件到nginx的网页目录,如果有覆盖提示就输入 y 回车

cp: overwrite 'html/index.html'? y      

真机浏览器访问192.168.88.5

二、用户认证

实验要求

沿用前一个实验,调整Nginx服务端配置,实现以下目标:

   访问nginx的Web页面需要进行用户认证

   用户名为:tom,密码为:123456

修改Nginx配置文件

1)修改nginx.conf,开启认证功能

[root@proxy nginx]# 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 nginx]# yum -y install  httpd-tools #如果已经安装,则不需要执行安装命令

[root@proxy nginx]# htpasswd -c /usr/local/nginx/pass tom    #创建密码文件,-c新创建

New password:           #密码123456

Re-type new password:   #密码123456

Adding password for user tom

3)重新加载配置

[root@proxy nginx]# /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.88.5,会出现一个登录的界面,输入用户名/密码:tom/123456登录即可


1)追加账户

[root@proxy nginx]# htpasswd  /usr/local/nginx/pass   alice    #追加用户,不使用-c选项

New password:           #密码123456

Re-type new password:   #密码123456

Adding password for user alice

[root@proxy ~]# cat /usr/local/nginx/pass

三、基于域名的虚拟主机

实验要求

配置基于域名的虚拟主机,实现两个基于域名的虚拟主机,域名分别为www.a.com和www.b.com

修改配置文件

1)修改Nginx服务配置

添加相关虚拟主机配置如下


[root@proxy nginx]# cp conf/nginx.conf.default conf/nginx.conf  #还原配置文件

[root@proxy nginx]# vim conf/nginx.conf  

...

http {

.. ..

   server {

       listen       80;                           #端口

       server_name  www.b.com;                    #定义虚拟主机域名

       location / {

           root   html_b;                         #指定网站根路径

           index  index.html index.htm;           #默认页面

       }

}

   server {

   listen  80;                                 #端口

   server_name  www.a.com;                     #定义虚拟主机域名

...    

   location / {  

       root   html;                            #指定网站根路径

       index  index.html index.htm;

   }

}

...

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

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

[root@proxy nginx]# echo "nginx-A~~~"  > html/index.html      #创建a网站测试页

[root@proxy nginx]# mkdir html_b                              #创建b网站的目录

[root@proxy nginx]# echo "nginx-B~~~"  > html_b/index.html    #创建b网站测试页


client客户端测试

[root@client ~]# vim  /etc/hosts        #修改hosts文件添加ip和域名的映射关系

192.168.88.5  www.a.com  www.b.com


[root@client ~]# curl  www.a.com

nginx-A~~~

[root@client ~]# curl  www.b.com

nginx-B~~~


如果想在linux中使用浏览器访问,同样在操作

[root@server1 ~]# vim  /etc/hosts    #修改hosts文件添加ip和域名的映射关系

192.168.88.5  www.a.com  www.b.com

浏览器直接访问www.a.com  或 www.b.com 即可


扩展补充:

windows环境配置hosts文件

C:\Windows\System32\drivers\etc\hosts

然后用文本打开hosts,在最后添加

192.168.88.5 www.a.com www.b.com


如果hosts文件是只读,可以

右击hosts文件---属性---安全---编辑---users---完全控制打钩

其他类型的虚拟主机(选做)

1)基于端口的虚拟主机

[root@proxy nginx]# vim conf/nginx.conf  

...

   server {

       listen       8080;               #端口

       server_name  www.a.com;          #域名

       ......

}

   server {

       listen       8000;                #端口

       server_name  www.a.com;           #域名

     .......

}

...

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


client客户端测试

[root@client ~]# curl  www.a.com:8080

nginx-B~~~

[root@client ~]# curl  www.a.com:8000

nginx-A~~~

2)基于IP的虚拟主机(了解,无须操作)

[root@proxy nginx]# vim conf/nginx.conf  

...

  server {

       listen       192.168.88.5:80;    #IP地址与端口

       server_name  www.a.com;          #域名

 ... ...

}

   server {

       listen       192.168.99.5:80;     #IP地址与端口

       server_name  www.a.com;

... ...

}

...

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

四、SSL虚拟主机

实验要求

配置基于加密网站的虚拟主机,实现以下目标:

   该站点通过https访问

   通过私钥、证书对该站点所有数据加密

加密算法说明

源码安装Nginx时必须使用--with-http_ssl_module参数,启用加密模块,对于需要进行SSL加密处理的站点添加ssl相关指令(设置网站需要的私钥和证书)


加密算法一般分为对称算法、非对称算法、信息摘要

对称算法有:AES、DES,主要应用在单机数据加密

非对称算法有:RSA、DSA,主要应用在网络数据加密

信息摘要:MD5、sha256,主要应用在数据完整性校验

[root@proxy nginx]# echo 123 > /root/test

[root@proxy nginx]# md5sum  /root/test


[root@proxy nginx]# echo 1234 > /root/test  #更改文件内容

[root@proxy nginx]# md5sum  /root/test      #md5值已经发生变化

配置SSL虚拟主机

1)修改Nginx配置文件,设置加密网站的虚拟主机

[root@proxy nginx]# cp conf/nginx.conf.default conf/nginx.conf  #还原配置文件

[root@proxy nginx]# vim  /usr/local/nginx/conf/nginx.conf

...

server {

       listen       443 ssl;    #监听端口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;

       }

   }

...    

2)生成私钥与证书

[root@proxy nginx]# openssl genrsa > conf/cert.key            #生成私钥,放到cert.key文件

[root@proxy nginx]# openssl req -x509 -key conf/cert.key > conf/cert.pem    #-x509格式,生成证书,生成过程会询问诸如你在哪个国家之类的问题,可以随意回答

Country Name (2 letter code) [XX]:dc        #国家名

State or Province Name (full name) []:dc    #省份

Locality Name (eg, city) [Default City]:dc  #城市

Organization Name (eg, company) [Default Company Ltd]:dc    #公司

Organizational Unit Name (eg, section) []:dc               #部门

Common Name (eg, your name or your server's hostname) []:dc #服务器名称

Email Address []:dc@dc.com          #电子邮件


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

[root@proxy nginx]# mkdir https    #创建安全网站的目录

[root@proxy nginx]# echo "nginx-https~"  > https/index.html     #创建安全网站的页面

3)客户端验证

命令行测试

[root@client ~]# curl  -k  https://192.168.88.5     #检验,-k是忽略安全风险

nginx-https~      #看到这个内容就说明实验成功


真机浏览器访问:https://192.168.88.5

举报

相关推荐

0 条评论