Nginx安装 、 用户认证 、 Nginx虚拟主机 、 HTTPS加密网站
主机列表
client eth0(192.168.4.10/24) proxy eth0(192.168.4.5/24)、eth1(192.168.2.5/24) web1 eth1(192.168.2.100/24) web2 eth1(192.168.2.200/24)
搭建 proxy
-
配置两块网卡
命令补全 bash-completion
环境变量 $LANG
nginx
-V -s
-
lnmp_soft.tar.gz 解压到家目录下
-
[root@localhost ~]# tar -xf lnmp_soft.tar.gz [root@localhost ~]# cd lnmp_soft [root@localhost lnmp_soft]# tar -xf nginx-1.12.2.tar.gz [root@localhost lnmp_soft]# cd nginx-1.12.2 #安装依赖包 yum -y install gcc pcre-devel openssl-devel make zlib-devel
-
第二步 编译前的配置
-
useradd -s /sbin/nologin nginx /configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module
[root@localhost nginx-1.12.2]# ./configure \ > --prefix=/usr/local/nginx \ > --user=nginx \ > --group=nginx \ > --with-http_ssl_module [root@localhost nginx-1.12.2]# useradd -s /sbin/nologin nginx [root@localhost nginx-1.12.2]# make && make install [root@localhost nginx-1.12.2]# /usr/local/nginx/sbin/nginx [root@localhost nginx-1.12.2]# systemctl stop firewalld - 查看版本及安装信息 [root@localhost nginx-1.12.2]# /usr/local/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
关键文件
/usr/local/nginx conf 配置文件 html 网址根目录 logs 日志 sbin 主程序 /usr/local/nginx/conf/nginx.conf 主配置文件
开启认证
修改主配置文件 - 打开到38行 vim +38 nginx.conf - 网站提示信息 auth_basic "input password"; - 生成账户密码 yum -y install httpd-tools
server { listen 80; server_name localhost; auth_basic "input password"; auth_basic_user_file "/usr/local/nginx/pass"; [root@localhost nginx]# yum -y install httpd-tools # 第一次创建账户 需要c [root@localhost nginx]# htpasswd -c /usr/local/nginx/pass tom # 后续追加账户 不需要c [root@localhost nginx]# htpasswd /usr/local/nginx/pass alice # 删除账户 [root@localhost nginx]# htpasswd -D /usr/local/nginx/pass alice (密码 123 需要连续输入两次密码)
浏览器中 会把账户密码保存到历史记录中
多开虚拟主机
技巧 % 可以切换{} 的头和尾
server {
listen 80;
server_name www.a.com;
root "/usr/local/nginx/html";
index index.html index.htm
}
server {
listen 80;
server_name www.b.com;
root www;
index index.html index.htm
}
# root www;是相对于nginx安装目录的路径 /usr/local/nginx/www
server {
listen 80;
server_name www.a.com;
#auth_basic "input password";
#auth_basic_user_file "/usr/local/nginx/pass";
#charset koi8-r;
server {
listen 8080;
server_name www.b.com;
root www;
index index.html index.htm
}
# 基于IP
server {
listen 192.168.4.5:8080;
server_name www.b.com;
root www;
index index.html index.htm
}
给运行的nginx 添加模块功能
添加功能只到make就结束 /root/lnmp_soft/nginx-1.12.2/objs make后产生的主程序目录, copy nginx到 /usr/local/nginx/sbin/nginx 覆盖,即可完成升级
搭建https的网站
- 对称算法 AES DES 主要应用在单机数据加密 - 非对称算法 RSA DSA主要应用在网络数据加密 - 信息摘要 MD5、sha256 主要应用数据完整性校验 文件abc的完整性校验 md5sum abc sha256sum abc - 将ssl 开启 修改主配置文件 将注释去掉
末行模式
:98,115 s/#//
server {
listen 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 html;
index index.html index.htm;
}
}
# 证书私钥默认存放在conf下面
ssl_certificate cert.pem;
ssl_certificate_key cert.key;
- 生成公钥私钥 - 生成私钥 openssl genrsa > cert.key - 生成证书 证书中有公钥 openssl req -new -x509 -key cert.key > cert.pem
Country Name (2 letter code) [XX]:cn State or Province Name (full name) []:shanxi Locality Name (eg, city) [Default City]:xian Organization Name (eg, company) [Default Company Ltd]:itqzw Organizational Unit Name (eg, section) []:itqzw Common Name (eg, your name or your server's hostname) []:itqzw Email Address []:itqzw@qq.com
点赞









