Ngnix安装及使用


Nginx安装部署

Web服务器对比

  • httpd —Apache
  • tomcat
  • Nginx , Tengine
    • 俄罗斯开发轻量级的http服务器
    • 高性能的http和反向代理服务器,IMAP/POP3/SMTP
    • 官方 http://nginx.org
[root@proxy opt]# ll -dh nginx-1.12.2.tar.gz 
-rw-r--r--. 1 root root 959K Jul  8 05:39 nginx-1.12.2.tar.gz
  • Tengine 淘宝网发起的web服务器项目
    nginx的改进版 ,下图淘宝网站同时使用 Nginx 和 Tengine/Aserver
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LlXAsnGW-1594254108252)(D48CE4A43EF9408E9422EA02E2366CD7)]
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rxNGEkrw-1594254108261)(AB670DA40A314149838D8ABB73E60BF7)]
Nginx优点
  1. 高性能、高并发
    • Nginx 使用更少的内存与CPU资源,支持更多的并发连接,体现更高的效率,因为 Nginx 选择了 epoll and kqueue 作为开发模型
  2. 高可靠、热部署
    • Nginx 可以支持作为 HTTP代理服务器 对外进行服务。Nginx 用 C 编写, 不论是系统资源开销还是 CPU 使用效率都很好,并且极其稳定,配置热更新,几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动
  3. 可扩展、BSD许可
    • 因nginx采用模块化设计,才有了很好的扩展性,又有上面两个非常好的特性,还有BSD许可,所以一些大公司都采用nginx作为反向代理服务器

返回

安装环境准备

  1. 网卡ip配置
  2. yum仓库搭建
  3. 需要的rpm包, 安装
    最小化安装,需安装的几个rpm包
yum -y install net-tools 支持ifconfig的安装包
yum -y install vim  支持vim编辑器安装包
yum -y install bash-completion 支持tab键补全
yum -y install NetworkManager 支持nmcli的安装包
  1. nginx安装必须软件包
  • gcc     c语言写的编译工具
  • pcre-devel   完全支持正则表达式
  • openssl-devel   数据加密,搭建https网站必备
    • 支持https(加密)网站

      返回

源码安装nginx

源码解压

lnmp_soft.tar.gz

→ tar -zxf解压到指定

→ nginx-1.12.2.tar.gz → 继续tar解压

→ cd到 nginx-1.12.2 安装包内

./configure
  • cd /opt/nginx.1.12 进入nginx源码包
[root@proxy nginx-1.12.2]# ./configure \
        --prefix=/usr/local/nginx   \ #指定安装目录
        --user=nginx  \               #指定账户名称, 防止恶意木马, 创建nginx的使用nologin解释器
        --group=nginx  \              #指定组名称
        --with-http_ssl_module  \     #支持加密功能

–with-http_ssl_module 提供SSL加密功能

  • 若不指定用户组, nginx默认归属账户nobody 返回
[root@web1 ~]# id nobody
uid=99(nobody) gid=99(nobody) groups=99(nobody)
  • make && make install

Nginx配置文件及目录

/usr/local/nginx/ 安装目录, 其下文件为
conf/nginx.conf      主配置文件   
html                 网页目录
logs                 日志文件  
sbin/nginx           启动脚本
[root@proxy nginx-1.12.2]# ls /usr/local/nginx/
conf  html  logs  sbin
[root@proxy nginx-1.12.2]# cd /usr/local/nginx/
[root@proxy nginx]# ls
conf  html  logs  sbin
  • /usr/local/nginx/sbin/nginx -V
    • 查看 nginx 版本, 以及安装的模块等信息
[root@proxy nginx]# sbin/nginx -V
nginx version: nginx/1.12.2   //版本信息
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) //安装模块
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
//配置信息
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module
[root@proxy nginx]# 
  • 创建nginx用户, 设置shell登录器为 /sbin/nologin ,防恶意访问
    并开启服务/usr/local/nginx/sbin/nginx
[root@proxy nginx]# sbin/nginx    //配置属主账户,而没有创建用户, 报错
nginx: [emerg] getpwnam("nginx") failed
[root@proxy nginx]# useradd nginx -s /sbin/nologin
[root@proxy nginx]# sbin/nginx
[root@proxy nginx]# 
  • 关防火墙, 查看服务是否开启 返回
[root@proxy nginx]# systemctl stop firewalld.service 
[root@proxy nginx]# netstat -ntulp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      14485/nginx: master 
[root@proxy nginx]# 


sbin/nginx -s stop 关闭服务

sbin/nginx -s reload 重置服务()

netstat -ntulp | grep nginx 查看端口信息

ps aux | grep nginx 查看服务进程信息

  • 测试,找到软件包目录,
anaconda-ks.cfg  lnmp_soft  lnmp_soft.tar.gz
[root@proxy ~]# cd lnmp_soft/
[root@proxy lnmp_soft]# ls
apache-tomcat-8.0.30.tar.gz  nginx-1.15.8.tar.gz  nginx.spec.example    vpn
apache-tomcat-9.0.6.tar.gz   nginx-1.16.1.tar.gz  php_scripts           wordpress.zip
buffer.sh                    nginx-1.17.6.tar.gz  redis-3.0.6.tar.gz    www_template.zip
nginx-1.12.2.tar.gz          nginx.conf.defualt   varnish-5.2.1.tar.gz  zabbix-3.4.4.tar.gz
// 查看unzip 对应的yum仓库软件包
[root@proxy lnmp_soft]# yum provides unzip
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
unzip-6.0-19.el7.x86_64 : A utility for unpacking zip files
Repo        : development

// 装unzip 软件包
[root@proxy lnmp_soft]# yum -y install unzip

  • 使用unzip 解压测试网站素材 返回
[root@proxy lnmp_soft]# unzip www_template.zip 
Archive:  www_template.zip
   creating: www_template/
 //....省略
  • 将网页素材拷贝到 nginx 默认网页根目录 /usr/local/nginx/html/
[root@proxy lnmp_soft]# ls
apache-tomcat-8.0.30.tar.gz  nginx-1.16.1.tar.gz  redis-3.0.6.tar.gz    www_template.zip
apache-tomcat-9.0.6.tar.gz   nginx-1.17.6.tar.gz  varnish-5.2.1.tar.gz  zabbix-3.4.4.tar.gz
buffer.sh                    nginx.conf.defualt   vpn
nginx-1.12.2.tar.gz          nginx.spec.example   wordpress.zip
nginx-1.15.8.tar.gz          php_scripts          www_template
[root@proxy lnmp_soft]# cp -r www_template/* /usr/local/nginx/html/
cp: overwrite ‘/usr/local/nginx/html/index.html’? y
[root@proxy lnmp_soft]# 

  • 真机上测试 http://192.168.4.5/index.html
    返回

主配置文件

1. 支持网站中文显示, 默认不支持中文
  • 素材网站内容删除 rm -rf /usr/local/nginx/html/*
    • echo “proxy_nginx_test_你好” /usr/local/nginx/html/index.html
  • 外网浏览器访问网站, 中文字体出现乱码
  • 更改配置文件/usr/local/nginx/conf/nginx.conf 第40行添加 charset utf-8;
[root@proxy nginx]# vim ./conf/nginx.conf
[root@proxy nginx]# /usr/local/nginx/sbin/nginx -s reload
2. 使用nginx实现网站用户认证功能

auth_basic “password”; //基本身份验证

auth_basic_user_file “/usr/local/nginx/pass”// …用户目录

  • 1. /usr/local/nginx/conf/ngnix.conf 主配置文件
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf

server {
        listen       80;
        server_name  localhost;
        auth_basic "password";                        # 提示信息
        auth_basic_user_file "/usr/local/nginx/pass"; #网站用户信息的存放文件路径     
        charset utf-8;
.....
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8rLcZubl-1594254108263)(792A7943E35A4B3B81DB1C4B57A77532)]
返回

  • 2. yum -y install httpd-tools 安装可生成认证文件的工具
  • 3. htpasswd -c /usr/local/nginx/pass tom 创建认证文件,并添加tom账户,需要输入2次密码
    • -c 创建文件
    • 如需再添加用户, 不要使用-c选项, 使用则会覆盖原有账户信息信息
[root@proxy ~]# htpasswd -c /usr/local/nginx/pass tom
New password: 
Re-type new password: 
Adding password for user tom
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload  #重新加载服务
[root@proxy ~]# 

##############      ###########################
[root@proxy ~]# htpasswd /usr/local/nginx/pass sec_user
New password: 
Re-type new password: 
Adding password for user sec_user
[root@proxy ~]# 

  • 真机上firefox测试
  1. 使用浏览器访问网站时发现需要输入用户名和密码

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TT8epfLH-1594254108267)(FD11F230739045CCB67FF23348559179)]
2. 输入错误,登录失败

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7e9yNNbA-1594254108269)(F77D5FF2E8504CF8961617DEA3D18970)]
3. 若再次测试该功能时, 可能需要清除浏览器的缓存
返回

案例

nginx配置虚拟主机(基于域名)

/usr/local/nginx/conf/nginx.conf

########   httpd的虚拟主机配置  ##################
<virtualhost *:80>
    servername www.a.com
    documentroot /var/www/html
</virtualhost>

#########   nginx的虚拟主机网站配置   #################
http{
#每个server代表一个虚拟主机
    server{             
        listen 80;
        server_name www.a.com;
        root  html;                   #网页文件路径/usr/local/nginx/html
        index index.html index.html; #默认访问网页
    }
    # 添加另外一个虚拟主机 www.b.com
    server{
        isten 80;
        server_name www.b.com;
        root  html_b;                   
        index index.html index.html; 
    }

}

返回

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
[root@proxy ~]# mkdir /usr/local/nginx/html_b
[root@proxy ~]# cd /usr/local/nginx/
[root@proxy nginx]# echo "proxy_web_b" /usr/local/nginx/html_b/index.html
proxy_web_b /usr/local/nginx/html_b/index.html
[root@proxy nginx]# echo "proxy_web_b" > /usr/local/nginx/html_b/index.html
[root@proxy nginx]# /usr/local/nginx/sbin/nginx -s reload

[root@proxy nginx]# vim /etc/hosts
[root@proxy nginx]# curl http://www.a.com
proxy_nginx_test_你好
[root@proxy nginx]# curl http://www.b.com
proxy_web_b
  • linux虚拟机访问
    • /etc/hosts 本地虚拟主机
    • 中添加192.168.4.5 www.a.com www.b.com
    • curl http://www.a.com
    • 或 curl http://www.b.com
[root@client ~]# vim /etc/hosts
[root@client ~]# curl http://www.a.com
proxy_nginx_test_你好
[root@client ~]# curl http://www.b.com
proxy_web_b

返回

  • windows 真机设置 C:\Windows\System32\drivers\etc\hosts
    • 添加192.168.4.5 www.a.com www.b.com
    • 如无法保存,右击→ 属性 → 安全 → users权限改为完全控制

主配置文件下,默认有备份文件为

nginx.conf.default

绝对路径 /usr/local/nginx/conf/ngnix.conf.default

配置基于加密网站的虚拟主机

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

    域名为www.c.com

    该站点通过https访问

    通过私钥、证书对该站点所有数据加密
方案
  • 源码安装Nginx时必须使用 –with-http_ssl_module参数,启用加密模块,对于需要进行SSL加密处理的站点添加ssl相关指令(设置网站需要的私钥和证书)。
  • 加密算法一般分为对称算法、非对称算法、信息摘要
    • 对称算法有:AES、DES,主要应用在单机数据加密。
    • 非对称算法有:RSA、DSA,主要应用在网络数据加密
      • 公钥加密 私钥解密 (服务器将公钥传给客户端, 服务器收到数据,利用私钥进行解密)
    • 信息摘要:MD5、sha256,主要应用在数据完整性校验
      • 对某些数据生成随机序列,用户下载数据后,
      • md5sum 文件名 查看是否与原来一样, 有变化则被篡改. <font size=4 color=>已被破解,服务器不使用
      • sha256sum 文件名 返回
配置文件-- 删除对应前的#

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tVmqMEfI-1594254108271)(0BF2CD7AEBC84E23A895618CCF6F3E0C)]
末行模式下 输入 :107,124s/#//
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LHYhGykl-1594254108273)(252CE1FB9D6348269A5D522394B198E1)]

ssl_certificate cert.pem;
ssl_certificate_key cert.key;

    server {
108         listen       443 ssl;                #加密端口443
109         server_name  www.c.com;              # 域名更改为www.c.com
110 
111         ssl_certificate      cert.pem;
112         ssl_certificate_key  cert.key;
113 
114         ssl_session_cache    shared:SSL:1m;
115         ssl_session_timeout  5m;
116 
117         ssl_ciphers  HIGH:!aNULL:!MD5;
118         ssl_prefer_server_ciphers  on;
119 
120         location / {
121             root   html_c;                  #网页存放地址
122             index  index.html index.htm;
123         }
124     }

  • 创建网页存放地址, 编写默认网页 返回
[root@proxy nginx]# mkdir /usr/local/nginx/html_c
[root@proxy nginx]# echo "proxy_web_c" > /usr/local/nginx/html_c/index.html

  • 生成私钥 cert.key
//cd  /usr/local/nginx/conf/  切换到此目录
[root@proxy conf]# openssl genrsa > cert.key               
Generating RSA private key, 2048 bit long modulus
.........................+++
.+++
e is 65537 (0x10001)
[root@proxy conf]# ls   //查看      
cert.key              fastcgi_params.default  mime.types.default  scgi_params.default
fastcgi.conf          koi-utf                 nginx.conf          uwsgi_params
fastcgi.conf.default  koi-win                 nginx.conf.default  uwsgi_params.default
fastcgi_params        mime.types              scgi_params         win-utf

  • 使用 私钥 生成公钥
openssl req   -x509   -key   cert.key  >  cert.pem
        请求  格式   生成公钥  私钥      生成的公钥

cert.pem 公钥, 必须根据私钥才能生成 返回

[root@proxy conf]# 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]:yse
string is too long, it needs to be less than  2 bytes long
Country Name (2 letter code) [XX]:yse
string is too long, it needs to be less than  2 bytes long
Country Name (2 letter code) [XX]:yse
string is too long, it needs to be less than  2 bytes long
Country Name (2 letter code) [XX]:yse
string is too long, it needs to be less than  2 bytes long
Country Name (2 letter code) [XX]:yse
string is too long, it needs to be less than  2 bytes long
Country Name (2 letter code) [XX]:ys
State or Province Name (full name) []:yse
Locality Name (eg, city) [Default City]:yse
Organization Name (eg, company) [Default Company Ltd]:yse
Organizational Unit Name (eg, section) []:yse
Common Name (eg, your name or your server's hostname) []:yse
Email Address []:yse.com  
[root@proxy conf]# ls                                //cert.pem 公钥
cert.key              fastcgi_params          mime.types          scgi_params           win-utf
cert.pem              fastcgi_params.default  mime.types.default  scgi_params.default
........
[root@proxy conf]# 
  • 重启nginx服务
    [root@proxy conf]# /usr/local/nginx/sbin/nginx -s reload
  • 真机测试
    • C:\Windows\System32\drivers\etc\hosts中添加 192.168.4.5 www.c.com
      [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EqGrgByF-1594254108275)(3991DD48FBB14ED69A4E0875ADD89CA5)]
  • 虚拟机客户端测试 返回
[root@proxy conf]# echo "192.168.4.5 www.c.com" >> /etc/hosts      //本地添加域名
[root@proxy conf]# curl https://www.c.com                          //https访问
curl: (60) Peer's certificate issuer has been marked as not trusted by the user.
More details here: http://curl.haxx.se/docs/sslcerts.html

curl performs SSL certificate verification by default, using a "bundle"
 of Certificate Authority (CA) public keys (CA certs). If the default
 bundle file isn't adequate, you can specify an alternate file
 using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
 the bundle, the certificate verification probably failed due to a
 problem with the certificate (it might be expired, or the name might
 not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
 the -k (or --insecure) option.
[root@proxy conf]# curl -k https://www.c.com                    //-k 去除安全证书
proxy_web_c
[root@proxy conf]# 

返回

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值