Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件 (IMAP/POP3) 代理服务器,并在一个BSD-like协议下发行.其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好.
编译安装Nginx 1.配置Yum仓库,安装Nginx所依赖的包文件,以及编译器.
[root@localhost ~] [root@localhost ~] [root@localhost ~]
2.编译安装Nginx.
[root@localhost ~] [root@localhost ~] [root@localhost ~] [root@localhost ~] [root@localhost ~] --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_stub_status_module [root@localhost ~]
3.检查Nginx配置文件正确性,启动关闭与重启Nginx配置.
[root@localhost ~] [root@localhost ~] [root@localhost ~] [root@localhost ~]
配置Nginx访问控制 ◆基于用户名密码的认证◆
作用:当我们打开指定网页时,会提示需要输入密码才能访问,这就是密码认证技术.
1.编辑Nginx主配置文件,在相应的区域中加入以下标★语句.
[root@localhost ~] 41 42 43 location / { 44 root html; 45 index index.html index.htm; ★ auth_basic "welcome to admin" ; ★ auth_basic_user_file /usr/local/nginx/html/login.pad; 46 } 47 48 49 50
2.由于Nginx没有生成密码文件的工具,这里需要借助Apache的工具生成密码文件.
[root@localhost ~] [root@localhost ~] [root@localhost ~]
3.重启Nginx服务,访问指定的页面,即可实现认证.
◆基于IP地址的身份认证◆
作用:当我们打开指定网页时,会判断您的IP地址是允许访问还是拒绝访问,这就是基于IP的认证技术
1.编辑Nginx主配置文件,在相应的区域中加入以下任意标★语句,具体情况具体对待
[root@localhost ~] 41 42 43 location / { 44 root html; 45 index index.html index.htm; ★ allow 192.168.1.10; ★ deny 192.168.1.10; ★ allow 0.0.0.0/0; ★ deny 0.0.0.0/0; 46 } 47 48 49 50
2.重启Nginx服务,访问指定的页面,即可实现认证.
配置Nginx虚拟主机 利用虚拟主机功能,可以把一台处于运行状态的物理服务器分割成多个,虚拟服务器,出于各种考虑目前各种企业都在使用虚拟主机功能,Nginx虚拟主机功能,是服务器基于用户的请求的不同Ip地址,主机域名或端口号,实现提供多个网站同时为外部提供访问服务的技术,用户取得的资源不同最后取得的页面也会不同.
◆基于IP的虚拟主机◆
如果一台服务器有多个IP地址,而且每个IP地址与服务器上部署的每个网站对应,这样当用户请求访问不同的IP时,会访问到不同网站的页面资源,而且每个网站都有一个独立的IP地址,以下实验将实现在一台服务器上配置多个IP,搭建多个网站,每个网站使用一个IP地址.
1.在eno16777728上配置一个网卡子接口.
[root@localhost ~] eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.10 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::20c:29ff:fe1e:14e2 prefixlen 64 scopeid 0x20<link > ether 00:0c:29:1e:14:e2 txqueuelen 1000 (Ethernet) RX packets 40292 bytes 4129804 (3.9 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 8962 bytes 1557264 (1.4 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eno16777728:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.20 netmask 255.255.255.0 broadcast 192.168.1.255 ether 00:0c:29:1e:14:e2 txqueuelen 1000 (Ethernet)
2.编辑主配置文件,分别复制并编辑两个主机区域,在相应的区域中加入以下标★语句
[root@localhost ~] 35 server { ★ listen 192.168.1.10:80; 37 server_name localhost; 38 39 location / { ★ root html/vhost1; 41 index index.html index.htm; 42 } 43 44 error_page 500 502 503 504 /50x.html; 45 location = /50x.html { 46 root html; 47 } 48 } 49 server { ★ listen 192.168.1.20:80; 51 server_name localhost; 52 53 location / { ★ root html/vhost2; 55 index index.html index.htm; 56 } 57 58 error_page 500 502 503 504 /50x.html; 59 location = /50x.html { 60 root html; 61 } 62 }
3.在html目录下创建相应目录以及html文件.
[root@localhost ~] [root@localhost ~] [root@localhost ~] [root@localhost ~]
4.重启Nginx服务,此时访问不同的IP会出现不同的页面
◆基于端口的虚拟主机◆
基于端口的虚拟主机,可以让用户通过端口号,来访问服务器上的资源,在使用Nginx配置虚拟网站时,基于端口的配置方式最为复杂,以下实验将实现在一台服务器上配置多个端口,搭建多个网站,每个网站使用一个端口.
1.编辑主配置文件,分别复制并编辑两个主机区域,在相应的区域中加入以下标★语句.
[root@localhost ~] 35 server { ★ listen 80; ★ server_name localhost; 38 39 location / { ★ root html/lyshark-80; 41 index index.html index.htm; 42 } 43 error_page 500 502 503 504 /50x.html; 44 location = /50x.html { 45 root html; 46 } 47 } 48 server { ★ listen 8080; ★ server_name localhost; 51 52 location / { ★ root html/lyshark-8080; 54 index index.html index.htm; 55 } 56 57 error_page 500 502 503 504 /50x.html; 58 location = /50x.html { 59 root html; 60 } 61 }
2.在html目录下创建相应目录以及html文件.
[root@localhost ~] [root@localhost ~] [root@localhost ~] [root@localhost ~]
3.重启Nginx服务,此时访问同一网站的不同端口,会有不同页面.
◆基于域名的虚拟主机◆
当服务器无法为每一个网站分配一个独立的IP的时候,可以尝试让Nginx自动识别用户请求的域名,从而根据不同的域名请求来传输不同的内容,这里我们为了验证实验要手动搭建一个DNS解析,以下实验将实现在一台服务器上多个域名,搭建多个网站,每个网站使用一个域名.
1.首先搭建DNS域名解析,模拟vhost1.com与vhost2.com两个网站域名.
[root@localhost ~] Loaded plugins: product-id, search-disabled-repos, subscription-manager This system is not registered with an entitlement server. You can use subscription-manager. Package 32:bind-9.9.4-61.el7.x86_64 already installed and latest version Package 32:bind-chroot-9.9.4-61.el7.x86_64 already installed and latest version Nothing to do
2.配置DNS解析,这里我们简单配置即可,有关DNS详细例子请查看其他相关文章.
[root@localhost ~] 12 options { 13 listen-on port 53 { any; }; 14 listen-on-v6 port 53 { ::1; }; 15 directory "/var/named" ; 16 dump-file "/var/named/data/cache_dump.db" ; 17 statistics-file "/var/named/data/named_stats.txt" ; 18 memstatistics-file "/var/named/data/named_mem_stats.txt" ; 19 allow-query { any; }; [root@localhost ~] 43 zone "vhost1.com" IN { 44 type master; 45 file "vhost1.com.zone" ; 46 allow-update { none; }; 47 }; 48 zone "vhost2.com" IN { 49 type master; 50 file "vhost2.com.zone" ; 51 allow-update { none; }; 52 };
3.拷贝配置文件,并修改成以下模样,并重启Bind.
[root@localhost ~] [root@localhost ~] [root@localhost ~] $TTL 1D@ IN SOA dns.vhost1.com. rname.invalid. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS dns.vhost1.com. dns A 127.0.0.1 www A 192.168.1.10 [root@localhost ~] $TTL 1D@ IN SOA dns.vhost2.com. rname.invalid. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS dns.vhost2.com. dns A 127.0.0.1 www A 192.168.1.10 [root@localhost ~]
4.编辑主配置文件,分别复制并编辑两个主机区域,在相应的区域中加入以下标★语句.
[root@localhost ~] 35 server { 36 listen 80; ★ server_name www.vhost1.com; 38 39 location / { ★ root html/vhost1; 41 index index.html index.htm; 42 } 43 error_page 500 502 503 504 /50x.html; 44 location = /50x.html { 45 root html; 46 } 47 } 48 server { 49 listen 80; ★ server_name www.vhost2.com; 51 52 location / { ★ root html/vhost2; 54 index index.html index.htm; 55 } 56 57 error_page 500 502 503 504 /50x.html; 58 location = /50x.html { 59 root html; 60 } 61 }
2.在html目录下创建相应目录以及html文件.
[root@localhost ~] [root@localhost ~] [root@localhost ~] [root@localhost ~]
3.重启Nginx服务,此时访问两个域名,出现两个网站之间不冲突.
配置反向代理服务器 作用:当用户访问本台Nginx代理服务器时,会自动跳转到代理的地址上面.
1.修改Nginx主配置文件,在相应的区域中加入以下标★语句.
[root@localhost ~] 41 42 43 location / { 44 root html; 45 index index.html index.htm; ★ proxy_pass http://192.168.1.100; 46 } 47 48 49 50
2.重启Nginx服务,访问本机自动跳转到 http://192.168.1.100 地址上去.
Nginx实现负载均衡 作用:当用户访问Nginx负载均衡器时,Nginx将访问请求平均分配给后端的Apache服务器,实现简单的负载均衡.
[实验环境] [IP地址] [主机作用] 192.168.1.100 Nginx负载均衡 192.168.1.10 Apache主机1 192.168.1.20 Apache主机2
1.修改Nginx主配置文件,在相应的区域中加入以下标★语句.
[root@localhost ~] 17 http { 18 include mime.types; 19 default_type application/octet-stream; 20 sendfile on; 21 keepalive_timeout 65; 22 --------------------------------------------------------------------------------------- ★在下处填写负载均衡语句 <语句应写在http语句内并且是在server语句外填写> ★ upstream lyshark.com { ★ ★ server 192.168.1.10:80 weight 1; ★ server 192.168.1.20:80 weight 2; ★ ★ server 192.168.1.30:80 weight 1 backup; ★ ★ } --------------------------------------------------------------------------------------- 31 32 server { 33 listen 80; 34 server_name localhost; 35 --------------------------------------------------------------------------------------- 36 location / { ★ ★ ★ proxy_pass http://lyshark.com; 40 } --------------------------------------------------------------------------------------- 41 42 error_page 500 502 503 504 /50x.html; 43 location = /50x.html { 44 root html; 45 } 46 } 47 }
2.重启Nginx服务,此时访问Nginx时,会自动分配给后端的Apache集群.
Nginx实现HTTPS加密 作用:证书加密,提高了页面的安全性.
1.安装软件依赖包
2.建立服务器私钥,过程中需要输入密码.
[root@localhost ~] ---------------------------------------------------------------------------- [参数解释] Genrsa –des3 -out server.key -1024 ----------------------------------------------------------------------------
3.建立证书,生成的csr文件交给CA签名后形成服务端自己的证书.
[root@localhost ~] ---------------------------------------------------------------------------- [参数解释] req -new -key server.key -out server.csr ----------------------------------------------------------------------------
4.转化成证书,这一步由证书CA机构来做的,这里只是实验.
5.复制证书和密钥到Nginx目录下.
[root@localhost ~] [root@localhost ~]
6.修改Nginx主配置文件,在相应的区域中加入以下标★语句.
[root@localhost ~] 33 34 35 server { ★ listen 443; 37 server_name localhost; 38 ---------------------------------------------------------------------------- ★ ssl on; ★ ssl_certificate server.crt; ★ ssl_certificate_key server.key; ★ ssl_session_timeout 5m; ★ ssl_protocols TLSv1; ★ ssl_ciphers HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM; ★ ssl_prefer_server_ciphers on; ---------------------------------------------------------------------------- 46 47 location / { 48 root html; 49 index index.html index.htm; 50 } 51 52 53 54
7.重启Nginx服务,即可实现https加密
8.访问地址,需要在末尾增加:443
Nginx实现域名地址跳转 作用:实现访问跳转,比如活动页面暂时跳转.
[实验效果] 当用户访问: http://127.0.0.1/index.html 将地址跳转到: http://59.110.167.239/index.html
1.修改Nginx主配置文件,在相应的区域中加入以下标★语句.
[root@localhost ~] 24 server { 25 listen 80; 26 server_name localhost; 27 28 29 30 31 32 location / { ---------------------------------------------------------------------------- ★ ★ ---------------------------------------------------------------------------- 35 ---------------------------------------------------------------------------- ★ rewrite ^(.*)$ http://59.110.167.239 permanent; ★ rewrite ^(.*)$ https://$host$1 permanent; ---------------------------------------------------------------------------- 37 } 38 39 40 41
2.重启Nginx服务,加载配置
3.手动访问本机 http://127.0.0.1 将自动跳转到 http://59.110.167.239
实现自身HTTP到HTTPS跳转 作用:当用户访问自身http地址时,会自动的跳转到https地址去访问.
[实验效果] 用户访问: http://127.0.0.1/index.html 会跳转到: https://127.0.0.1/index.html
1.安装软件依赖包.
2.建立服务器私钥,过程中需要输入密码.
3.建立证书,生成的csr文件交给CA签名后形成服务端自己的证书.
4.转化成证书,这一步由证书CA机构来做的,这里只是实验.
5.复制证书和密钥到Nginx目录下
[root@localhost ~] [root@localhost ~]
6.修改Nginx主配置文件,在相应的区域中加入以下标★语句.
[root@localhost ~] 17 http { 18 include mime.types; 19 default_type application/octet-stream; 20 sendfile on; 21 keepalive_timeout 65; 22 23 server { 24 listen 80; 25 server_name localhost; 26 27 location / { ---------------------------------------------------------------------------- ★ ★ ★ rewrite ^(.*)$ https://$host$1 permanent; ---------------------------------------------------------------------------- 31 } 32 33 error_page 500 502 503 504 /50x.html; 34 location = /50x.html { 35 root html; 36 } 37 } 38 39 server { ★ listen 443; 41 server_name localhost; 42 ---------------------------------------------------------------------------- ★ ssl on; ★ ssl_certificate server.crt; ★ ssl_certificate_key server.key; ★ ssl_session_timeout 5m; ★ ssl_protocols TLSv1; ★ ssl_ciphers HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM; ★ ssl_prefer_server_ciphers on; ---------------------------------------------------------------------------- 50 51 location / { 52 root html; 53 index index.html index.htm; 54 55 } 56 57 error_page 500 502 503 504 /50x.html; 58 location = /50x.html { 59 root html; 60 } 61 } 62 }
7.重启Nginx服务,加载配置.
8.手动访问本机 http://127.0.0.1 将自动跳转到 https://127.0.0.1 .
Nginx配置HSTS 作用:配置HSTS是为了提高网页的安全性,以及防止盗链接,了解即可.
1.在Https的Server站点添加如下头部
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload" ;
2.在Http的Server站点添加相应代码
return 301 https://$host ;
3.添加X-Frame-Options头部,确保不会嵌入到Frame 或 iframe 避免点击劫持
add_header X-Frame-Options "DENY" ;
4.访问页面,测试
实战:实现企业Web环境 作用:本实战项目实现了企业环境的负载均衡,并启动了https认证,当作生产环境无压力.
[实验环境] [主机IP] [主机名称] [主机作用] 192.168.1.12 Nginx 反向代理+https认证 192.168.1.13 Web1 负载主机1 192.168.1.14 Web2 负载主机2 [实验过程] 1.生成SSL证书 2.配置一个DNS,实现本地解析,将192.168.1.12解析成 www.lyshark.com 3.配置好两台后台Apache服务器,12-13 4.安装并配置Nginx 5.Nginx能正常访问后 6.在做做http到https的跳转 7.紧接着跳转代理,做负载均衡 8.最后访问www.lyshark.com实现https跳转,和压力分摊
1.配置DNS服务器,DNS服务器解析到本机Nginx服务器上
Nginx 192.168.1.12 解析成 www.lyshark.com
2.配置两台Apache服务器
Apache Web1 192.168.1.13 Apache Web2 192.168.1.14
3.安装并配置Nginx
a)生成证书与私钥
[root@localhost ~] [root@localhost ~] [root@localhost ~]
b)复制证书和密钥到Nginx目录下
[root@localhost ~] [root@localhost ~]
4.修改Nginx主配置文件,在相应的区域中加入以下标★语句
[root@localhost ~] 1 worker_processes 1; 2 3 events { 4 worker_connections 1024; 5 } 6 7 8 http { 9 include mime.types; 10 default_type application/octet-stream; 11 sendfile on; 12 keepalive_timeout 65; 13 ---------------------------------------------------------------------------- ★ upstream lyshark.com { ★ ★ server 192.168.1.13:80; ★ server 192.168.1.14:80; ★ } ---------------------------------------------------------------------------- 19 20 21 server { 22 listen 80; ★ server_name www.lyshark.com; 24 25 location / { ---------------------------------------------------------------------------- ★ ★ ★ rewrite ^(.*)$ https://$host$1 permanent; ---------------------------------------------------------------------------- 29 } 30 31 error_page 500 502 503 504 /50x.html; 32 location = /50x.html { 33 root html; 34 } 35 } 36 37 server { ★ listen 443; ★ server_name www.lyshark.com; 40 ---------------------------------------------------------------------------- ★ ssl on; ★ ssl_certificate server.crt; ★ ssl_certificate_key server.key; ★ ssl_session_timeout 5m; ★ ssl_protocols TLSv1; ★ ssl_ciphers HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM; ★ ssl_prefer_server_ciphers on; ---------------------------------------------------------------------------- 48 49 location / { ---------------------------------------------------------------------------- ★ ★ ★ proxy_pass http://lyshark.com; ---------------------------------------------------------------------------- 53 } 54 55 error_page 500 502 503 504 /50x.html; 56 location = /50x.html { 57 root html; 58 } 59 } 60 }
5.重启Nginx服务,加载配置
6.手动访问 http://127.0.0.1/index.html 将跳转到 https://127.0.0.1/index.html
去除版本号 开启nginx监控模块
写入 (约在47行) location /lyshark { stub_status on; }
源码编译修改版本号
vim nginx-1.13 .12 /src/core/nginx.h #define nginx_version 1013012 #define NGINX_VERSION "1.13.12" #define NGINX_VER "nginx/" NGINX_VERSION