使用源码包安装nginx软件包
- [root@proxy ~]# yum -y install gcc make #安装编译工具
- [root@proxy ~]# yum -y install pcre-devel #正则表达式依赖包
- [root@proxy ~]# yum -y install openssl-devel #SSL加密依赖包
- [root@proxy ~]# tar -xf nginx-1.22.1.tar.gz
- [root@proxy ~]# 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 #安装
conf 配置文件 sbin 主程序 html 网站页面 logs 日志
2)nginx命令的用法
- /usr/local/nginx/sbin/nginx #启动服务
- /usr/local/nginx/sbin/nginx -s stop #关闭服务
- /usr/local/nginx/sbin/nginx -s reload #重新加载配置文件
- /usr/local/nginx/sbin/nginx -V #查看软件信息
ss命令可以查看系统中启动的端口信息,该命令常用选项如下:
-a显示所有端口的信息
-n以数字格式显示端口号
-t显示TCP连接的端口
-u显示UDP连接的端口
-l显示服务正在监听的端口信息,如httpd启动后,会一直监听80端口
-p显示监听端口的服务名称是什么(也就是程序名称)
nginx服务默认通过TCP 80端口监听客户端请求:
- [root@proxy nginx]# ss -anptu | grep nginx
- ss -ntulp | grep nginx
测试网站自定义页面:echo "abc-test~~~" > html/abc.html #创建新页面
curl http://192.168.88.5
案例2:用户认证
步骤一:修改Nginx配置文件
1)修改/usr/local/nginx/conf/nginx.conf
- auth_basic "Input Password:"; #认证提示符信息
- auth_basic_user_file "/usr/local/nginx/pass"; #认证的密码文件
2)生成密码文件,创建用户及密码
使用htpasswd命令创建账户文件,需要确保系统中已经安装了httpd-tools。
- yum -y install httpd-tools
- [root@proxy nginx]# htpasswd -c /usr/local/nginx/pass tom #创建密码文件
- New password:
- Re-type new password:
[root@proxy nginx]# /usr/local/nginx/sbin/nginx -s reload #重新加载配置文件
- htpasswd /usr/local/nginx/pass jerry #追加用户,不使用-c选项
- cat /usr/local/nginx/pass 可以看到用户名
基于域名的虚拟主机
- http {
- .. ..
- server {
- listen 80; #端口
- server_name www.b.com; #新虚拟主机定义域名
- location / {
- root html_b; #指定网站根路径
- index index.html index.htm; #默认页面
- }
- sbin/nginx -s reload #之后重新加载配置文件,服务必须是开启状态
- [root@proxy nginx]# mkdir html_b #创建b网站的目录
- echo "nginx-A~~~" > html/index.html #创建a网站测试页
- echo "nginx-B~~~" > html_b/index.html #创建b网站测试页
- vim /etc/hosts #修改hosts文件添加ip和域名的映射关系
- 192.168.99.5 www.a.com www.b.com
- curl www.a.com #检测a网站或b网站都可以看到页面
1.基于端口的虚拟主机(参考模板)
- server {
- listen 8080; #端口
- server_name www.a.com; #域名
SSL虚拟主机
源码安装Nginx时必须使用--with-http_ssl_module参数,启用加密模块,对于需要进行SSL加密处理的站点添加ssl相关指令(设置网站需要的私钥和证书)。
加密算法一般分为对称算法、非对称算法、信息摘要。
对称算法有:AES、DES,主要应用在单机数据加密。
非对称算法有:RSA、DSA,主要应用在网络数据加密。
信息摘要:MD5、sha256,主要应用在数据完整性校验。
- [root@proxy nginx]#openssl genrsa > conf/cert.key #生成私钥
- [root@proxy nginx]#openssl req -x509 -key conf/cert.key > conf/cert.pem #生成证书,生成过程会询问诸如你在哪个国家之类的问题,可以随意回答
- mkdir https #创建安全网站的目录
- [root@proxy nginx]# echo "https~~~~ " > https/index.html #创建安全网站的页面
- [root@proxy nginx]# sbin/nginx #服务没开的话开服务
- [root@proxy nginx]# sbin/nginx -s reload #已经开了的话重加载配置
- [root@proxy nginx]# curl -k https://192.168.99.5 #检验,-k是忽略安全风险