编译Nginx服务部署静态网站
  qL2sLBQXnavW 2023年11月02日 117 0

Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件 (IMAP/POP3) 代理服务器,并在一个BSD-like协议下发行.其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等.



♥ 文章声明 ♥
该系列文章部分文字描述,整理于以下文献,化繁为简.
《鸟哥的Linux私房菜 (基础学习篇 第三版)》 - 作者:鸟哥
《Linux就该这么学》 - 作者:刘遄
《linux运维之道》- 作者:丁明一



编译安装Nginx

1.配置Yum仓库,安装Nginx所依赖的包文件,以及编译器.

[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost ~]# yum -y install epel-release
[root@localhost ~]# yum install -y gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel

2.编译安装Nginx.

[root@localhost ~]# useradd -s /sbin/nologin -M nginx
[root@localhost ~]# wget http://nginx.org/download/nginx-1.13.12.tar.gz
[root@localhost ~]# tar -xzvf nginx-1.13.12.tar.gz
[root@localhost ~]# cd nginx-1.13.12/

[root@localhost ~]# ./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_stub_status_module

[root@localhost ~]# make && make install

3.检查Nginx配置文件正确性,启动关闭与重启Nginx配置.

[root@localhost ~]# /usr/local/nginx/sbin/nginx -t                       #检测配置文件正确性
[root@localhost ~]# /usr/local/nginx/sbin/nginx #启动Nginx

[root@localhost ~]# kill -QUIT $(cat /usr/local/nginx/logs/nginx.pid) #关闭Nginx
[root@localhost ~]# kill -HUP $(cat /usr/local/nginx/logs/nginx.pid) #重启Nginx


配置Nginx访问控制

◆基于用户名密码的认证◆

作用:当我们打开指定网页时,会提示需要输入密码才能访问,这就是密码认证技术.

1.编辑Nginx主配置文件,在相应的区域中加入以下标★语句.

[root@localhost ~]# vim  /usr/local/nginx/conf/nginx.conf

41 #access_log logs/host.access.log main;
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 #error_page 404 /404.html;
49
50 # redirect server error pages to the static page /50x.html

2.由于Nginx没有生成密码文件的工具,这里需要借助Apache的工具生成密码文件.

[root@localhost ~]# yum install -y httpd
[root@localhost ~]# htpasswd -c /usr/local/nginx/html/login.pad lyshark #创建认证用户(覆盖)
[root@localhost ~]# htpasswd -m /usr/local/nginx/html/login.pad lyshark #写入认证用户(追加)

3.重启Nginx服务,访问指定的页面,即可实现认证.

[root@localhost ~]# kill -HUP $(cat /usr/local/nginx/logs/nginx.pid)

◆基于IP地址的身份认证◆

作用:当我们打开指定网页时,会判断您的IP地址是允许访问还是拒绝访问,这就是基于IP的认证技术

1.编辑Nginx主配置文件,在相应的区域中加入以下任意标★语句,具体情况具体对待

[root@localhost ~]# vim  /usr/local/nginx/conf/nginx.conf

41 #access_log logs/host.access.log main;
42
43 location / {
44 root html;
45 index index.html index.htm;

★ allow 192.168.1.10; #允许单个IP访问
★ deny 192.168.1.10; #拒绝单个IP访问
★ allow 0.0.0.0/0; #允许所有网段
★ deny 0.0.0.0/0; #拒绝所有网段

46 }
47
48 #error_page 404 /404.html;
49
50 # redirect server error pages to the static page /50x.html

2.重启Nginx服务,访问指定的页面,即可实现认证.

[root@localhost ~]# kill -HUP $(cat /usr/local/nginx/logs/nginx.pid)


配置Nginx虚拟主机

利用虚拟主机功能,可以把一台处于运行状态的物理服务器分割成多个,虚拟服务器,出于各种考虑目前各种企业都在使用虚拟主机功能,Nginx虚拟主机功能,是服务器基于用户的请求的不同Ip地址,主机域名或端口号,实现提供多个网站同时为外部提供访问服务的技术,用户取得的资源不同最后取得的页面也会不同.

◆基于IP的虚拟主机◆

如果一台服务器有多个IP地址,而且每个IP地址与服务器上部署的每个网站对应,这样当用户请求访问不同的IP时,会访问到不同网站的页面资源,而且每个网站都有一个独立的IP地址,以下实验将实现在一台服务器上配置多个IP,搭建多个网站,每个网站使用一个IP地址.

1.在eno16777728上配置一个网卡子接口.

[root@localhost ~]# ifconfig

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 ~]# vim /usr/local/nginx/conf/nginx.conf

35 server {
★ listen 192.168.1.10:80; #指定区域1的IP地址
37 server_name localhost;
38
39 location / {
★ root html/vhost1; #指定区域1的文件目录
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; #指定区域2的IP地址
51 server_name localhost;
52
53 location / {
★ root html/vhost2; #指定区域2的文件目录
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 ~]# mkdir /usr/local/nginx/html/vhost1/
[root@localhost ~]# mkdir /usr/local/nginx/html/vhost2/

[root@localhost ~]# echo "ip 1 server" > /usr/local/nginx/html/vhost1/index.html
[root@localhost ~]# echo "ip 2 server" > /usr/local/nginx/html/vhost2/index.html

4.重启Nginx服务,此时访问不同的IP会出现不同的页面

[root@localhost ~]# kill -HUP $(cat /usr/local/nginx/logs/nginx.pid)

◆基于端口的虚拟主机◆

基于端口的虚拟主机,可以让用户通过端口号,来访问服务器上的资源,在使用Nginx配置虚拟网站时,基于端口的配置方式最为复杂,以下实验将实现在一台服务器上配置多个端口,搭建多个网站,每个网站使用一个端口.

1.编辑主配置文件,分别复制并编辑两个主机区域,在相应的区域中加入以下标★语句.

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

35 server { #主机区域1(server1)
★ listen 80; #主机1端口
★ server_name localhost; #主机1服务器名(域名1)
38
39 location / {
★ root html/lyshark-80; #域名1网页存放位置
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 { #主机区域2(server2)
★ listen 8080; #主机区域2端口
★ server_name localhost; #主机2服务器名(域名2)
51
52 location / {
★ root html/lyshark-8080; #域名2网页存放位置
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 ~]# mkdir /usr/local/nginx/html/vhost-80/
[root@localhost ~]# mkdir /usr/local/nginx/html/vhost-8080/

[root@localhost ~]# echo "80 server zone" > /usr/local/nginx/html/vhost-80/index.html
[root@localhost ~]# echo "8080 server zone" > /usr/local/nginx/html/vhost-8080/index.html

3.重启Nginx服务,此时访问同一网站的不同端口,会有不同页面.

[root@localhost ~]# kill -HUP $(cat /usr/local/nginx/logs/nginx.pid)

◆基于域名的虚拟主机◆

当服务器无法为每一个网站分配一个独立的IP的时候,可以尝试让Nginx自动识别用户请求的域名,从而根据不同的域名请求来传输不同的内容,这里我们为了验证实验要手动搭建一个DNS解析,以下实验将实现在一台服务器上多个域名,搭建多个网站,每个网站使用一个域名.

1.首先搭建DNS域名解析,模拟vhost1.com与vhost2.com两个网站域名.

[root@localhost ~]# yum install -y bind bind-chroot
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 ~]# vim /etc/named.conf

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 ~]# vim /etc/named.rfc1912.zones

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 ~]# cp -a /var/named/named.localhost /var/named/vhost1.com.zone
[root@localhost ~]# cp -a /var/named/named.localhost /var/named/vhost2.com.zone

[root@localhost ~]# vim /var/named/vhost1.com.zone
$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 ~]# vim /var/named/vhost2.com.zone
$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 ~]# systemctl restart named

4.编辑主配置文件,分别复制并编辑两个主机区域,在相应的区域中加入以下标★语句.

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

35 server { #主机区域1(server1)
36 listen 80;
★ server_name www.vhost1.com; #主机1服务器名(域名1)
38
39 location / {
★ root html/vhost1; #域名1网页存放位置
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 { #主机区域2(server2)
49 listen 80;
★ server_name www.vhost2.com; #主机2服务器名(域名2)
51
52 location / {
★ root html/vhost2; #域名2网页存放位置
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 ~]# mkdir /usr/local/nginx/html/vhost1/
[root@localhost ~]# mkdir /usr/local/nginx/html/vhost2/

[root@localhost ~]# echo "vhost1 server zone" > /usr/local/nginx/html/vhost1/index.html
[root@localhost ~]# echo "vhost2 server zone" > /usr/local/nginx/html/vhost2/index.html

3.重启Nginx服务,此时访问两个域名,出现两个网站之间不冲突.

[root@localhost ~]# kill -HUP $(cat /usr/local/nginx/logs/nginx.pid)


配置反向代理服务器

作用:当用户访问本台Nginx代理服务器时,会自动跳转到代理的地址上面.

1.修改Nginx主配置文件,在相应的区域中加入以下标★语句.

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

41 #access_log logs/host.access.log main;
42
43 location / {
44 root html; #注释掉
45 index index.html index.htm; #注释掉
★ proxy_pass http://192.168.1.100; #反向代理,当有人访问根时,自动转到100上
46 }
47
48 #error_page 404 /404.html;
49
50 # redirect server error pages to the static page /50x.html

2.重启Nginx服务,访问本机自动跳转到 ​​http://192.168.1.100​​ 地址上去.

[root@localhost ~]# kill -HUP $(cat /usr/local/nginx/logs/nginx.pid)


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 ~]# vim /usr/local/nginx/conf/nginx.conf

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; #负载均衡主机web1
★ server 192.168.1.20:80 weight 2; #负载均衡主机web2

★ server 192.168.1.30:80 weight 1 backup;
★ #backup表示机器处于热备状态,weight代表权重,权重越高使用越多!
★ }
---------------------------------------------------------------------------------------
31
32 server {
33 listen 80;
34 server_name localhost;
35
---------------------------------------------------------------------------------------
36 location / {
★ # root html; #注释掉
★ # index index.html index.htm; #注释掉
★ 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集群.

[root@localhost ~]# kill -HUP $(cat /usr/local/nginx/logs/nginx.pid)


Nginx实现HTTPS加密

作用:证书加密,提高了页面的安全性.

1.安装软件依赖包

[root@localhost ~]# yum install -y pcre-devel zlib-devel openssl openssl-devel

2.建立服务器私钥,过程中需要输入密码.

[root@localhost ~]# openssl genrsa -des3 -out server.key 1024

----------------------------------------------------------------------------
[参数解释]
Genrsa –des3 #加密类型
-out server.key #输出文件
-1024 #加密长度
----------------------------------------------------------------------------

3.建立证书,生成的csr文件交给CA签名后形成服务端自己的证书.

[root@localhost ~]# openssl req -new -key server.key -out server.csr

----------------------------------------------------------------------------
[参数解释]
req -new #新建证书
-key server.key #私钥文件
-out server.csr #输出文件

#注:依次输入:国家 省 市 组织 机构 全称 EMAIL 是否要改变密码 是否改名称
----------------------------------------------------------------------------

4.转化成证书,这一步由证书CA机构来做的,这里只是实验.

[root@localhost ~]# openssl x509 -req -days 365 -sha256 -in server.csr -signkey server.key -out servernew.crt

5.复制证书和密钥到Nginx目录下.

[root@localhost ~]# cp -a server.key /usr/local/nginx/conf/server.key           #复制密钥到conf目录下
[root@localhost ~]# cp -a servernew.crt /usr/local/nginx/conf/server.crt #复制证书到conf目录下

6.修改Nginx主配置文件,在相应的区域中加入以下标★语句.

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

33 #gzip on;
34
35 server {
★ listen 443; #修改端口为443
37 server_name localhost;
38
----------------------------------------------------------------------------
#添加以下内容,启用证书

★ ssl on; #开启SSL加密
★ 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 #error_page 404 /404.html;
53
54 # redirect server error pages to the static page /50x.html

7.重启Nginx服务,即可实现https加密

[root@localhost ~]# kill -HUP $(cat /usr/local/nginx/logs/nginx.pid)

8.访问地址,需要在末尾增加:443

[root@localhost ~]# elinks https://127.0.0.1:443


Nginx实现域名地址跳转

作用:实现访问跳转,比如活动页面暂时跳转.

[实验效果]

当用户访问: http://127.0.0.1/index.html
将地址跳转到: http://59.110.167.239/index.html

1.修改Nginx主配置文件,在相应的区域中加入以下标★语句.

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

24 server {
25 listen 80;
26 server_name localhost;
27
28 #charset koi8-r;
29
30 #access_log logs/host.access.log main;
31
32 location / {
----------------------------------------------------------------------------
★ #root html; #注释掉
★ #index index.html index.htm; #注释掉
----------------------------------------------------------------------------
35
----------------------------------------------------------------------------
#添加以下内容
★ rewrite ^(.*)$ http://59.110.167.239 permanent; #实现地址全跳转(访问本机跳转到59.110.167.239上)
★ rewrite ^(.*)$ https://$host$1 permanent; #实现自身http到https的全跳转
----------------------------------------------------------------------------
37 }
38
39 #error_page 404 /404.html;
40
41 # redirect server error pages to the static page /50x.html

2.重启Nginx服务,加载配置

[root@localhost ~]# kill -HUP $(cat /usr/local/nginx/logs/nginx.pid)

3.手动访问本机 ​​http://127.0.0.1​​​ 将自动跳转到 ​​http://59.110.167.239​

[root@localhost ~]# elinks http://127.0.0.1/index.html


实现自身HTTP到HTTPS跳转

作用:当用户访问自身http地址时,会自动的跳转到https地址去访问.

[实验效果]

用户访问: http://127.0.0.1/index.html
会跳转到: https://127.0.0.1/index.html

1.安装软件依赖包.

[root@localhost ~]# yum install -y pcre-devel zlib-devel openssl openssl-devel

2.建立服务器私钥,过程中需要输入密码.

[root@localhost ~]# openssl genrsa -des3 -out server.key 1024

3.建立证书,生成的csr文件交给CA签名后形成服务端自己的证书.

[root@localhost ~]# openssl req -new -key server.key -out server.csr

4.转化成证书,这一步由证书CA机构来做的,这里只是实验.

[root@localhost ~]# openssl x509 -req -days 365 -sha256 -in server.csr -signkey server.key -out servernew.crt

5.复制证书和密钥到Nginx目录下

[root@localhost ~]# cp -a server.key /usr/local/nginx/conf/server.key      #复制密钥到conf目录下
[root@localhost ~]# cp -a servernew.crt /usr/local/nginx/conf/server.crt #复制证书到conf目录下

6.修改Nginx主配置文件,在相应的区域中加入以下标★语句.

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

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 / {
----------------------------------------------------------------------------
#修改以下内容
★ #root html; #注释掉
★ #index index.html index.htm; #注释掉
★ rewrite ^(.*)$ https://$host$1 permanent; #实现自身http到https的跳转
----------------------------------------------------------------------------
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加密
★ 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服务,加载配置.

[root@localhost ~]# kill -HUP $(cat /usr/local/nginx/logs/nginx.pid)

8.手动访问本机 ​​http://127.0.0.1​​​ 将自动跳转到 ​​https://127.0.0.1​​.

[root@localhost ~]# elinks http://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.访问页面,测试

chrome://net-internals/


实战:实现企业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 ~]# openssl genrsa -des3 -out server.key 1024
[root@localhost ~]# openssl req -new -key server.key -out server.csr
[root@localhost ~]# openssl x509 -req -days 365 -sha256 -in server.csr -signkey server.key -out servernew.crt

b)复制证书和密钥到Nginx目录下

[root@localhost ~]# cp -a server.key /usr/local/nginx/conf/server.key
[root@localhost ~]# cp -a servernew.crt /usr/local/nginx/conf/server.crt

4.修改Nginx主配置文件,在相应的区域中加入以下标★语句

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

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
----------------------------------------------------------------------------
#在此处填写负载均衡语句(在http语句内,server语句外填写)
★ upstream lyshark.com { #自定义区域名

★ server 192.168.1.13:80; #均衡主机1
★ server 192.168.1.14:80; #均衡主机2
★ }
----------------------------------------------------------------------------
19
20
21 server {
22 listen 80;
★ server_name www.lyshark.com; #本机域名
24
25 location / {
----------------------------------------------------------------------------
#配置http到https的自身跳转
★ #root html; #注释掉
★ #index index.html index.htm; #注释掉
★ rewrite ^(.*)$ https://$host$1 permanent; #将http请求跳转到https
----------------------------------------------------------------------------
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加密
★ 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 / {
----------------------------------------------------------------------------
#地址跳转配置
★ #root html; #注释掉
★ #index index.html index.htm; #注释掉
★ proxy_pass http://lyshark.com; #实现地址跳转,有80的请求转443
----------------------------------------------------------------------------
53 }
54
55 error_page 500 502 503 504 /50x.html;
56 location = /50x.html {
57 root html;
58 }
59 }
60 }

5.重启Nginx服务,加载配置

[root@localhost ~]# kill -HUP $(cat /usr/local/nginx/logs/nginx.pid)

6.手动访问 ​​http://127.0.0.1/index.html​​​ 将跳转到 ​​https://127.0.0.1/index.html​

[root@localhost ~]# elinks http://127.0.0.1/index.html


去除版本号

开启nginx监控模块

写入 (约在47行)

location /lyshark {
stub_status on;
}

#查看监控页面输入: http://127.0.0.1/lyshark

源码编译修改版本号

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

//修改完保存退出,编译即可

版权声明:本博客文章与代码均为学习时整理的笔记,文章 [均为原创] 作品,转载请 [添加出处] ,您添加出处是我创作的动力!





【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  ehrZuhofWJiC   2024年05月17日   39   0   0 服务器linux
qL2sLBQXnavW