源码安装nginx
yum -y install gcc pcre-devel openssl-devel
useradd -s /sbin/nologin nginx
tar -xf nginx-1.10.3.tar.gz
cd nginx-1.10.3
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module
make && make install
/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 //查看详细信息
ln -s /usr/local/nginx/sbin/nginx /sbin/ //做软链接,方便后期使用
Nginx升级
升级原因:
1:1.10—1.12旧版本有bug,或者出新的了,就是想升级
2: 1.10—1.10之间安装没有某个功能,现在需要(例:–with-http_ssl_module)
tar -xf nginx-1.12.2.tar.gz
cd nginx-1.12.2
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module //啥都没有变,就版本变了
make //编译
nginx-1.12.2
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/oldnginx //生产环境先做一个移动(备份)
cp objs/nginx /usr/local/nginx/sbin/ //把编译好的文件,复制到之前用的目录下
make upgrade //如果报错,就把Nginx杀掉,重启就更新完了
/usr/local/nginx/sbin/nginx -V //查看下版本号
用户认证
访问web页面需要进行用户认证,用户名为tom,密码为:123456
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name localhost;
auth_basic "Input Password:"; #auth_basic一会登入网站弹个框出来,用来输入用户名帐号和密码,显示的文本就是“”里面的内容,不能为中文,会成为乱码
auth_basic_user_file "/usr/local/nginx/pass";#用来存放密码的文件为“”里面的路径,没有pass路径一会还要自己做
创建用户名密码不是不vim 需要安装httpd-tools
[root@localhost nginx-1.12.2]#yum -y install httpd-tools
[root@localhost nginx-1.12.2]# htpasswd -c /usr/local/nginx/pass tom
####-c 是create的意思,同一个命令不能执行第二遍,如是你想再创建jerry用户,你执行完以后,就会只剩下jerry,如果你想追加,把-c去掉就就可以
New password:
Re-type new password:
Adding password for user tom
[root@localhost nginx-1.12.2]# cat /usr/local/nginx/pass
tom:$apr1$setAeMau$NyZKKoZ0ss4LT471q6EqY/
[root@localhost nginx-1.12.2]# htpasswd /usr/local/nginx/pass jerry
New password:
Re-type new password:
Adding password for user jerry
[root@localhost nginx-1.12.2]# cat /usr/local/nginx/pass
tom:$apr1$setAeMau$NyZKKoZ0ss4LT471q6EqY/
jerry:$apr1$hk2qL3WX$WTza0jHIV4RCeqnG3zNQN/
###又创建一个jerry用户,nginx -s reload加载,测试即可
基于域名虚拟主机
1.实现两个基于域名的虚拟主机,域名分别为www.a.com和www.b.com
2.对域名为www.a.com的站点进行用户认证,用户名称为tom,密码为123456
server {
listen 80;
server_name www.a.com; ///只改了这里
auth_basic "Input Password:";
auth_basic_user_file "/usr/local/nginx/pass";
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#another virtual host using mix of IP-, name-, and port-based configuration
server {
listen 80; //改
server_name somename www.b.com; ///改
charset utf-8; //本页面支持中文,在哪个web主机写,哪个就支持中文
location / {
root www; ///改
index index.html index.htm;
}
}
创建www目录文件
[root@localhost nginx-1.12.2]# mkdir /usr/local/nginx/www
[root@localhost nginx-1.12.2]# echo “www” > /usr/local/nginx/www/index.html
vim /etc/hosts //域名解析
192.168.25.130 www.a.com www.b.com
windows上面做域名解析:
目录:C:\Windows\System32\drivers\etc下的hosts文件,编辑好后
win+R cmd
ipconfig /flushdns //刷新dns的
SSL虚拟主机
源码安装Nginx时必须使用–with-http_ssl_module参数,启用加密模块,对于需要进行SSL加密处理的站点添加ssl相关指令(设置网站需要的私钥和证书)。
加密算法一般分为对称算法、非对称算法、信息摘要。
对称算法有:AES、DES,主要应用在单机数据加密。
非对称算法有:RSA、DSA,主要应用在网络数据加密。
信息摘要:MD5、sha256,主要应用在数据完整性校验。
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/conf/ ##必须在这个目录下,因为调用的时候在这个目录
[root@localhost conf]# openssl genrsa > cert.key #cert.key这个名字可以任意起,为什么这里用呢?因为一会儿修改配置文件里面默认是这个名字,就不用改了
[root@localhost conf]# openssl req -new -x509 -key cert.key > cert.pem ###req请求做一个证书,new新的,-x509证书格式,比如:doc,xls,ppt cert.key根据这个证书所做的公钥,名字为cert.pem
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/conf/ ##必须在这个目录下,因为调用的时候在这个目录
[root@localhost conf]# openssl genrsa > cert.key #cert.key这个名字可以任意起,为什么这里用呢?因为一会儿修改配置文件里面默认是这个名字,就不用改了
Generating RSA private key, 2048 bit long modulus
.+++
..........+++
e is 65537 (0x10001)
[root@localhost conf]# openssl req -new -x509 -key cert.key > cert.pem ###req请求做一个证书,new新的,-x509证书格式,比如:doc,xls,ppt 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) []:a //任意写
Locality Name (eg, city) [Default City]:a
Organization Name (eg, company) [Default Company Ltd]:a
Organizational Unit Name (eg, section) []:a
Common Name (eg, your name or your server's hostname) []:a
Email Address []:a@126.com
98,115 s/#// 98-115行,把#号去掉
vim /usr/local/nginx/conf/nginx.conf
98 server {
99 listen 443 ssl;
100 server_name www.c.com; ###只改了这一个地方
101
102 ssl_certificate cert.pem;
103 ssl_certificate_key cert.key;
104
105 ssl_session_cache shared:SSL:1m;
106 ssl_session_timeout 5m;
107
108 ssl_ciphers HIGH:!aNULL:!MD5;
109 ssl_prefer_server_ciphers on;
110
111 location / {
112 root html;
113 index index.html index.htm;
114 }
115 }
nginx -s reload
下载了火狐浏览器,chrome浏览器做这个实验不行
问题:
1.报错:the ssl parameter requires nginx_http_ssl_module
2.使用IP,会打开默认网站(配置文件中第一个server)
3.做https,http:区别访部方式
4.firefox https://www.c.com
提示不信任,锁不是绿色
问题:利益(自己创建的密钥)
浏览器+合作企业(密钥)钱